blob: 225a00c942f0cf7f169f7aa2858b36f99f47e21c [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"
Richard Smithd88a7f12015-09-01 20:35:42 +000016#include "ASTReaderInternals.h"
17#include "MultiOnDiskHashTable.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000021#include "clang/AST/DeclFriend.h"
Richard Smith961eae52014-03-25 01:14:22 +000022#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000028#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000030#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000031#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000032#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000033#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000034#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000035#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000036#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000037#include "clang/Lex/HeaderSearch.h"
38#include "clang/Lex/HeaderSearchOptions.h"
39#include "clang/Lex/MacroInfo.h"
40#include "clang/Lex/PreprocessingRecord.h"
41#include "clang/Lex/Preprocessor.h"
42#include "clang/Lex/PreprocessorOptions.h"
43#include "clang/Sema/IdentifierResolver.h"
44#include "clang/Sema/Sema.h"
45#include "clang/Serialization/ASTReader.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000046#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000047#include "llvm/ADT/APFloat.h"
48#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000049#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000050#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051#include "llvm/Bitcode/BitstreamWriter.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000052#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000053#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000054#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000055#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000056#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000057#include "llvm/Support/Process.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000058#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000059#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000060#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000061#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000062using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000063using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000064
Sebastian Redl3df5a082010-07-30 17:03:48 +000065template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000066static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000067 if (v.empty()) return StringRef();
68 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000069 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000070}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000071
72template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +000073static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000074 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000075 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000076}
77
Douglas Gregoref84c4b2009-04-09 22:27:44 +000078//===----------------------------------------------------------------------===//
79// Type serialization
80//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000081
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000083 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000084 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000085 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000086
87 public:
88 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000089 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +000090 /// \brief Abbreviation to use for the record, if any.
91 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000093 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000094 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095
96 void VisitArrayType(const ArrayType *T);
97 void VisitFunctionType(const FunctionType *T);
98 void VisitTagType(const TagType *T);
99
100#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
101#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102#include "clang/AST/TypeNodes.def"
103 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000104}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000107 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108}
109
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000112 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000117 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000118}
119
Reid Kleckner8a365022013-06-24 17:51:48 +0000120void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
121 Writer.AddTypeRef(T->getOriginalType(), Record);
122 Code = TYPE_DECAYED;
123}
124
Reid Kleckner0503a872013-12-05 01:23:43 +0000125void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
126 Writer.AddTypeRef(T->getOriginalType(), Record);
127 Writer.AddTypeRef(T->getAdjustedType(), Record);
128 Code = TYPE_ADJUSTED;
129}
130
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000131void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000132 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000133 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134}
135
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000137 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
138 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000139 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140}
141
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000143 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000148 Writer.AddTypeRef(T->getPointeeType(), Record);
149 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000150 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 Writer.AddTypeRef(T->getElementType(), Record);
155 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000156 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157}
158
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160 VisitArrayType(T);
161 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000162 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163}
164
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000167 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168}
169
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000171 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000172 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
173 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000174 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000175 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176}
177
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179 Writer.AddTypeRef(T->getElementType(), Record);
180 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000181 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000182 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183}
184
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000185void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000186 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000187 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000188}
189
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000190void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000191 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000192 FunctionType::ExtInfo C = T->getExtInfo();
193 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000194 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000195 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000196 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000197 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000198 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000199
200 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
201 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202}
203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000206 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207}
208
Richard Smith564417a2014-03-20 21:47:22 +0000209static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
210 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000211 Record.push_back(T->getExceptionSpecType());
212 if (T->getExceptionSpecType() == EST_Dynamic) {
213 Record.push_back(T->getNumExceptions());
214 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
215 Writer.AddTypeRef(T->getExceptionType(I), Record);
216 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
217 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000218 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
219 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
220 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000221 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
222 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000223 }
Richard Smith564417a2014-03-20 21:47:22 +0000224}
225
226void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
227 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000228
Richard Smith564417a2014-03-20 21:47:22 +0000229 Record.push_back(T->isVariadic());
230 Record.push_back(T->hasTrailingReturn());
231 Record.push_back(T->getTypeQuals());
232 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
233 addExceptionSpec(Writer, T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000234
235 Record.push_back(T->getNumParams());
236 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
237 Writer.AddTypeRef(T->getParamType(I), Record);
238
239 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
240 T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
241 AbbrevToUse = 0;
242
Sebastian Redl539c5062010-08-18 23:57:32 +0000243 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244}
245
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000246void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000247 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000248 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000249}
John McCallb96ec562009-12-04 22:46:56 +0000250
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000251void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000252 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000253 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
254 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000255 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256}
257
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000258void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000259 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000260 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261}
262
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000263void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000264 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000265 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000266}
267
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000268void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000269 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000270 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000271 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000272}
273
Alexis Hunte852b102011-05-24 22:41:36 +0000274void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
275 Writer.AddTypeRef(T->getBaseType(), Record);
276 Writer.AddTypeRef(T->getUnderlyingType(), Record);
277 Record.push_back(T->getUTTKind());
278 Code = TYPE_UNARY_TRANSFORM;
279}
280
Richard Smith30482bc2011-02-20 03:19:35 +0000281void ASTTypeWriter::VisitAutoType(const AutoType *T) {
282 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000283 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000284 if (T->getDeducedType().isNull())
285 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000286 Code = TYPE_AUTO;
287}
288
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000289void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000290 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000291 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000292 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000293 "Cannot serialize in the middle of a type definition");
294}
295
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000296void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000297 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000298 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000299}
300
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000301void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000302 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000303 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000304}
305
John McCall81904512011-01-06 01:58:22 +0000306void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
307 Writer.AddTypeRef(T->getModifiedType(), Record);
308 Writer.AddTypeRef(T->getEquivalentType(), Record);
309 Record.push_back(T->getAttrKind());
310 Code = TYPE_ATTRIBUTED;
311}
312
Mike Stump11289f42009-09-09 15:08:12 +0000313void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000314ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000315 const SubstTemplateTypeParmType *T) {
316 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
317 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000318 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000319}
320
321void
Douglas Gregorada4b792011-01-14 02:55:32 +0000322ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
323 const SubstTemplateTypeParmPackType *T) {
324 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
325 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
326 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
327}
328
329void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000331 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000332 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000333 Writer.AddTemplateName(T->getTemplateName(), Record);
334 Record.push_back(T->getNumArgs());
335 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
336 ArgI != ArgE; ++ArgI)
337 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000338 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
339 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000340 : T->getCanonicalTypeInternal(),
341 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000343}
344
345void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000346ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000347 VisitArrayType(T);
348 Writer.AddStmt(T->getSizeExpr());
349 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000350 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000351}
352
353void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000354ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000355 const DependentSizedExtVectorType *T) {
356 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000357 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000358}
359
360void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000361ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000362 Record.push_back(T->getDepth());
363 Record.push_back(T->getIndex());
364 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000365 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000366 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000367}
368
369void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000370ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000371 Record.push_back(T->getKeyword());
372 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
373 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000374 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
375 : T->getCanonicalTypeInternal(),
376 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000377 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000378}
379
380void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000381ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000382 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000383 Record.push_back(T->getKeyword());
384 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
385 Writer.AddIdentifierRef(T->getIdentifier(), Record);
386 Record.push_back(T->getNumArgs());
387 for (DependentTemplateSpecializationType::iterator
388 I = T->begin(), E = T->end(); I != E; ++I)
389 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000390 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000391}
392
Douglas Gregord2fa7662010-12-20 02:24:11 +0000393void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
394 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000395 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000396 Record.push_back(*NumExpansions + 1);
397 else
398 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000399 Code = TYPE_PACK_EXPANSION;
400}
401
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000402void ASTTypeWriter::VisitParenType(const ParenType *T) {
403 Writer.AddTypeRef(T->getInnerType(), Record);
404 Code = TYPE_PAREN;
405}
406
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000407void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000408 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000409 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
410 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000411 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000412}
413
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000414void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000415 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000416 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000417 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000418}
419
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000420void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000421 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000422 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000423}
424
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000425void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000426 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregore83b9562015-07-07 03:57:53 +0000427 Record.push_back(T->getTypeArgsAsWritten().size());
428 for (auto TypeArg : T->getTypeArgsAsWritten())
Douglas Gregore9d95f12015-07-07 03:57:35 +0000429 Writer.AddTypeRef(TypeArg, Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000430 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000431 for (const auto *I : T->quals())
432 Writer.AddDeclRef(I, Record);
Douglas Gregorab209d82015-07-07 03:58:42 +0000433 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000434 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000435}
436
Steve Narofffb4330f2009-06-17 22:40:22 +0000437void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000438ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000439 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000440 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000441}
442
Eli Friedman0dfb8892011-10-06 23:00:33 +0000443void
444ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
445 Writer.AddTypeRef(T->getValueType(), Record);
446 Code = TYPE_ATOMIC;
447}
448
John McCall8f115c62009-10-16 21:56:05 +0000449namespace {
450
451class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000452 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000453 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000454
455public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000456 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000457 : Writer(Writer), Record(Record) { }
458
John McCall17001972009-10-18 01:05:36 +0000459#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000460#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000461 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000462#include "clang/AST/TypeLocNodes.def"
463
John McCall17001972009-10-18 01:05:36 +0000464 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
465 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000466};
467
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000468}
John McCall8f115c62009-10-16 21:56:05 +0000469
John McCall17001972009-10-18 01:05:36 +0000470void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
471 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000472}
John McCall17001972009-10-18 01:05:36 +0000473void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000474 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
475 if (TL.needsExtraLocalData()) {
476 Record.push_back(TL.getWrittenTypeSpec());
477 Record.push_back(TL.getWrittenSignSpec());
478 Record.push_back(TL.getWrittenWidthSpec());
479 Record.push_back(TL.hasModeAttr());
480 }
John McCall8f115c62009-10-16 21:56:05 +0000481}
John McCall17001972009-10-18 01:05:36 +0000482void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000484}
John McCall17001972009-10-18 01:05:36 +0000485void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000487}
Reid Kleckner8a365022013-06-24 17:51:48 +0000488void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
489 // nothing to do
490}
Reid Kleckner0503a872013-12-05 01:23:43 +0000491void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
492 // nothing to do
493}
John McCall17001972009-10-18 01:05:36 +0000494void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000496}
John McCall17001972009-10-18 01:05:36 +0000497void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000502}
John McCall17001972009-10-18 01:05:36 +0000503void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000505 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000506}
John McCall17001972009-10-18 01:05:36 +0000507void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
509 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
510 Record.push_back(TL.getSizeExpr() ? 1 : 0);
511 if (TL.getSizeExpr())
512 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000513}
John McCall17001972009-10-18 01:05:36 +0000514void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
515 VisitArrayTypeLoc(TL);
516}
517void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
518 VisitArrayTypeLoc(TL);
519}
520void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
521 VisitArrayTypeLoc(TL);
522}
523void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
524 DependentSizedArrayTypeLoc TL) {
525 VisitArrayTypeLoc(TL);
526}
527void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
528 DependentSizedExtVectorTypeLoc TL) {
529 Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
531void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
532 Writer.AddSourceLocation(TL.getNameLoc(), Record);
533}
534void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
537void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000538 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000539 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
540 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000541 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000542 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
543 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000544}
545void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
546 VisitFunctionTypeLoc(TL);
547}
548void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
549 VisitFunctionTypeLoc(TL);
550}
John McCallb96ec562009-12-04 22:46:56 +0000551void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
553}
John McCall17001972009-10-18 01:05:36 +0000554void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
557void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000558 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
559 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
560 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000561}
562void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000563 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
564 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
565 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
566 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000567}
568void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
569 Writer.AddSourceLocation(TL.getNameLoc(), Record);
570}
Alexis Hunte852b102011-05-24 22:41:36 +0000571void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
572 Writer.AddSourceLocation(TL.getKWLoc(), Record);
573 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
574 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
575 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
576}
Richard Smith30482bc2011-02-20 03:19:35 +0000577void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
578 Writer.AddSourceLocation(TL.getNameLoc(), Record);
579}
John McCall17001972009-10-18 01:05:36 +0000580void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
583void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getNameLoc(), Record);
585}
John McCall81904512011-01-06 01:58:22 +0000586void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
587 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
588 if (TL.hasAttrOperand()) {
589 SourceRange range = TL.getAttrOperandParensRange();
590 Writer.AddSourceLocation(range.getBegin(), Record);
591 Writer.AddSourceLocation(range.getEnd(), Record);
592 }
593 if (TL.hasAttrExprOperand()) {
594 Expr *operand = TL.getAttrExprOperand();
595 Record.push_back(operand ? 1 : 0);
596 if (operand) Writer.AddStmt(operand);
597 } else if (TL.hasAttrEnumOperand()) {
598 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
599 }
600}
John McCall17001972009-10-18 01:05:36 +0000601void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
602 Writer.AddSourceLocation(TL.getNameLoc(), Record);
603}
John McCallcebee162009-10-18 09:09:24 +0000604void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
605 SubstTemplateTypeParmTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getNameLoc(), Record);
607}
Douglas Gregorada4b792011-01-14 02:55:32 +0000608void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
609 SubstTemplateTypeParmPackTypeLoc TL) {
610 Writer.AddSourceLocation(TL.getNameLoc(), Record);
611}
John McCall17001972009-10-18 01:05:36 +0000612void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
613 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000614 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000615 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
616 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
617 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
618 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000619 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
620 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000621}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000622void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
623 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
624 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
625}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000626void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000627 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000628 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000629}
John McCalle78aac42010-03-10 03:28:59 +0000630void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
631 Writer.AddSourceLocation(TL.getNameLoc(), Record);
632}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000633void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000634 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000635 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000636 Writer.AddSourceLocation(TL.getNameLoc(), Record);
637}
John McCallc392f372010-06-11 00:33:02 +0000638void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
639 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000640 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000641 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000642 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000643 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000644 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
645 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
646 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000647 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
648 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000649}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000650void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
651 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
652}
John McCall17001972009-10-18 01:05:36 +0000653void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
654 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000655}
656void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
657 Record.push_back(TL.hasBaseTypeAsWritten());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000658 Writer.AddSourceLocation(TL.getTypeArgsLAngleLoc(), Record);
659 Writer.AddSourceLocation(TL.getTypeArgsRAngleLoc(), Record);
660 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
661 Writer.AddTypeSourceInfo(TL.getTypeArgTInfo(i), Record);
662 Writer.AddSourceLocation(TL.getProtocolLAngleLoc(), Record);
663 Writer.AddSourceLocation(TL.getProtocolRAngleLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000664 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
665 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000666}
John McCallfc93cf92009-10-22 22:37:11 +0000667void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
668 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000669}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000670void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
671 Writer.AddSourceLocation(TL.getKWLoc(), Record);
672 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
673 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
674}
John McCall8f115c62009-10-16 21:56:05 +0000675
Richard Smith01b2cb42014-07-26 06:37:51 +0000676void ASTWriter::WriteTypeAbbrevs() {
677 using namespace llvm;
678
679 BitCodeAbbrev *Abv;
680
681 // Abbreviation for TYPE_EXT_QUAL
682 Abv = new BitCodeAbbrev();
683 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
684 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
685 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
686 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
687
688 // Abbreviation for TYPE_FUNCTION_PROTO
689 Abv = new BitCodeAbbrev();
690 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
691 // FunctionType
692 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
694 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
695 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
696 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
697 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
698 // FunctionProtoType
699 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
700 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
701 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
702 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
703 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
704 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
705 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
706 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
707 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
708}
709
Chris Lattner19cea4e2009-04-22 05:57:30 +0000710//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000711// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000712//===----------------------------------------------------------------------===//
713
Chris Lattner28fa4e62009-04-26 22:26:21 +0000714static void EmitBlockID(unsigned ID, const char *Name,
715 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000716 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000717 Record.clear();
718 Record.push_back(ID);
719 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
720
721 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000722 if (!Name || Name[0] == 0)
723 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000724 Record.clear();
725 while (*Name)
726 Record.push_back(*Name++);
727 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
728}
729
730static void EmitRecordID(unsigned ID, const char *Name,
731 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000732 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000733 Record.clear();
734 Record.push_back(ID);
735 while (*Name)
736 Record.push_back(*Name++);
737 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000738}
739
740static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000741 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000742#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000743 RECORD(STMT_STOP);
744 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000745 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000746 RECORD(STMT_NULL);
747 RECORD(STMT_COMPOUND);
748 RECORD(STMT_CASE);
749 RECORD(STMT_DEFAULT);
750 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000751 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000752 RECORD(STMT_IF);
753 RECORD(STMT_SWITCH);
754 RECORD(STMT_WHILE);
755 RECORD(STMT_DO);
756 RECORD(STMT_FOR);
757 RECORD(STMT_GOTO);
758 RECORD(STMT_INDIRECT_GOTO);
759 RECORD(STMT_CONTINUE);
760 RECORD(STMT_BREAK);
761 RECORD(STMT_RETURN);
762 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000763 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000764 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000765 RECORD(EXPR_PREDEFINED);
766 RECORD(EXPR_DECL_REF);
767 RECORD(EXPR_INTEGER_LITERAL);
768 RECORD(EXPR_FLOATING_LITERAL);
769 RECORD(EXPR_IMAGINARY_LITERAL);
770 RECORD(EXPR_STRING_LITERAL);
771 RECORD(EXPR_CHARACTER_LITERAL);
772 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000773 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000774 RECORD(EXPR_UNARY_OPERATOR);
775 RECORD(EXPR_SIZEOF_ALIGN_OF);
776 RECORD(EXPR_ARRAY_SUBSCRIPT);
777 RECORD(EXPR_CALL);
778 RECORD(EXPR_MEMBER);
779 RECORD(EXPR_BINARY_OPERATOR);
780 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
781 RECORD(EXPR_CONDITIONAL_OPERATOR);
782 RECORD(EXPR_IMPLICIT_CAST);
783 RECORD(EXPR_CSTYLE_CAST);
784 RECORD(EXPR_COMPOUND_LITERAL);
785 RECORD(EXPR_EXT_VECTOR_ELEMENT);
786 RECORD(EXPR_INIT_LIST);
787 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000788 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000789 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000790 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000791 RECORD(EXPR_VA_ARG);
792 RECORD(EXPR_ADDR_LABEL);
793 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000794 RECORD(EXPR_CHOOSE);
795 RECORD(EXPR_GNU_NULL);
796 RECORD(EXPR_SHUFFLE_VECTOR);
797 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000798 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000799 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000800 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000801 RECORD(EXPR_OBJC_ARRAY_LITERAL);
802 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000803 RECORD(EXPR_OBJC_ENCODE);
804 RECORD(EXPR_OBJC_SELECTOR_EXPR);
805 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
806 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
807 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
808 RECORD(EXPR_OBJC_KVC_REF_EXPR);
809 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000810 RECORD(STMT_OBJC_FOR_COLLECTION);
811 RECORD(STMT_OBJC_CATCH);
812 RECORD(STMT_OBJC_FINALLY);
813 RECORD(STMT_OBJC_AT_TRY);
814 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
815 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000816 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000817 RECORD(STMT_CXX_CATCH);
818 RECORD(STMT_CXX_TRY);
819 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000820 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000821 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000822 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000823 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000824 RECORD(EXPR_CXX_STATIC_CAST);
825 RECORD(EXPR_CXX_DYNAMIC_CAST);
826 RECORD(EXPR_CXX_REINTERPRET_CAST);
827 RECORD(EXPR_CXX_CONST_CAST);
828 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000829 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000830 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000831 RECORD(EXPR_CXX_BOOL_LITERAL);
832 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000833 RECORD(EXPR_CXX_TYPEID_EXPR);
834 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000835 RECORD(EXPR_CXX_THIS);
836 RECORD(EXPR_CXX_THROW);
837 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000838 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000839 RECORD(EXPR_CXX_BIND_TEMPORARY);
840 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
841 RECORD(EXPR_CXX_NEW);
842 RECORD(EXPR_CXX_DELETE);
843 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
844 RECORD(EXPR_EXPR_WITH_CLEANUPS);
845 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
846 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
847 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
848 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
849 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000850 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000851 RECORD(EXPR_CXX_NOEXCEPT);
852 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000853 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
854 RECORD(EXPR_TYPE_TRAIT);
855 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000856 RECORD(EXPR_PACK_EXPANSION);
857 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000858 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000859 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000860 RECORD(EXPR_FUNCTION_PARM_PACK);
861 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000862 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000863 RECORD(EXPR_CXX_UUIDOF_EXPR);
864 RECORD(EXPR_CXX_UUIDOF_TYPE);
865 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000866#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000867}
Mike Stump11289f42009-09-09 15:08:12 +0000868
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000869void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000870 RecordData Record;
871 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000872
Sebastian Redl539c5062010-08-18 23:57:32 +0000873#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
874#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000875
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000876 // Control Block.
877 BLOCK(CONTROL_BLOCK);
878 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000879 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000880 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +0000881 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000882 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000883 RECORD(IMPORTS);
884 RECORD(LANGUAGE_OPTIONS);
885 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000886 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000887 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000888 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000889 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000890 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000891 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000892 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000893 RECORD(PREPROCESSOR_OPTIONS);
894
Douglas Gregor108cb222012-10-19 00:45:00 +0000895 BLOCK(INPUT_FILES_BLOCK);
896 RECORD(INPUT_FILE);
897
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000898 // AST Top-Level Block.
899 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000900 RECORD(TYPE_OFFSET);
901 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000902 RECORD(IDENTIFIER_OFFSET);
903 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000904 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000905 RECORD(SPECIAL_TYPES);
906 RECORD(STATISTICS);
907 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000908 RECORD(SELECTOR_OFFSETS);
909 RECORD(METHOD_POOL);
910 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000911 RECORD(SOURCE_LOCATION_OFFSETS);
912 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000913 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +0000914 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000915 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +0000916 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000917 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000918 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000919 RECORD(SEMA_DECL_REFS);
920 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
921 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
922 RECORD(DECL_REPLACEMENTS);
923 RECORD(UPDATE_VISIBLE);
924 RECORD(DECL_UPDATE_OFFSETS);
925 RECORD(DECL_UPDATES);
926 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
927 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000928 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000929 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000930 RECORD(FP_PRAGMA_OPTIONS);
931 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000932 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000933 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000934 RECORD(MODULE_OFFSET_MAP);
935 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000936 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000937 RECORD(FILE_SORTED_DECLS);
938 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000939 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000940 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +0000941 RECORD(INTERESTING_IDENTIFIERS);
942 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000943 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000944 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +0000945 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
946 RECORD(CXX_CTOR_INITIALIZERS_OFFSETS);
947 RECORD(DELETE_EXPRS_TO_ANALYZE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000948
Chris Lattner28fa4e62009-04-26 22:26:21 +0000949 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000950 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000951 RECORD(SM_SLOC_FILE_ENTRY);
952 RECORD(SM_SLOC_BUFFER_ENTRY);
953 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000954 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000955
Chris Lattner28fa4e62009-04-26 22:26:21 +0000956 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000957 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +0000958 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000959 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +0000960 RECORD(PP_MACRO_OBJECT_LIKE);
961 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000962 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +0000963
Richard Smithfa715652015-08-31 22:43:10 +0000964 // Submodule Block.
965 BLOCK(SUBMODULE_BLOCK);
966 RECORD(SUBMODULE_METADATA);
967 RECORD(SUBMODULE_DEFINITION);
968 RECORD(SUBMODULE_UMBRELLA_HEADER);
969 RECORD(SUBMODULE_HEADER);
970 RECORD(SUBMODULE_TOPHEADER);
971 RECORD(SUBMODULE_UMBRELLA_DIR);
972 RECORD(SUBMODULE_IMPORTS);
973 RECORD(SUBMODULE_EXPORTS);
974 RECORD(SUBMODULE_REQUIRES);
975 RECORD(SUBMODULE_EXCLUDED_HEADER);
976 RECORD(SUBMODULE_LINK_LIBRARY);
977 RECORD(SUBMODULE_CONFIG_MACRO);
978 RECORD(SUBMODULE_CONFLICT);
979 RECORD(SUBMODULE_PRIVATE_HEADER);
980 RECORD(SUBMODULE_TEXTUAL_HEADER);
981 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
982
983 // Comments Block.
984 BLOCK(COMMENTS_BLOCK);
985 RECORD(COMMENTS_RAW_COMMENT);
986
Douglas Gregor12bfa382009-10-17 00:13:19 +0000987 // Decls and Types block.
988 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000989 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000990 RECORD(TYPE_COMPLEX);
991 RECORD(TYPE_POINTER);
992 RECORD(TYPE_BLOCK_POINTER);
993 RECORD(TYPE_LVALUE_REFERENCE);
994 RECORD(TYPE_RVALUE_REFERENCE);
995 RECORD(TYPE_MEMBER_POINTER);
996 RECORD(TYPE_CONSTANT_ARRAY);
997 RECORD(TYPE_INCOMPLETE_ARRAY);
998 RECORD(TYPE_VARIABLE_ARRAY);
999 RECORD(TYPE_VECTOR);
1000 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001001 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001002 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001003 RECORD(TYPE_TYPEDEF);
1004 RECORD(TYPE_TYPEOF_EXPR);
1005 RECORD(TYPE_TYPEOF);
1006 RECORD(TYPE_RECORD);
1007 RECORD(TYPE_ENUM);
1008 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001009 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001010 RECORD(TYPE_DECLTYPE);
1011 RECORD(TYPE_ELABORATED);
1012 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1013 RECORD(TYPE_UNRESOLVED_USING);
1014 RECORD(TYPE_INJECTED_CLASS_NAME);
1015 RECORD(TYPE_OBJC_OBJECT);
1016 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1017 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1018 RECORD(TYPE_DEPENDENT_NAME);
1019 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1020 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1021 RECORD(TYPE_PAREN);
1022 RECORD(TYPE_PACK_EXPANSION);
1023 RECORD(TYPE_ATTRIBUTED);
1024 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001025 RECORD(TYPE_AUTO);
1026 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001027 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001028 RECORD(TYPE_DECAYED);
1029 RECORD(TYPE_ADJUSTED);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001030 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001031 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001032 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001033 RECORD(DECL_ENUM);
1034 RECORD(DECL_RECORD);
1035 RECORD(DECL_ENUM_CONSTANT);
1036 RECORD(DECL_FUNCTION);
1037 RECORD(DECL_OBJC_METHOD);
1038 RECORD(DECL_OBJC_INTERFACE);
1039 RECORD(DECL_OBJC_PROTOCOL);
1040 RECORD(DECL_OBJC_IVAR);
1041 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001042 RECORD(DECL_OBJC_CATEGORY);
1043 RECORD(DECL_OBJC_CATEGORY_IMPL);
1044 RECORD(DECL_OBJC_IMPLEMENTATION);
1045 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1046 RECORD(DECL_OBJC_PROPERTY);
1047 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001048 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001049 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001050 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001051 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001052 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001053 RECORD(DECL_FILE_SCOPE_ASM);
1054 RECORD(DECL_BLOCK);
1055 RECORD(DECL_CONTEXT_LEXICAL);
1056 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001057 RECORD(DECL_NAMESPACE);
1058 RECORD(DECL_NAMESPACE_ALIAS);
1059 RECORD(DECL_USING);
1060 RECORD(DECL_USING_SHADOW);
1061 RECORD(DECL_USING_DIRECTIVE);
1062 RECORD(DECL_UNRESOLVED_USING_VALUE);
1063 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1064 RECORD(DECL_LINKAGE_SPEC);
1065 RECORD(DECL_CXX_RECORD);
1066 RECORD(DECL_CXX_METHOD);
1067 RECORD(DECL_CXX_CONSTRUCTOR);
1068 RECORD(DECL_CXX_DESTRUCTOR);
1069 RECORD(DECL_CXX_CONVERSION);
1070 RECORD(DECL_ACCESS_SPEC);
1071 RECORD(DECL_FRIEND);
1072 RECORD(DECL_FRIEND_TEMPLATE);
1073 RECORD(DECL_CLASS_TEMPLATE);
1074 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1075 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001076 RECORD(DECL_VAR_TEMPLATE);
1077 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1078 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001079 RECORD(DECL_FUNCTION_TEMPLATE);
1080 RECORD(DECL_TEMPLATE_TYPE_PARM);
1081 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1082 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1083 RECORD(DECL_STATIC_ASSERT);
1084 RECORD(DECL_CXX_BASE_SPECIFIERS);
1085 RECORD(DECL_INDIRECTFIELD);
1086 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1087
Douglas Gregor03412ba2011-06-03 02:27:19 +00001088 // Statements and Exprs can occur in the Decls and Types block.
1089 AddStmtsExprs(Stream, Record);
1090
Douglas Gregor92a96f52011-02-08 21:58:10 +00001091 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001092 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001093 RECORD(PPD_MACRO_DEFINITION);
1094 RECORD(PPD_INCLUSION_DIRECTIVE);
1095
Chris Lattner28fa4e62009-04-26 22:26:21 +00001096#undef RECORD
1097#undef BLOCK
1098 Stream.ExitBlock();
1099}
1100
Richard Smith54cc3c22014-12-11 20:50:24 +00001101/// \brief Prepares a path for being written to an AST file by converting it
1102/// to an absolute path and removing nested './'s.
1103///
1104/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001105static bool cleanPathForOutput(FileManager &FileMgr,
1106 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001107 bool Changed = FileMgr.makeAbsolutePath(Path);
1108 return Changed | FileMgr.removeDotPaths(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001109}
1110
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001111/// \brief Adjusts the given filename to only write out the portion of the
1112/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001113///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001114/// \param Filename the file name to adjust.
1115///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001116/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1117/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001118///
1119/// \returns either the original filename (if it needs no adjustment) or the
1120/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001121static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001122adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001123 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001124
Richard Smith7ed1bc92014-12-05 22:42:13 +00001125 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001126 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001127
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001128 // Verify that the filename and the system root have the same prefix.
1129 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001130 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1131 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001132 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001133
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001134 // We hit the end of the filename before we hit the end of the system root.
1135 if (!Filename[Pos])
1136 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001137
Richard Smith7ed1bc92014-12-05 22:42:13 +00001138 // If there's not a path separator at the end of the base directory nor
1139 // immediately after it, then this isn't within the base directory.
1140 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1141 if (!llvm::sys::path::is_separator(BaseDir.back()))
1142 return Filename;
1143 } else {
1144 // If the file name has a '/' at the current position, skip over the '/'.
1145 // We distinguish relative paths from absolute paths by the
1146 // absence of '/' at the beginning of relative paths.
1147 //
1148 // FIXME: This is wrong. We distinguish them by asking if the path is
1149 // absolute, which isn't the same thing. And there might be multiple '/'s
1150 // in a row. Use a better mechanism to indicate whether we have emitted an
1151 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001152 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001153 }
Mike Stump11289f42009-09-09 15:08:12 +00001154
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001155 return Filename + Pos;
1156}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001157
Ben Langmuir487ea142014-10-23 18:05:36 +00001158static ASTFileSignature getSignature() {
1159 while (1) {
1160 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1161 return S;
1162 // Rely on GetRandomNumber to eventually return non-zero...
1163 }
1164}
1165
Douglas Gregor112b9072012-10-18 05:31:06 +00001166/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001167void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1168 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001169 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001170 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001171 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1172 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001173
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001174 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001175 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1176 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1177 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1178 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1179 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1180 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1181 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001182 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001183 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1184 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1185 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1186 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001187 Record.push_back(VERSION_MAJOR);
1188 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001189 Record.push_back(CLANG_VERSION_MAJOR);
1190 Record.push_back(CLANG_VERSION_MINOR);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001191 assert((!WritingModule || isysroot.empty()) &&
1192 "writing module as a relocatable PCH?");
Douglas Gregorc567ba22011-07-22 16:35:34 +00001193 Record.push_back(!isysroot.empty());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001194 Record.push_back(IncludeTimestamps);
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001195 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001196 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1197 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001198
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001199 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001200 // For implicit modules we output a signature that we can use to ensure
1201 // duplicate module builds don't collide in the cache as their output order
1202 // is non-deterministic.
1203 // FIXME: Remove this when output is deterministic.
1204 if (Context.getLangOpts().ImplicitModules) {
1205 Record.clear();
1206 Record.push_back(getSignature());
1207 Stream.EmitRecord(SIGNATURE, Record);
1208 }
1209
Richard Smith7ed1bc92014-12-05 22:42:13 +00001210 // Module name
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001211 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1212 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1214 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1215 RecordData Record;
1216 Record.push_back(MODULE_NAME);
1217 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1218 }
1219
Richard Smith7ed1bc92014-12-05 22:42:13 +00001220 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001221 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001222 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001223
1224 // If the home of the module is the current working directory, then we
1225 // want to pick up the cwd of the build process loading the module, not
1226 // our cwd, when we load this module.
1227 if (!PP.getHeaderSearchInfo()
1228 .getHeaderSearchOpts()
1229 .ModuleMapFileHomeIsCwd ||
1230 WritingModule->Directory->getName() != StringRef(".")) {
1231 // Module directory.
1232 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1233 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1235 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1236
1237 RecordData Record;
1238 Record.push_back(MODULE_DIRECTORY);
1239 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1240 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001241
1242 // Write out all other paths relative to the base directory if possible.
1243 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1244 } else if (!isysroot.empty()) {
1245 // Write out paths relative to the sysroot if possible.
1246 BaseDirectory = isysroot;
1247 }
1248
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001249 // Module map file
1250 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001251 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001252
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001253 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1254
1255 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001256 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001257
1258 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001259 if (auto *AdditionalModMaps =
1260 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001261 Record.push_back(AdditionalModMaps->size());
1262 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001263 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001264 } else {
1265 Record.push_back(0);
1266 }
1267
1268 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001269 }
1270
Douglas Gregor112b9072012-10-18 05:31:06 +00001271 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001272 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001273 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001274 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001275
Richard Smith7f330cd2015-03-18 01:42:29 +00001276 for (auto *M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001277 // Skip modules that weren't directly imported.
Richard Smith7f330cd2015-03-18 01:42:29 +00001278 if (!M->isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001279 continue;
1280
Richard Smith7f330cd2015-03-18 01:42:29 +00001281 Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1282 AddSourceLocation(M->ImportLoc, Record);
1283 Record.push_back(M->File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001284 Record.push_back(getTimestampForOutput(M->File));
Richard Smith7f330cd2015-03-18 01:42:29 +00001285 Record.push_back(M->Signature);
1286 AddPath(M->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001287 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001288 Stream.EmitRecord(IMPORTS, Record);
1289 }
Mike Stump11289f42009-09-09 15:08:12 +00001290
Douglas Gregor112b9072012-10-18 05:31:06 +00001291 // Language options.
1292 Record.clear();
1293 const LangOptions &LangOpts = Context.getLangOpts();
1294#define LANGOPT(Name, Bits, Default, Description) \
1295 Record.push_back(LangOpts.Name);
1296#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1297 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001298#include "clang/Basic/LangOptions.def"
1299#define SANITIZER(NAME, ID) \
1300 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001301#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001302
Ben Langmuircd98cb72015-06-23 18:20:18 +00001303 Record.push_back(LangOpts.ModuleFeatures.size());
1304 for (StringRef Feature : LangOpts.ModuleFeatures)
1305 AddString(Feature, Record);
1306
Douglas Gregor112b9072012-10-18 05:31:06 +00001307 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1308 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001309
1310 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001311
1312 // Comment options.
1313 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1314 for (CommentOptions::BlockCommandNamesTy::const_iterator
1315 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1316 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1317 I != IEnd; ++I) {
1318 AddString(*I, Record);
1319 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001320 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001321
Douglas Gregor112b9072012-10-18 05:31:06 +00001322 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1323
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001324 // Target options.
1325 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001326 const TargetInfo &Target = Context.getTargetInfo();
1327 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001328 AddString(TargetOpts.Triple, Record);
1329 AddString(TargetOpts.CPU, Record);
1330 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001331 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1332 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1333 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1334 }
1335 Record.push_back(TargetOpts.Features.size());
1336 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1337 AddString(TargetOpts.Features[I], Record);
1338 }
1339 Stream.EmitRecord(TARGET_OPTIONS, Record);
1340
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001341 // Diagnostic options.
1342 Record.clear();
1343 const DiagnosticOptions &DiagOpts
1344 = Context.getDiagnostics().getDiagnosticOptions();
1345#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1346#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1347 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1348#include "clang/Basic/DiagnosticOptions.def"
1349 Record.push_back(DiagOpts.Warnings.size());
1350 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1351 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001352 Record.push_back(DiagOpts.Remarks.size());
1353 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1354 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001355 // Note: we don't serialize the log or serialization file names, because they
1356 // are generally transient files and will almost always be overridden.
1357 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1358
Douglas Gregorc6317db2012-10-24 15:49:58 +00001359 // File system options.
1360 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001361 const FileSystemOptions &FSOpts =
1362 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001363 AddString(FSOpts.WorkingDir, Record);
1364 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1365
Douglas Gregor2d302362012-10-24 16:50:34 +00001366 // Header search options.
1367 Record.clear();
1368 const HeaderSearchOptions &HSOpts
1369 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1370 AddString(HSOpts.Sysroot, Record);
1371
1372 // Include entries.
1373 Record.push_back(HSOpts.UserEntries.size());
1374 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1375 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1376 AddString(Entry.Path, Record);
1377 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001378 Record.push_back(Entry.IsFramework);
1379 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001380 }
1381
1382 // System header prefixes.
1383 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1384 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1385 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1386 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1387 }
1388
1389 AddString(HSOpts.ResourceDir, Record);
1390 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001391 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001392 Record.push_back(HSOpts.DisableModuleHash);
1393 Record.push_back(HSOpts.UseBuiltinIncludes);
1394 Record.push_back(HSOpts.UseStandardSystemIncludes);
1395 Record.push_back(HSOpts.UseStandardCXXIncludes);
1396 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001397 // Write out the specific module cache path that contains the module files.
1398 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001399 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1400
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001401 // Preprocessor options.
1402 Record.clear();
1403 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1404
1405 // Macro definitions.
1406 Record.push_back(PPOpts.Macros.size());
1407 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1408 AddString(PPOpts.Macros[I].first, Record);
1409 Record.push_back(PPOpts.Macros[I].second);
1410 }
1411
1412 // Includes
1413 Record.push_back(PPOpts.Includes.size());
1414 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1415 AddString(PPOpts.Includes[I], Record);
1416
1417 // Macro includes
1418 Record.push_back(PPOpts.MacroIncludes.size());
1419 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1420 AddString(PPOpts.MacroIncludes[I], Record);
1421
Douglas Gregorb6368752012-10-24 23:41:50 +00001422 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001423 // Detailed record is important since it is used for the module cache hash.
1424 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001425 AddString(PPOpts.ImplicitPCHInclude, Record);
1426 AddString(PPOpts.ImplicitPTHInclude, Record);
1427 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1428 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1429
Douglas Gregora3b20262011-05-06 21:43:30 +00001430 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001431 SourceManager &SM = Context.getSourceManager();
1432 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1433 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001434 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1435 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001436 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1437 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1438
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001439 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001440 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001441 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001442 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001443 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001444
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001445 Record.clear();
1446 Record.push_back(SM.getMainFileID().getOpaqueValue());
1447 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1448
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001449 // Original PCH directory
1450 if (!OutputFile.empty() && OutputFile != "-") {
1451 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1452 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1454 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1455
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001456 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001457
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001458 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001459 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1460
1461 RecordData Record;
1462 Record.push_back(ORIGINAL_PCH_DIR);
1463 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1464 }
1465
Douglas Gregor49491f72013-03-15 22:15:07 +00001466 WriteInputFiles(Context.SourceMgr,
1467 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001468 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001469 Stream.ExitBlock();
1470}
1471
Douglas Gregor49491f72013-03-15 22:15:07 +00001472namespace {
1473 /// \brief An input file.
1474 struct InputFileEntry {
1475 const FileEntry *File;
1476 bool IsSystemFile;
1477 bool BufferOverridden;
1478 };
1479}
1480
1481void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1482 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001483 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001484 using namespace llvm;
1485 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1486 RecordData Record;
1487
1488 // Create input-file abbreviation.
1489 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1490 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001491 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001492 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1493 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001494 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001495 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1496 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1497
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001498 // Get all ContentCache objects for files, sorted by whether the file is a
1499 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001500 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001501 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1502 // Get this source location entry.
1503 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001504 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001505
1506 // We only care about file entries that were not overridden.
1507 if (!SLoc->isFile())
1508 continue;
1509 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001510 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001511 continue;
1512
Douglas Gregor49491f72013-03-15 22:15:07 +00001513 InputFileEntry Entry;
1514 Entry.File = Cache->OrigEntry;
1515 Entry.IsSystemFile = Cache->IsSystemFile;
1516 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001517 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001518 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001519 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001520 SortedFiles.push_front(Entry);
1521 }
1522
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001523 unsigned UserFilesNum = 0;
1524 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001525 std::vector<uint64_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001526 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001527 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001528 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001529
Douglas Gregor49491f72013-03-15 22:15:07 +00001530 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001531 if (InputFileID != 0)
1532 continue; // already recorded this file.
1533
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001534 // Record this entry's offset.
1535 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001536
1537 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001538
Douglas Gregor49491f72013-03-15 22:15:07 +00001539 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001540 ++UserFilesNum;
1541
Douglas Gregor72be3902012-10-19 00:38:02 +00001542 Record.clear();
1543 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001544 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001545
1546 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001547 Record.push_back(Entry.File->getSize());
Richard Smithe75ee0f2015-08-17 07:13:32 +00001548 Record.push_back(getTimestampForOutput(Entry.File));
Douglas Gregor72be3902012-10-19 00:38:02 +00001549
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001550 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001551 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001552
Richard Smith7ed1bc92014-12-05 22:42:13 +00001553 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1554 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001555
Douglas Gregor112b9072012-10-18 05:31:06 +00001556 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001557
1558 // Create input file offsets abbreviation.
1559 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1560 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1561 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001562 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1563 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001564 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1565 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1566
1567 // Write input file offsets.
1568 Record.clear();
1569 Record.push_back(INPUT_FILE_OFFSETS);
1570 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001571 Record.push_back(UserFilesNum);
Reid Kleckner012665e2015-05-21 00:13:09 +00001572 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001573}
1574
Douglas Gregora7f71a92009-04-10 03:52:48 +00001575//===----------------------------------------------------------------------===//
1576// Source Manager Serialization
1577//===----------------------------------------------------------------------===//
1578
1579/// \brief Create an abbreviation for the SLocEntry that refers to a
1580/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001581static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001582 using namespace llvm;
1583 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001584 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001589 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001592 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001594 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001595}
1596
1597/// \brief Create an abbreviation for the SLocEntry that refers to a
1598/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001599static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001600 using namespace llvm;
1601 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001602 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001608 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001609}
1610
1611/// \brief Create an abbreviation for the SLocEntry that refers to a
1612/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001613static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001614 using namespace llvm;
1615 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001616 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001618 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001619}
1620
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001621/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1622/// expansion.
1623static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001624 using namespace llvm;
1625 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001626 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001632 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001633}
1634
Douglas Gregor09b69892011-02-10 17:09:37 +00001635namespace {
1636 // Trait used for the on-disk hash table of header search information.
1637 class HeaderFileInfoTrait {
1638 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001639 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001640
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001641 // Keep track of the framework names we've used during serialization.
1642 SmallVector<char, 128> FrameworkStringData;
1643 llvm::StringMap<unsigned> FrameworkNameOffset;
1644
Douglas Gregor09b69892011-02-10 17:09:37 +00001645 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001646 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1647 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001648
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001649 struct key_type {
1650 const FileEntry *FE;
1651 const char *Filename;
1652 };
1653 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001654
1655 typedef HeaderFileInfo data_type;
1656 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001657 typedef unsigned hash_value_type;
1658 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001659
Richard Smithe75ee0f2015-08-17 07:13:32 +00001660 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001661 // The hash is based only on size/time of the file, so that the reader can
1662 // match even when symlinking or excess path elements ("foo/../", "../")
1663 // change the form of the name. However, complete path is still the key.
1664 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001665 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001666 }
1667
1668 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001669 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001670 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001671 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001672 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001673 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001674 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001675 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1676 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1677 DataLen += 4;
1678 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001679 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001680 }
1681
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001682 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001683 using namespace llvm::support;
1684 endian::Writer<little> LE(Out);
1685 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001686 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001687 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001688 KeyLen -= 8;
1689 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001690 }
1691
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001692 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001693 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001694 using namespace llvm::support;
1695 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001696 uint64_t Start = Out.tell(); (void)Start;
1697
Richard Smith386bb072015-08-18 23:42:23 +00001698 unsigned char Flags = (Data.isImport << 4)
1699 | (Data.isPragmaOnce << 3)
1700 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001701 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001702 LE.write<uint8_t>(Flags);
1703 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001704
1705 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001706 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001707 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001708 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001709
1710 unsigned Offset = 0;
1711 if (!Data.Framework.empty()) {
1712 // If this header refers into a framework, save the framework name.
1713 llvm::StringMap<unsigned>::iterator Pos
1714 = FrameworkNameOffset.find(Data.Framework);
1715 if (Pos == FrameworkNameOffset.end()) {
1716 Offset = FrameworkStringData.size() + 1;
1717 FrameworkStringData.append(Data.Framework.begin(),
1718 Data.Framework.end());
1719 FrameworkStringData.push_back(0);
1720
1721 FrameworkNameOffset[Data.Framework] = Offset;
1722 } else
1723 Offset = Pos->second;
1724 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001725 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001726
Richard Smith386bb072015-08-18 23:42:23 +00001727 // FIXME: If the header is excluded, we should write out some
1728 // record of that fact.
1729 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1730 if (uint32_t ModID =
1731 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1732 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1733 assert((Value >> 2) == ModID && "overflow in header module info");
1734 LE.write<uint32_t>(Value);
1735 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001736 }
1737
Douglas Gregor09b69892011-02-10 17:09:37 +00001738 assert(Out.tell() - Start == DataLen && "Wrong data length");
1739 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001740
1741 const char *strings_begin() const { return FrameworkStringData.begin(); }
1742 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001743 };
1744} // end anonymous namespace
1745
1746/// \brief Write the header search block for the list of files that
1747///
1748/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001749void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001750 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001751 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1752
1753 if (FilesByUID.size() > HS.header_file_size())
1754 FilesByUID.resize(HS.header_file_size());
1755
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001756 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001757 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001758 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001759 unsigned NumHeaderSearchEntries = 0;
1760 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1761 const FileEntry *File = FilesByUID[UID];
1762 if (!File)
1763 continue;
1764
Richard Smith386bb072015-08-18 23:42:23 +00001765 // Get the file info. This will load info from the external source if
1766 // necessary. Skip emitting this file if we have no information on it
1767 // as a header file (in which case HFI will be null) or if it hasn't
1768 // changed since it was loaded. Also skip it if it's for a modular header
1769 // from a different module; in that case, we rely on the module(s)
1770 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001771 const HeaderFileInfo *HFI =
1772 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1773 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001774 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001775
Richard Smith7ed1bc92014-12-05 22:42:13 +00001776 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001777 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001778 SmallString<128> FilenameTmp(Filename);
1779 if (PreparePathForOutput(FilenameTmp)) {
1780 // If we performed any translation on the file name at all, we need to
1781 // save this string, since the generator will refer to it later.
1782 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001783 SavedStrings.push_back(Filename);
1784 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001785
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001786 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001787 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001788 ++NumHeaderSearchEntries;
1789 }
1790
1791 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001792 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001793 uint32_t BucketOffset;
1794 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001795 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001796 llvm::raw_svector_ostream Out(TableData);
1797 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001798 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001799 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1800 }
1801
1802 // Create a blob abbreviation
1803 using namespace llvm;
1804 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1805 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1810 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1811
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001812 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001813 RecordData Record;
1814 Record.push_back(HEADER_SEARCH_TABLE);
1815 Record.push_back(BucketOffset);
1816 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001817 Record.push_back(TableData.size());
1818 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001819 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001820
1821 // Free all of the strings we had to duplicate.
1822 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001823 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001824}
1825
Douglas Gregora7f71a92009-04-10 03:52:48 +00001826/// \brief Writes the block containing the serialized form of the
1827/// source manager.
1828///
1829/// TODO: We should probably use an on-disk hash table (stored in a
1830/// blob), indexed based on the file name, so that we only create
1831/// entries for files that we actually need. In the common case (no
1832/// errors), we probably won't have to create file entries for any of
1833/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001834void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001835 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001836 RecordData Record;
1837
Chris Lattner0910e3b2009-04-10 17:16:57 +00001838 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001839 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001840
1841 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001842 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1843 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1844 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001845 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001846
Douglas Gregor258ae542009-04-27 06:38:32 +00001847 // Write out the source location entry table. We skip the first
1848 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001849 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001850 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001851 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1852 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001853 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001854 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001855 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001856 FileID FID = FileID::get(I);
1857 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001858
Douglas Gregor258ae542009-04-27 06:38:32 +00001859 // Record the offset of this source-location entry.
1860 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1861
1862 // Figure out which record code to use.
1863 unsigned Code;
1864 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001865 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1866 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001867 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001868 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001869 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001870 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001871 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001872 Record.clear();
1873 Record.push_back(Code);
1874
Douglas Gregor925296b2011-07-19 16:10:42 +00001875 // Starting offset of this entry within this module, so skip the dummy.
1876 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001877 if (SLoc->isFile()) {
1878 const SrcMgr::FileInfo &File = SLoc->getFile();
1879 Record.push_back(File.getIncludeLoc().getRawEncoding());
1880 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1881 Record.push_back(File.hasLineDirectives());
1882
1883 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001884 if (Content->OrigEntry) {
1885 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001886 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001887
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001888 // The source location entry is a file. Emit input file ID.
1889 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1890 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001891
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001892 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001893
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001894 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001895 if (FDI != FileDeclIDs.end()) {
1896 Record.push_back(FDI->second->FirstDeclIndex);
1897 Record.push_back(FDI->second->DeclIDs.size());
1898 } else {
1899 Record.push_back(0);
1900 Record.push_back(0);
1901 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001902
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001903 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001904
1905 if (Content->BufferOverridden) {
1906 Record.clear();
1907 Record.push_back(SM_SLOC_BUFFER_BLOB);
1908 const llvm::MemoryBuffer *Buffer
1909 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1910 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1911 StringRef(Buffer->getBufferStart(),
1912 Buffer->getBufferSize() + 1));
1913 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001914 } else {
1915 // The source location entry is a buffer. The blob associated
1916 // with this entry contains the contents of the buffer.
1917
1918 // We add one to the size so that we capture the trailing NULL
1919 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1920 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001921 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001922 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001923 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001924 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001925 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001926 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001927 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001928 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001929 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001930 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001931
Douglas Gregor925296b2011-07-19 16:10:42 +00001932 if (strcmp(Name, "<built-in>") == 0) {
1933 PreloadSLocs.push_back(SLocEntryOffsets.size());
1934 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001935 }
1936 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001937 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001938 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001939 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1940 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001941 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1942 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001943
1944 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001945 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001946 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001947 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001948 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001949 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001950 }
1951 }
1952
Douglas Gregor8f45df52009-04-16 22:23:12 +00001953 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001954
1955 if (SLocEntryOffsets.empty())
1956 return;
1957
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001958 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001959 // table is used for lazily loading source-location information.
1960 using namespace llvm;
1961 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001962 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001965 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1966 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001967
Douglas Gregor258ae542009-04-27 06:38:32 +00001968 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001969 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001970 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001971 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Reid Kleckner012665e2015-05-21 00:13:09 +00001972 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, bytes(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001973
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001974 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001975 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001976 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001977
1978 // Write the line table. It depends on remapping working, so it must come
1979 // after the source location offsets.
1980 if (SourceMgr.hasLineTable()) {
1981 LineTableInfo &LineTable = SourceMgr.getLineTable();
1982
1983 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00001984
1985 // Emit the needed file names.
1986 llvm::DenseMap<int, int> FilenameMap;
1987 for (const auto &L : LineTable) {
1988 if (L.first.ID < 0)
1989 continue;
1990 for (auto &LE : L.second) {
1991 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
1992 FilenameMap.size())).second)
1993 AddPath(LineTable.getFilename(LE.FilenameID), Record);
1994 }
1995 }
1996 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00001997
1998 // Emit the line entries
1999 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
2000 L != LEnd; ++L) {
2001 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00002002 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002003 continue;
2004
2005 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00002006 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002007
2008 // Emit the line entries
2009 Record.push_back(L->second.size());
2010 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
2011 LEEnd = L->second.end();
2012 LE != LEEnd; ++LE) {
2013 Record.push_back(LE->FileOffset);
2014 Record.push_back(LE->LineNo);
Richard Smith63078492015-09-01 07:41:55 +00002015 Record.push_back(FilenameMap[LE->FilenameID]);
Douglas Gregor925296b2011-07-19 16:10:42 +00002016 Record.push_back((unsigned)LE->FileKind);
2017 Record.push_back(LE->IncludeOffset);
2018 }
2019 }
Richard Smith63078492015-09-01 07:41:55 +00002020
Douglas Gregor925296b2011-07-19 16:10:42 +00002021 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2022 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002023}
2024
Douglas Gregorc5046832009-04-27 18:38:38 +00002025//===----------------------------------------------------------------------===//
2026// Preprocessor Serialization
2027//===----------------------------------------------------------------------===//
2028
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002029static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2030 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002031 if (MacroInfo *MI = MD->getMacroInfo())
2032 if (MI->isBuiltinMacro())
2033 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002034
2035 if (IsModule) {
2036 SourceLocation Loc = MD->getLocation();
2037 if (Loc.isInvalid())
2038 return true;
2039 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2040 return true;
2041 }
2042
2043 return false;
2044}
2045
Chris Lattnereeffaef2009-04-10 17:15:23 +00002046/// \brief Writes the block containing the serialized form of the
2047/// preprocessor.
2048///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002049void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002050 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2051 if (PPRec)
2052 WritePreprocessorDetail(*PPRec);
2053
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002054 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002055 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002056
Chris Lattner0af3ba12009-04-13 01:29:17 +00002057 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2058 if (PP.getCounterValue() != 0) {
2059 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002060 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002061 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002062 }
2063
2064 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002065 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002066
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002067 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002068 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002069 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002070 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002071
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002072 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002073 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002074
Richard Smithee977932015-05-01 21:22:17 +00002075 // Construct the list of identifiers with macro directives that need to be
2076 // serialized.
2077 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2078 for (auto &Id : PP.getIdentifierTable())
2079 if (Id.second->hadMacroDefinition() &&
2080 (!Id.second->isFromAST() ||
2081 Id.second->hasChangedSinceDeserialization()))
2082 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002083 // Sort the set of macro definitions that need to be serialized by the
2084 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002085 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2086 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002087
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002088 // Emit the macro directives as a list and associate the offset with the
2089 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002090 for (const IdentifierInfo *Name : MacroIdentifiers) {
2091 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002092 auto StartOffset = Stream.GetCurrentBitNo();
2093
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002094 // Emit the macro directives in reverse source order.
2095 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002096 // Once we hit an ignored macro, we're done: the rest of the chain
2097 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002098 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002099 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002100
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002101 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002102 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002103 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002104 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002105 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002106 Record.push_back(VisMD->isPublic());
2107 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002108 }
Richard Smithd7329392015-04-21 21:46:32 +00002109
Richard Smithb8b2ed62015-04-23 18:18:26 +00002110 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002111 bool EmittedModuleMacros = false;
Richard Smithb8b2ed62015-04-23 18:18:26 +00002112 if (IsModule) {
2113 auto Leafs = PP.getLeafModuleMacros(Name);
2114 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2115 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2116 while (!Worklist.empty()) {
2117 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002118
Richard Smithb8b2ed62015-04-23 18:18:26 +00002119 // Emit a record indicating this submodule exports this macro.
2120 ModuleMacroRecord.push_back(
2121 getSubmoduleID(Macro->getOwningModule()));
Richard Smithee977932015-05-01 21:22:17 +00002122 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
Richard Smithb8b2ed62015-04-23 18:18:26 +00002123 for (auto *M : Macro->overrides())
2124 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002125
Richard Smithb8b2ed62015-04-23 18:18:26 +00002126 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2127 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002128
Richard Smithb8b2ed62015-04-23 18:18:26 +00002129 // Enqueue overridden macros once we've visited all their ancestors.
2130 for (auto *M : Macro->overrides())
2131 if (++Visits[M] == M->getNumOverridingMacros())
2132 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002133
2134 EmittedModuleMacros = true;
Richard Smithd7329392015-04-21 21:46:32 +00002135 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002136 }
Richard Smithd7329392015-04-21 21:46:32 +00002137
Richard Smithee977932015-05-01 21:22:17 +00002138 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002139 continue;
2140
Richard Smithd7329392015-04-21 21:46:32 +00002141 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002142 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2143 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002144 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002145
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002146 /// \brief Offsets of each of the macros into the bitstream, indexed by
2147 /// the local macro ID
2148 ///
2149 /// For each identifier that is associated with a macro, this map
2150 /// provides the offset into the bitstream where that macro is
2151 /// defined.
2152 std::vector<uint32_t> MacroOffsets;
2153
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002154 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2155 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2156 MacroInfo *MI = MacroInfosToEmit[I].MI;
2157 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002158
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002159 if (ID < FirstMacroID) {
2160 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2161 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002162 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002163
2164 // Record the local offset of this macro.
2165 unsigned Index = ID - FirstMacroID;
2166 if (Index == MacroOffsets.size())
2167 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2168 else {
2169 if (Index > MacroOffsets.size())
2170 MacroOffsets.resize(Index + 1);
2171
2172 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2173 }
2174
2175 AddIdentifierRef(Name, Record);
2176 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2177 AddSourceLocation(MI->getDefinitionLoc(), Record);
2178 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2179 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002180 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002181 unsigned Code;
2182 if (MI->isObjectLike()) {
2183 Code = PP_MACRO_OBJECT_LIKE;
2184 } else {
2185 Code = PP_MACRO_FUNCTION_LIKE;
2186
2187 Record.push_back(MI->isC99Varargs());
2188 Record.push_back(MI->isGNUVarargs());
2189 Record.push_back(MI->hasCommaPasting());
2190 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002191 for (const IdentifierInfo *Arg : MI->args())
2192 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002193 }
2194
2195 // If we have a detailed preprocessing record, record the macro definition
2196 // ID that corresponds to this macro.
2197 if (PPRec)
2198 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2199
2200 Stream.EmitRecord(Code, Record);
2201 Record.clear();
2202
2203 // Emit the tokens array.
2204 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2205 // Note that we know that the preprocessor does not have any annotation
2206 // tokens in it because they are created by the parser, and thus can't
2207 // be in a macro definition.
2208 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002209 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002210 Stream.EmitRecord(PP_TOKEN, Record);
2211 Record.clear();
2212 }
2213 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002214 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002215
Douglas Gregor92a96f52011-02-08 21:58:10 +00002216 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002217
2218 // Write the offsets table for macro IDs.
2219 using namespace llvm;
Richard Smith13090a22015-04-10 02:02:24 +00002220 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002221 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2225
2226 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2227 Record.clear();
2228 Record.push_back(MACRO_OFFSET);
2229 Record.push_back(MacroOffsets.size());
2230 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2231 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002232 bytes(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002233}
2234
2235void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002236 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002237 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002238
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002239 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002240
Douglas Gregor92a96f52011-02-08 21:58:10 +00002241 // Enter the preprocessor block.
2242 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002243
Douglas Gregoraae92242010-03-19 21:51:54 +00002244 // If the preprocessor has a preprocessing record, emit it.
2245 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002246 using namespace llvm;
2247
2248 // Set up the abbreviation for
2249 unsigned InclusionAbbrev = 0;
2250 {
2251 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2252 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2258 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2259 }
2260
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002261 unsigned FirstPreprocessorEntityID
2262 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2263 + NUM_PREDEF_PP_ENTITY_IDS;
2264 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002265 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002266 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2267 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002268 E != EEnd;
2269 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002270 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002271
Richard Smith66a81862015-05-04 02:25:31 +00002272 PreprocessedEntityOffsets.push_back(
2273 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002274
Richard Smith66a81862015-05-04 02:25:31 +00002275 if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002276 // Record this macro definition's ID.
2277 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002278
Douglas Gregor92a96f52011-02-08 21:58:10 +00002279 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002280 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2281 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002282 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002283
Chandler Carrutha88a22182011-07-14 08:20:46 +00002284 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002285 Record.push_back(ME->isBuiltinMacro());
2286 if (ME->isBuiltinMacro())
2287 AddIdentifierRef(ME->getName(), Record);
2288 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002289 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002290 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002291 continue;
2292 }
2293
2294 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2295 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002296 Record.push_back(ID->getFileName().size());
2297 Record.push_back(ID->wasInQuotes());
2298 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002299 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002300 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002301 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002302 // Check that the FileEntry is not null because it was not resolved and
2303 // we create a PCH even with compiler errors.
2304 if (ID->getFile())
2305 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002306 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2307 continue;
2308 }
2309
2310 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2311 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002312 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002313
Douglas Gregoraae92242010-03-19 21:51:54 +00002314 // Write the offsets table for the preprocessing record.
2315 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002316 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2317
Douglas Gregoraae92242010-03-19 21:51:54 +00002318 // Write the offsets table for identifier IDs.
2319 using namespace llvm;
2320 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002321 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002324 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002325
Douglas Gregoraae92242010-03-19 21:51:54 +00002326 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002327 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002328 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002329 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002330 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002331 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002332}
2333
Richard Smith386bb072015-08-18 23:42:23 +00002334unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002335 if (!Mod)
2336 return 0;
2337
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002338 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2339 if (Known != SubmoduleIDs.end())
2340 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002341
2342 if (Mod->getTopLevelModule() != WritingModule)
2343 return 0;
2344
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002345 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2346}
2347
Richard Smith386bb072015-08-18 23:42:23 +00002348unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2349 // FIXME: This can easily happen, if we have a reference to a submodule that
2350 // did not result in us loading a module file for that submodule. For
2351 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2352 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2353 assert((ID || !Mod) &&
2354 "asked for module ID for non-local, non-imported module");
2355 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002356}
2357
Douglas Gregor253eefe2011-12-01 00:59:36 +00002358/// \brief Compute the number of modules within the given tree (including the
2359/// given module).
2360static unsigned getNumberOfModules(Module *Mod) {
2361 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002362 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2363 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002364 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002365 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002366
2367 return ChildModules + 1;
2368}
2369
Douglas Gregorde3ef502011-11-30 23:21:26 +00002370void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002371 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002372 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002373
2374 // Write the abbreviations needed for the submodules block.
2375 using namespace llvm;
2376 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2377 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2389 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2390
2391 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002392 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2394 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2395
2396 Abbrev = new BitCodeAbbrev();
2397 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2399 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002400
2401 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002402 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2404 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2405
2406 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002407 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2409 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2410
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002411 Abbrev = new BitCodeAbbrev();
2412 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002415 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2416
Douglas Gregor59527662012-10-15 06:28:11 +00002417 Abbrev = new BitCodeAbbrev();
2418 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2420 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2421
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002422 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002423 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2425 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2426
2427 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002428 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2430 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2431
2432 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002433 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2434 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2435 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2436
2437 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002438 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2440 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2441 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2442
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002443 Abbrev = new BitCodeAbbrev();
2444 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2446 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2447
Douglas Gregorfb912652013-03-20 21:10:35 +00002448 Abbrev = new BitCodeAbbrev();
2449 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2450 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2452 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2453
Douglas Gregor253eefe2011-12-01 00:59:36 +00002454 // Write the submodule metadata block.
2455 RecordData Record;
2456 Record.push_back(getNumberOfModules(WritingModule));
2457 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2458 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2459
Douglas Gregor69021972011-11-30 17:33:56 +00002460 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002461 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002462 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002463 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002464 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002465 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002466 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002467
2468 // Emit the definition of the block.
2469 Record.clear();
2470 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002471 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002472 if (Mod->Parent) {
2473 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2474 Record.push_back(SubmoduleIDs[Mod->Parent]);
2475 } else {
2476 Record.push_back(0);
2477 }
2478 Record.push_back(Mod->IsFramework);
2479 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002480 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002481 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002482 Record.push_back(Mod->InferSubmodules);
2483 Record.push_back(Mod->InferExplicitSubmodules);
2484 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002485 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002486 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2487
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002488 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002489 for (const auto &R : Mod->Requirements) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002490 Record.clear();
2491 Record.push_back(SUBMODULE_REQUIRES);
Richard Smith080056b2015-08-18 21:53:42 +00002492 Record.push_back(R.second);
2493 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002494 }
2495
Douglas Gregor69021972011-11-30 17:33:56 +00002496 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002497 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002498 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002499 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Richard Smith2b63d152015-05-16 02:28:53 +00002500 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2501 UmbrellaHeader.NameAsWritten);
2502 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Douglas Gregor524e33e2011-12-08 19:11:24 +00002503 Record.clear();
2504 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2505 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002506 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002507 }
Richard Smith306d8922014-10-22 23:50:56 +00002508
Douglas Gregor69021972011-11-30 17:33:56 +00002509 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002510 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002511 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002512 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002513 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002514 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002515 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2516 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2517 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002518 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002519 Module::HK_PrivateTextual},
2520 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002521 };
2522 for (auto &HL : HeaderLists) {
Douglas Gregor69021972011-11-30 17:33:56 +00002523 Record.clear();
Richard Smith3c1a41a2014-12-02 00:08:08 +00002524 Record.push_back(HL.RecordKind);
2525 for (auto &H : Mod->Headers[HL.HeaderKind])
2526 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2527 }
2528
2529 // Emit the top headers.
2530 {
2531 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2532 Record.clear();
2533 Record.push_back(SUBMODULE_TOPHEADER);
2534 for (auto *H : TopHeaders)
2535 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002536 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002537
2538 // Emit the imports.
2539 if (!Mod->Imports.empty()) {
2540 Record.clear();
Richard Smith080056b2015-08-18 21:53:42 +00002541 for (auto *I : Mod->Imports)
2542 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002543 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2544 }
2545
Douglas Gregor24bb9232011-12-02 18:58:38 +00002546 // Emit the exports.
2547 if (!Mod->Exports.empty()) {
2548 Record.clear();
Richard Smith080056b2015-08-18 21:53:42 +00002549 for (const auto &E : Mod->Exports) {
2550 // FIXME: This may fail; we don't require that all exported modules
2551 // are local or imported.
2552 Record.push_back(getSubmoduleID(E.getPointer()));
2553 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002554 }
2555 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2556 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002557
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002558 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2559 // Might be unnecessary as use declarations are only used to build the
2560 // module itself.
2561
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002562 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002563 for (const auto &LL : Mod->LinkLibraries) {
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002564 Record.clear();
2565 Record.push_back(SUBMODULE_LINK_LIBRARY);
Richard Smith080056b2015-08-18 21:53:42 +00002566 Record.push_back(LL.IsFramework);
2567 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002568 }
2569
Douglas Gregorfb912652013-03-20 21:10:35 +00002570 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002571 for (const auto &C : Mod->Conflicts) {
Douglas Gregorfb912652013-03-20 21:10:35 +00002572 Record.clear();
2573 Record.push_back(SUBMODULE_CONFLICT);
Richard Smith080056b2015-08-18 21:53:42 +00002574 // FIXME: This may fail; we don't require that all conflicting modules
2575 // are local or imported.
2576 Record.push_back(getSubmoduleID(C.Other));
2577 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002578 }
2579
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002580 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002581 for (const auto &CM : Mod->ConfigMacros) {
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002582 Record.clear();
2583 Record.push_back(SUBMODULE_CONFIG_MACRO);
Richard Smith080056b2015-08-18 21:53:42 +00002584 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002585 }
2586
Douglas Gregor69021972011-11-30 17:33:56 +00002587 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002588 for (auto *M : Mod->submodules())
2589 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002590 }
2591
2592 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002593
Richard Smith23d8d032015-05-14 00:45:20 +00002594 assert((NextSubmoduleID - FirstSubmoduleID ==
2595 getNumberOfModules(WritingModule)) &&
2596 "Wrong # of submodules; found a reference to a non-local, "
2597 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002598}
2599
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002600serialization::SubmoduleID
2601ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002602 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002603 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002604
2605 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002606 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002607 Module *OwningMod
2608 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002609 if (!OwningMod)
2610 return 0;
2611
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002612 // Check whether this submodule is part of our own module.
2613 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002614 return 0;
2615
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002616 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002617}
2618
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002619void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2620 bool isModule) {
2621 // Make sure set diagnostic pragmas don't affect the translation unit that
2622 // imports the module.
2623 // FIXME: Make diagnostic pragma sections work properly with modules.
2624 if (isModule)
2625 return;
2626
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002627 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2628 DiagStateIDMap;
2629 unsigned CurrID = 0;
2630 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002631 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002632 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002633 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2634 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002635 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002636 if (point.Loc.isInvalid())
2637 continue;
2638
2639 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002640 unsigned &DiagStateID = DiagStateIDMap[point.State];
2641 Record.push_back(DiagStateID);
2642
2643 if (DiagStateID == 0) {
2644 DiagStateID = ++CurrID;
2645 for (DiagnosticsEngine::DiagState::const_iterator
2646 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2647 if (I->second.isPragma()) {
2648 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002649 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002650 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002651 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002652 Record.push_back(-1); // mark the end of the diag/map pairs for this
2653 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002654 }
2655 }
2656
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002657 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002658 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002659}
2660
Richard Smithc2bb8182015-03-24 06:36:48 +00002661void ASTWriter::WriteCXXCtorInitializersOffsets() {
2662 if (CXXCtorInitializersOffsets.empty())
2663 return;
2664
2665 RecordData Record;
2666
2667 // Create a blob abbreviation for the C++ ctor initializer offsets.
2668 using namespace llvm;
2669
2670 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2671 Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS));
2672 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2673 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2674 unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2675
2676 // Write the base specifier offsets table.
2677 Record.clear();
2678 Record.push_back(CXX_CTOR_INITIALIZERS_OFFSETS);
2679 Record.push_back(CXXCtorInitializersOffsets.size());
2680 Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002681 bytes(CXXCtorInitializersOffsets));
Richard Smithc2bb8182015-03-24 06:36:48 +00002682}
2683
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002684void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2685 if (CXXBaseSpecifiersOffsets.empty())
2686 return;
2687
2688 RecordData Record;
2689
2690 // Create a blob abbreviation for the C++ base specifiers offsets.
2691 using namespace llvm;
2692
2693 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2694 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2695 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2696 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2697 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2698
Douglas Gregorc27b2872011-08-04 00:01:48 +00002699 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002700 Record.clear();
2701 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2702 Record.push_back(CXXBaseSpecifiersOffsets.size());
2703 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002704 bytes(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002705}
2706
Douglas Gregorc5046832009-04-27 18:38:38 +00002707//===----------------------------------------------------------------------===//
2708// Type Serialization
2709//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002710
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002711/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002712void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002713 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002714 if (Idx.getIndex() == 0) // we haven't seen this type before.
2715 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002716
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002717 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002718
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002719 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002720 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002721 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002722 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002723 else if (TypeOffsets.size() < Index) {
2724 TypeOffsets.resize(Index + 1);
2725 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002726 }
2727
2728 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002729
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002730 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002731 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002732 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002733
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002734 if (T.hasLocalNonFastQualifiers()) {
2735 Qualifiers Qs = T.getLocalQualifiers();
2736 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002737 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002738 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002739 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002740 } else {
2741 switch (T->getTypeClass()) {
2742 // For all of the concrete, non-dependent types, call the
2743 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002744#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002745 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002746#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002747#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002748 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002749 }
2750
2751 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002752 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002753
2754 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002755 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002756}
2757
Douglas Gregorc5046832009-04-27 18:38:38 +00002758//===----------------------------------------------------------------------===//
2759// Declaration Serialization
2760//===----------------------------------------------------------------------===//
2761
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002762/// \brief Write the block containing all of the declaration IDs
2763/// lexically declared within the given DeclContext.
2764///
2765/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2766/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002767uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002768 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002769 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002770 return 0;
2771
Douglas Gregor8f45df52009-04-16 22:23:12 +00002772 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002773 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002774 Record.push_back(DECL_CONTEXT_LEXICAL);
Richard Smith82f8fcd2015-08-06 22:07:25 +00002775 SmallVector<uint32_t, 128> KindDeclPairs;
2776 for (const auto *D : DC->decls()) {
2777 KindDeclPairs.push_back(D->getKind());
2778 KindDeclPairs.push_back(GetDeclRef(D));
2779 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002780
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002781 ++NumLexicalDeclContexts;
Richard Smith82f8fcd2015-08-06 22:07:25 +00002782 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2783 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002784 return Offset;
2785}
2786
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002787void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002788 using namespace llvm;
2789 RecordData Record;
2790
2791 // Write the type offsets array
2792 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002793 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2797 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2798 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002799 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002800 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002801 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Reid Kleckner012665e2015-05-21 00:13:09 +00002802 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002803
2804 // Write the declaration offsets array
2805 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002806 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2810 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2811 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002812 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002813 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002814 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Reid Kleckner012665e2015-05-21 00:13:09 +00002815 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002816}
2817
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002818void ASTWriter::WriteFileDeclIDsMap() {
2819 using namespace llvm;
2820 RecordData Record;
2821
Chandler Carruthb306d732015-03-27 00:31:20 +00002822 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2823 FileDeclIDs.begin(), FileDeclIDs.end());
2824 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2825 llvm::less_first());
2826
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002827 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002828 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2829 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2830 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2831 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2832 for (auto &LocDeclEntry : Info.DeclIDs)
2833 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002834 }
2835
2836 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2837 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2840 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2841 Record.push_back(FILE_SORTED_DECLS);
Chandler Carruthb306d732015-03-27 00:31:20 +00002842 Record.push_back(FileGroupedDeclIDs.size());
Reid Kleckner012665e2015-05-21 00:13:09 +00002843 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002844}
2845
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002846void ASTWriter::WriteComments() {
2847 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002848 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002849 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002850 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2851 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002852 I != E; ++I) {
2853 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002854 AddSourceRange((*I)->getSourceRange(), Record);
2855 Record.push_back((*I)->getKind());
2856 Record.push_back((*I)->isTrailingComment());
2857 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002858 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2859 }
2860 Stream.ExitBlock();
2861}
2862
Douglas Gregorc5046832009-04-27 18:38:38 +00002863//===----------------------------------------------------------------------===//
2864// Global Method Pool and Selector Serialization
2865//===----------------------------------------------------------------------===//
2866
Douglas Gregore84a9da2009-04-20 20:36:09 +00002867namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002868// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002869class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002870 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002871
2872public:
2873 typedef Selector key_type;
2874 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002875
Sebastian Redl834bb972010-08-04 17:20:04 +00002876 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002877 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002878 ObjCMethodList Instance, Factory;
2879 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002880 typedef const data_type& data_type_ref;
2881
Justin Bogner25463f12014-04-18 20:27:24 +00002882 typedef unsigned hash_value_type;
2883 typedef unsigned offset_type;
2884
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002885 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002886
Justin Bogner25463f12014-04-18 20:27:24 +00002887 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002888 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002889 }
Mike Stump11289f42009-09-09 15:08:12 +00002890
2891 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002892 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002893 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002894 using namespace llvm::support;
2895 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002896 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002897 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002898 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2899 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002900 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002901 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002902 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002903 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002904 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002905 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002906 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002907 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002908 return std::make_pair(KeyLen, DataLen);
2909 }
Mike Stump11289f42009-09-09 15:08:12 +00002910
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002911 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002912 using namespace llvm::support;
2913 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002914 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002915 assert((Start >> 32) == 0 && "Selector key offset too large");
2916 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002917 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002918 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002919 if (N == 0)
2920 N = 1;
2921 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002922 LE.write<uint32_t>(
2923 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002924 }
Mike Stump11289f42009-09-09 15:08:12 +00002925
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002926 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002927 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002928 using namespace llvm::support;
2929 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002930 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002931 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002932 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002933 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002934 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002935 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002936 ++NumInstanceMethods;
2937
2938 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002939 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002940 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002941 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002942 ++NumFactoryMethods;
2943
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002944 unsigned InstanceBits = Methods.Instance.getBits();
2945 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002946 unsigned InstanceHasMoreThanOneDeclBit =
2947 Methods.Instance.hasMoreThanOneDecl();
2948 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2949 (InstanceHasMoreThanOneDeclBit << 2) |
2950 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002951 unsigned FactoryBits = Methods.Factory.getBits();
2952 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002953 unsigned FactoryHasMoreThanOneDeclBit =
2954 Methods.Factory.hasMoreThanOneDecl();
2955 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2956 (FactoryHasMoreThanOneDeclBit << 2) |
2957 FactoryBits;
2958 LE.write<uint16_t>(FullInstanceBits);
2959 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002960 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002961 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002962 if (Method->getMethod())
2963 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00002964 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002965 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002966 if (Method->getMethod())
2967 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002968
2969 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002970 }
2971};
2972} // end anonymous namespace
2973
Sebastian Redla19a67f2010-08-03 21:58:15 +00002974/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002975///
2976/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002977/// in an on-disk hash table indexed by the selector. The hash table also
2978/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002979void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002980 using namespace llvm;
2981
Sebastian Redla19a67f2010-08-03 21:58:15 +00002982 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002983 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002984 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002985 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002986 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002987 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002988 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002989 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002990
Sebastian Redla19a67f2010-08-03 21:58:15 +00002991 // Create the on-disk hash table representation. We walk through every
2992 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002993 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002994 for (auto &SelectorAndID : SelectorIDs) {
2995 Selector S = SelectorAndID.first;
2996 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002997 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002998 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002999 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003000 ObjCMethodList(),
3001 ObjCMethodList()
3002 };
3003 if (F != SemaRef.MethodPool.end()) {
3004 Data.Instance = F->second.first;
3005 Data.Factory = F->second.second;
3006 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003007 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003008 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003009 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003010 // Selector already exists. Did it change?
3011 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003012 for (ObjCMethodList *M = &Data.Instance;
3013 !changed && M && M->getMethod(); M = M->getNext()) {
3014 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003015 changed = true;
3016 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003017 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003018 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003019 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003020 changed = true;
3021 }
3022 if (!changed)
3023 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003024 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003025 // A new method pool entry.
3026 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003027 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003028 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003029 }
3030
Douglas Gregorc78d3462009-04-24 21:10:55 +00003031 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003032 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003033 uint32_t BucketOffset;
3034 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003035 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003036 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003037 llvm::raw_svector_ostream Out(MethodPool);
3038 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003039 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003040 BucketOffset = Generator.Emit(Out, Trait);
3041 }
3042
3043 // Create a blob abbreviation
3044 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003045 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3049 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3050
Douglas Gregor95c13f52009-04-25 17:48:32 +00003051 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003052 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003053 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003054 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003055 Record.push_back(NumTableEntries);
Yaron Keren92e1b622015-03-18 10:17:07 +00003056 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003057
3058 // Create a blob abbreviation for the selector table offsets.
3059 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003060 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3064 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3065
3066 // Write the selector offsets table.
3067 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003068 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003069 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003070 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003071 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003072 bytes(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003073 }
3074}
3075
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003076/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003077void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003078 using namespace llvm;
3079 if (SemaRef.ReferencedSelectors.empty())
3080 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003081
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003082 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003083
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003084 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003085 // very tricky to fix, and given that @selector shouldn't really appear in
3086 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003087 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3088 Selector Sel = SelectorAndLocation.first;
3089 SourceLocation Loc = SelectorAndLocation.second;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003090 AddSelectorRef(Sel, Record);
3091 AddSourceLocation(Loc, Record);
3092 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003093 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003094}
3095
Douglas Gregorc5046832009-04-27 18:38:38 +00003096//===----------------------------------------------------------------------===//
3097// Identifier Table Serialization
3098//===----------------------------------------------------------------------===//
3099
Richard Smithb3761912015-02-05 23:08:52 +00003100/// Determine the declaration that should be put into the name lookup table to
3101/// represent the given declaration in this module. This is usually D itself,
3102/// but if D was imported and merged into a local declaration, we want the most
3103/// recent local declaration instead. The chosen declaration will be the most
3104/// recent declaration in any module that imports this one.
3105static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3106 NamedDecl *D) {
3107 if (!LangOpts.Modules || !D->isFromASTFile())
3108 return D;
3109
3110 if (Decl *Redecl = D->getPreviousDecl()) {
3111 // For Redeclarable decls, a prior declaration might be local.
3112 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3113 if (!Redecl->isFromASTFile())
3114 return cast<NamedDecl>(Redecl);
Richard Smithfe620d22015-03-05 23:24:12 +00003115 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003116 // local one.
3117 if (D->getOwningModuleID() == 0)
3118 break;
3119 }
3120 } else if (Decl *First = D->getCanonicalDecl()) {
3121 // For Mergeable decls, the first decl might be local.
3122 if (!First->isFromASTFile())
3123 return cast<NamedDecl>(First);
3124 }
3125
3126 // All declarations are imported. Our most recent declaration will also be
3127 // the most recent one in anyone who imports us.
3128 return D;
3129}
3130
Douglas Gregorc78d3462009-04-24 21:10:55 +00003131namespace {
Richard Smithcf97cf62015-04-19 01:34:23 +00003132class ASTIdentifierTableTrait {
3133 ASTWriter &Writer;
3134 Preprocessor &PP;
3135 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003136 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003137 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003138 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003139
3140 /// \brief Determines whether this is an "interesting" identifier that needs a
3141 /// full IdentifierInfo structure written into the hash table. Notably, this
3142 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3143 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003144 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003145 if (MacroOffset ||
3146 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003147 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003148 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003149 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003150 return true;
3151
3152 return false;
3153 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003154
Douglas Gregore84a9da2009-04-20 20:36:09 +00003155public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003156 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003157 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003158
Sebastian Redl539c5062010-08-18 23:57:32 +00003159 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003160 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003161
Justin Bogner25463f12014-04-18 20:27:24 +00003162 typedef unsigned hash_value_type;
3163 typedef unsigned offset_type;
3164
Richard Smithd7329392015-04-21 21:46:32 +00003165 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003166 IdentifierResolver &IdResolver, bool IsModule,
3167 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003168 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003169 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3170 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003171
Justin Bogner25463f12014-04-18 20:27:24 +00003172 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003173 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003174 }
Mike Stump11289f42009-09-09 15:08:12 +00003175
Richard Smith33e0f7e2015-07-22 02:08:40 +00003176 bool isInterestingIdentifier(const IdentifierInfo *II) {
3177 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3178 return isInterestingIdentifier(II, MacroOffset);
3179 }
Richard Smith9c254182015-07-19 21:41:12 +00003180 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3181 return isInterestingIdentifier(II, 0);
3182 }
3183
Mike Stump11289f42009-09-09 15:08:12 +00003184 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003185 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003186 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003187 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003188 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3189 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003190 DataLen += 2; // 2 bytes for builtin ID
3191 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003192 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003193 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003194
Richard Smitha534a312015-07-21 23:54:07 +00003195 if (NeedDecls) {
3196 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3197 DEnd = IdResolver.end();
3198 D != DEnd; ++D)
3199 DataLen += 4;
3200 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003201 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003202 using namespace llvm::support;
3203 endian::Writer<little> LE(Out);
3204
Richard Smithdf8a8312015-03-13 04:05:01 +00003205 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003206 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003207 // We emit the key length after the data length so that every
3208 // string is preceded by a 16-bit length. This matches the PTH
3209 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003210 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003211 return std::make_pair(KeyLen, DataLen);
3212 }
Mike Stump11289f42009-09-09 15:08:12 +00003213
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003214 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003215 unsigned KeyLen) {
3216 // Record the location of the key data. This is used when generating
3217 // the mapping from persistent IDs to strings.
3218 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003219
3220 // Emit the offset of the key/data length information to the interesting
3221 // identifiers table if necessary.
3222 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3223 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3224
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003225 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003226 }
Mike Stump11289f42009-09-09 15:08:12 +00003227
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003228 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003229 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003230 using namespace llvm::support;
3231 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003232
Richard Smithd7329392015-04-21 21:46:32 +00003233 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3234 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003235 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003236 return;
3237 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003238
Justin Bognere1c147c2014-03-28 22:03:19 +00003239 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003240 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3241 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003242 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003243 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003244 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003245 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003246 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3247 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003248 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003249 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003250 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003251 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003252
Richard Smithd7329392015-04-21 21:46:32 +00003253 if (HadMacroDefinition)
3254 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003255
Richard Smitha534a312015-07-21 23:54:07 +00003256 if (NeedDecls) {
3257 // Emit the declaration IDs in reverse order, because the
3258 // IdentifierResolver provides the declarations as they would be
3259 // visible (e.g., the function "stat" would come before the struct
3260 // "stat"), but the ASTReader adds declarations to the end of the list
3261 // (so we need to see the struct "stat" before the function "stat").
3262 // Only emit declarations that aren't from a chained PCH, though.
3263 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3264 IdResolver.end());
3265 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3266 DEnd = Decls.rend();
3267 D != DEnd; ++D)
3268 LE.write<uint32_t>(
3269 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3270 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003271 }
3272};
3273} // end anonymous namespace
3274
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003275/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003276///
3277/// The identifier table consists of a blob containing string data
3278/// (the actual identifiers themselves) and a separate "offsets" index
3279/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003280void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3281 IdentifierResolver &IdResolver,
3282 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003283 using namespace llvm;
3284
Richard Smith33e0f7e2015-07-22 02:08:40 +00003285 RecordData InterestingIdents;
3286
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003287 // Create and write out the blob that contains the identifier
3288 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003289 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003290 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003291 ASTIdentifierTableTrait Trait(
3292 *this, PP, IdResolver, IsModule,
3293 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003294
Douglas Gregore6648fb2009-04-28 20:33:11 +00003295 // Look for any identifiers that were named while processing the
3296 // headers, but are otherwise not needed. We add these to the hash
3297 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003298 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003299 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003300 SmallVector<const IdentifierInfo *, 128> IIs;
Douglas Gregore6648fb2009-04-28 20:33:11 +00003301 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3302 IDEnd = PP.getIdentifierTable().end();
3303 ID != IDEnd; ++ID)
Chandler Carruth8440c982015-03-26 23:54:15 +00003304 IIs.push_back(ID->second);
3305 // Sort the identifiers lexicographically before getting them references so
3306 // that their order is stable.
3307 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3308 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003309 if (Trait.isInterestingNonMacroIdentifier(II))
3310 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003311
Sebastian Redlff4a2952010-07-23 23:49:55 +00003312 // Create the on-disk hash table representation. We only store offsets
3313 // for identifiers that appear here for the first time.
3314 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003315 for (auto IdentIDPair : IdentifierIDs) {
3316 IdentifierInfo *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3317 IdentID ID = IdentIDPair.second;
3318 assert(II && "NULL identifier in identifier table");
3319 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
3320 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003321 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003322
Douglas Gregore84a9da2009-04-20 20:36:09 +00003323 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003324 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003325 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003326 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003327 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003328 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003329 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003330 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003331 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003332 }
3333
3334 // Create a blob abbreviation
3335 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003336 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003339 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003340
3341 // Write the identifier table
3342 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003343 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003344 Record.push_back(BucketOffset);
Yaron Keren92e1b622015-03-18 10:17:07 +00003345 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003346 }
3347
3348 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003349 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003350 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3354 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3355
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003356#ifndef NDEBUG
3357 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3358 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3359#endif
3360
Douglas Gregor0e149972009-04-25 19:10:14 +00003361 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003362 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003363 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003364 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003365 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003366 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003367
3368 // In C++, write the list of interesting identifiers (those that are
3369 // defined as macros, poisoned, or similar unusual things).
3370 if (!InterestingIdents.empty())
3371 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003372}
3373
Douglas Gregorc5046832009-04-27 18:38:38 +00003374//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003375// DeclContext's Name Lookup Table Serialization
3376//===----------------------------------------------------------------------===//
3377
3378namespace {
3379// Trait used for the on-disk hash table used in the method pool.
3380class ASTDeclContextNameLookupTrait {
3381 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003382 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003383
3384public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003385 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003386 typedef key_type key_type_ref;
3387
Richard Smithd88a7f12015-09-01 20:35:42 +00003388 /// A start and end index into DeclIDs, representing a sequence of decls.
3389 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003390 typedef const data_type& data_type_ref;
3391
Justin Bogner25463f12014-04-18 20:27:24 +00003392 typedef unsigned hash_value_type;
3393 typedef unsigned offset_type;
3394
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003395 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3396
Richard Smithd88a7f12015-09-01 20:35:42 +00003397 template<typename Coll>
3398 data_type getData(const Coll &Decls) {
3399 unsigned Start = DeclIDs.size();
3400 for (NamedDecl *D : Decls) {
3401 DeclIDs.push_back(
3402 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3403 }
3404 return std::make_pair(Start, DeclIDs.size());
3405 }
3406
3407 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3408 unsigned Start = DeclIDs.size();
3409 for (auto ID : FromReader)
3410 DeclIDs.push_back(ID);
3411 return std::make_pair(Start, DeclIDs.size());
3412 }
3413
3414 static bool EqualKey(key_type_ref a, key_type_ref b) {
3415 return a == b;
3416 }
3417
Richard Smitha06c7e62015-08-26 23:55:49 +00003418 hash_value_type ComputeHash(DeclarationNameKey Name) {
3419 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003420 }
3421
Richard Smithd88a7f12015-09-01 20:35:42 +00003422 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3423 assert(Writer.hasChain() &&
3424 "have reference to loaded module file but no chain?");
3425
3426 using namespace llvm::support;
3427 endian::Writer<little>(Out)
3428 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3429 }
3430
Richard Smitha06c7e62015-08-26 23:55:49 +00003431 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3432 DeclarationNameKey Name,
3433 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003434 using namespace llvm::support;
3435 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003436 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003437 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003438 case DeclarationName::Identifier:
3439 case DeclarationName::ObjCZeroArgSelector:
3440 case DeclarationName::ObjCOneArgSelector:
3441 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003442 case DeclarationName::CXXLiteralOperatorName:
3443 KeyLen += 4;
3444 break;
3445 case DeclarationName::CXXOperatorName:
3446 KeyLen += 1;
3447 break;
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:
3452 break;
3453 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003454 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003455
Richard Smithf02662d2015-07-30 03:17:16 +00003456 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003457 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3458 assert(uint16_t(DataLen) == DataLen &&
3459 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003460 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003461
3462 return std::make_pair(KeyLen, DataLen);
3463 }
3464
Richard Smitha06c7e62015-08-26 23:55:49 +00003465 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003466 using namespace llvm::support;
3467 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003468 LE.write<uint8_t>(Name.getKind());
3469 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003470 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003471 case DeclarationName::CXXLiteralOperatorName:
3472 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003473 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003474 case DeclarationName::ObjCZeroArgSelector:
3475 case DeclarationName::ObjCOneArgSelector:
3476 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003477 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003478 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003479 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003480 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003481 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003482 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003483 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003484 case DeclarationName::CXXConstructorName:
3485 case DeclarationName::CXXDestructorName:
3486 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003487 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003488 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003489 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003490
3491 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003492 }
3493
Richard Smitha06c7e62015-08-26 23:55:49 +00003494 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3495 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003496 using namespace llvm::support;
3497 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003498 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003499 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3500 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003501 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3502 }
3503};
3504} // end anonymous namespace
3505
Chandler Carruthe972c362015-03-26 03:11:40 +00003506bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3507 DeclContext *DC) {
3508 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3509}
3510
3511bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3512 DeclContext *DC) {
3513 for (auto *D : Result.getLookupResult())
3514 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3515 return false;
3516
3517 return true;
3518}
3519
Richard Smithd88a7f12015-09-01 20:35:42 +00003520void
Chandler Carruthe972c362015-03-26 03:11:40 +00003521ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003522 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003523 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3524 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003525 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003526
Chandler Carruthe972c362015-03-26 03:11:40 +00003527 // FIXME: We need to build the lookups table, which is logically const.
3528 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
3529 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3530
3531 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003532 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3533 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003534 ASTDeclContextNameLookupTrait Trait(*this);
3535
Chandler Carruthe972c362015-03-26 03:11:40 +00003536 // The first step is to collect the declaration names which we need to
3537 // serialize into the name lookup table, and to collect them in a stable
3538 // order.
3539 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003540
Chandler Carruthe972c362015-03-26 03:11:40 +00003541 // We also build up small sets of the constructor and conversion function
3542 // names which are visible.
3543 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003544
Chandler Carruthe972c362015-03-26 03:11:40 +00003545 for (auto &Lookup : *DC->buildLookup()) {
3546 auto &Name = Lookup.first;
3547 auto &Result = Lookup.second;
3548
3549 // If there are no local declarations in our lookup result, we don't
3550 // need to write an entry for the name at all unless we're rewriting
3551 // the decl context. If we can't write out a lookup set without
3552 // performing more deserialization, just skip this entry.
3553 if (isLookupResultExternal(Result, DC) && !isRewritten(cast<Decl>(DC)) &&
3554 isLookupResultEntirelyExternal(Result, DC))
3555 continue;
3556
3557 // We also skip empty results. If any of the results could be external and
3558 // the currently available results are empty, then all of the results are
3559 // external and we skip it above. So the only way we get here with an empty
3560 // results is when no results could have been external *and* we have
3561 // external results.
3562 //
3563 // FIXME: While we might want to start emitting on-disk entries for negative
3564 // lookups into a decl context as an optimization, today we *have* to skip
3565 // them because there are names with empty lookup results in decl contexts
3566 // which we can't emit in any stable ordering: we lookup constructors and
3567 // conversion functions in the enclosing namespace scope creating empty
3568 // results for them. This in almost certainly a bug in Clang's name lookup,
3569 // but that is likely to be hard or impossible to fix and so we tolerate it
3570 // here by omitting lookups with empty results.
3571 if (Lookup.second.getLookupResult().empty())
3572 continue;
3573
3574 switch (Lookup.first.getNameKind()) {
3575 default:
3576 Names.push_back(Lookup.first);
3577 break;
3578
Richard Smithcd45dbc2014-04-19 03:48:30 +00003579 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003580 assert(isa<CXXRecordDecl>(DC) &&
3581 "Cannot have a constructor name outside of a class!");
3582 ConstructorNameSet.insert(Name);
3583 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003584
3585 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003586 assert(isa<CXXRecordDecl>(DC) &&
3587 "Cannot have a conversion function name outside of a class!");
3588 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003589 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003590 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003591 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003592
Chandler Carruthe972c362015-03-26 03:11:40 +00003593 // Sort the names into a stable order.
3594 std::sort(Names.begin(), Names.end());
3595
Chandler Carruthffbf7052015-03-26 22:27:09 +00003596 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003597 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003598 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003599
Chandler Carruthffbf7052015-03-26 22:27:09 +00003600 // First we try the easy case by forming the current context's constructor
3601 // name and adding that name first. This is a very useful optimization to
3602 // avoid walking the lexical declarations in many cases, and it also
3603 // handles the only case where a constructor name can come from some other
3604 // lexical context -- when that name is an implicit constructor merged from
3605 // another declaration in the redecl chain. Any non-implicit constructor or
3606 // conversion function which doesn't occur in all the lexical contexts
3607 // would be an ODR violation.
3608 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3609 Context->getCanonicalType(Context->getRecordType(D)));
3610 if (ConstructorNameSet.erase(ImplicitCtorName))
3611 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003612
Chandler Carruthffbf7052015-03-26 22:27:09 +00003613 // If we still have constructors or conversion functions, we walk all the
3614 // names in the decl and add the constructors and conversion functions
3615 // which are visible in the order they lexically occur within the context.
3616 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3617 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3618 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3619 auto Name = ChildND->getDeclName();
3620 switch (Name.getNameKind()) {
3621 default:
3622 continue;
3623
3624 case DeclarationName::CXXConstructorName:
3625 if (ConstructorNameSet.erase(Name))
3626 Names.push_back(Name);
3627 break;
3628
3629 case DeclarationName::CXXConversionFunctionName:
3630 if (ConversionNameSet.erase(Name))
3631 Names.push_back(Name);
3632 break;
3633 }
3634
3635 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3636 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003637 }
3638
Chandler Carruthe972c362015-03-26 03:11:40 +00003639 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3640 "constructors by walking all the "
3641 "lexical members of the context.");
3642 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3643 "conversion functions by walking all "
3644 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003645 }
3646
Chandler Carruthe972c362015-03-26 03:11:40 +00003647 // Next we need to do a lookup with each name into this decl context to fully
3648 // populate any results from external sources. We don't actually use the
3649 // results of these lookups because we only want to use the results after all
3650 // results have been loaded and the pointers into them will be stable.
3651 for (auto &Name : Names)
3652 DC->lookup(Name);
3653
3654 // Now we need to insert the results for each name into the hash table. For
3655 // constructor names and conversion function names, we actually need to merge
3656 // all of the results for them into one list of results each and insert
3657 // those.
3658 SmallVector<NamedDecl *, 8> ConstructorDecls;
3659 SmallVector<NamedDecl *, 8> ConversionDecls;
3660
3661 // Now loop over the names, either inserting them or appending for the two
3662 // special cases.
3663 for (auto &Name : Names) {
3664 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3665
3666 switch (Name.getNameKind()) {
3667 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003668 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003669 break;
3670
3671 case DeclarationName::CXXConstructorName:
3672 ConstructorDecls.append(Result.begin(), Result.end());
3673 break;
3674
3675 case DeclarationName::CXXConversionFunctionName:
3676 ConversionDecls.append(Result.begin(), Result.end());
3677 break;
3678 }
3679 }
3680
3681 // Handle our two special cases if we ended up having any. We arbitrarily use
3682 // the first declaration's name here because the name itself isn't part of
3683 // the key, only the kind of name is used.
3684 if (!ConstructorDecls.empty())
3685 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003686 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003687 if (!ConversionDecls.empty())
3688 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003689 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003690
Richard Smithd88a7f12015-09-01 20:35:42 +00003691 // Create the on-disk hash table. Also emit the existing imported and
3692 // merged table if there is one.
3693 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3694 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003695}
3696
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003697/// \brief Write the block containing all of the declaration IDs
3698/// visible from the given DeclContext.
3699///
3700/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003701/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003702uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3703 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003704 // If we imported a key declaration of this namespace, write the visible
3705 // lookup results as an update record for it rather than including them
3706 // on this declaration. We will only look at key declarations on reload.
3707 if (isa<NamespaceDecl>(DC) && Chain &&
3708 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3709 // Only do this once, for the first local declaration of the namespace.
3710 for (NamespaceDecl *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
3711 Prev = Prev->getPreviousDecl())
3712 if (!Prev->isFromASTFile())
3713 return 0;
3714
3715 // Note that we need to emit an update record for the primary context.
3716 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3717
3718 // Make sure all visible decls are written. They will be recorded later. We
3719 // do this using a side data structure so we can sort the names into
3720 // a deterministic order.
3721 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3722 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3723 LookupResults;
3724 if (Map) {
3725 LookupResults.reserve(Map->size());
3726 for (auto &Entry : *Map)
3727 LookupResults.push_back(
3728 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3729 }
3730
3731 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3732 for (auto &NameAndResult : LookupResults) {
3733 DeclarationName Name = NameAndResult.first;
3734 DeclContext::lookup_result Result = NameAndResult.second;
3735 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3736 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3737 // We have to work around a name lookup bug here where negative lookup
3738 // results for these names get cached in namespace lookup tables (these
3739 // names should never be looked up in a namespace).
3740 assert(Result.empty() && "Cannot have a constructor or conversion "
3741 "function name in a namespace!");
3742 continue;
3743 }
3744
3745 for (NamedDecl *ND : Result)
3746 if (!ND->isFromASTFile())
3747 GetDeclRef(ND);
3748 }
3749
3750 return 0;
3751 }
3752
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003753 if (DC->getPrimaryContext() != DC)
3754 return 0;
3755
Chandler Carruthe972c362015-03-26 03:11:40 +00003756 // Skip contexts which don't support name lookup.
3757 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003758 return 0;
3759
3760 // If not in C++, we perform name lookup for the translation unit via the
3761 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003762 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003763 return 0;
3764
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003765 // Serialize the contents of the mapping used for lookup. Note that,
3766 // although we have two very different code paths, the serialized
3767 // representation is the same for both cases: a declaration name,
3768 // followed by a size, followed by references to the visible
3769 // declarations that have that name.
3770 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003771 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003772 if (!Map || Map->empty())
3773 return 0;
3774
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003775 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003776 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003777 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003778
3779 // Write the lookup table
3780 RecordData Record;
3781 Record.push_back(DECL_CONTEXT_VISIBLE);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003782 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003783 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003784 ++NumVisibleDeclContexts;
3785 return Offset;
3786}
3787
Sebastian Redla4071b42010-08-24 00:50:09 +00003788/// \brief Write an UPDATE_VISIBLE block for the given context.
3789///
3790/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3791/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003792/// (in C++), for namespaces, and for classes with forward-declared unscoped
3793/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003794void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003795 if (isRewritten(cast<Decl>(DC)))
3796 return;
3797
Richard Smith961eae52014-03-25 01:14:22 +00003798 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003799 if (!Map || Map->empty())
3800 return;
3801
Sebastian Redla4071b42010-08-24 00:50:09 +00003802 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003803 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003804 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003805
Richard Smith5fc18a92015-07-12 23:43:21 +00003806 // If we're updating a namespace, select a key declaration as the key for the
3807 // update record; those are the only ones that will be checked on reload.
3808 if (isa<NamespaceDecl>(DC))
3809 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3810
Sebastian Redla4071b42010-08-24 00:50:09 +00003811 // Write the lookup table
3812 RecordData Record;
3813 Record.push_back(UPDATE_VISIBLE);
3814 Record.push_back(getDeclID(cast<Decl>(DC)));
Yaron Keren92e1b622015-03-18 10:17:07 +00003815 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003816}
3817
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003818/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3819void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3820 RecordData Record;
3821 Record.push_back(Opts.fp_contract);
3822 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3823}
3824
3825/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3826void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003827 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003828 return;
3829
3830 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3831 RecordData Record;
3832#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3833#include "clang/Basic/OpenCLExtensions.def"
3834 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3835}
3836
Douglas Gregor404cdde2012-01-27 01:47:08 +00003837void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003838 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003839 RecordData Categories;
3840
3841 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3842 unsigned Size = 0;
3843 unsigned StartIndex = Categories.size();
3844
3845 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3846
3847 // Allocate space for the size.
3848 Categories.push_back(0);
3849
3850 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003851 for (ObjCInterfaceDecl::known_categories_iterator
3852 Cat = Class->known_categories_begin(),
3853 CatEnd = Class->known_categories_end();
3854 Cat != CatEnd; ++Cat, ++Size) {
3855 assert(getDeclID(*Cat) != 0 && "Bogus category");
3856 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003857 }
3858
3859 // Update the size.
3860 Categories[StartIndex] = Size;
3861
3862 // Record this interface -> category map.
3863 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3864 CategoriesMap.push_back(CatInfo);
3865 }
3866
3867 // Sort the categories map by the definition ID, since the reader will be
3868 // performing binary searches on this information.
3869 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3870
3871 // Emit the categories map.
3872 using namespace llvm;
3873 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3874 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3877 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3878
3879 RecordData Record;
3880 Record.push_back(OBJC_CATEGORIES_MAP);
3881 Record.push_back(CategoriesMap.size());
3882 Stream.EmitRecordWithBlob(AbbrevID, Record,
3883 reinterpret_cast<char*>(CategoriesMap.data()),
3884 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3885
3886 // Emit the category lists.
3887 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3888}
3889
Richard Smithe40f2ba2013-08-07 21:41:30 +00003890void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3891 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3892
3893 if (LPTMap.empty())
3894 return;
3895
3896 RecordData Record;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00003897 for (auto LPTMapEntry : LPTMap) {
3898 const FunctionDecl *FD = LPTMapEntry.first;
3899 LateParsedTemplate *LPT = LPTMapEntry.second;
3900 AddDeclRef(FD, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003901 AddDeclRef(LPT->D, Record);
3902 Record.push_back(LPT->Toks.size());
3903
3904 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3905 TokEnd = LPT->Toks.end();
3906 TokIt != TokEnd; ++TokIt) {
3907 AddToken(*TokIt, Record);
3908 }
3909 }
3910 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3911}
3912
Dario Domizioli13a0a382014-05-23 12:13:25 +00003913/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3914void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3915 RecordData Record;
3916 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3917 AddSourceLocation(PragmaLoc, Record);
3918 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3919}
3920
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003921//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003922// General Serialization Routines
3923//===----------------------------------------------------------------------===//
3924
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003925/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003926void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3927 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003928 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003929 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3930 e = Attrs.end(); i != e; ++i){
3931 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003932 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003933 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003934
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003935#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003936
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003937 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003938}
3939
John McCallf413f5e2013-05-03 00:10:13 +00003940void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3941 AddSourceLocation(Tok.getLocation(), Record);
3942 Record.push_back(Tok.getLength());
3943
3944 // FIXME: When reading literal tokens, reconstruct the literal pointer
3945 // if it is needed.
3946 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3947 // FIXME: Should translate token kind to a stable encoding.
3948 Record.push_back(Tok.getKind());
3949 // FIXME: Should translate token flags to a stable encoding.
3950 Record.push_back(Tok.getFlags());
3951}
3952
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003953void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003954 Record.push_back(Str.size());
3955 Record.insert(Record.end(), Str.begin(), Str.end());
3956}
3957
Richard Smith7ed1bc92014-12-05 22:42:13 +00003958bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00003959 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00003960
Richard Smith54cc3c22014-12-11 20:50:24 +00003961 bool Changed =
3962 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00003963
3964 // Remove a prefix to make the path relative, if relevant.
3965 const char *PathBegin = Path.data();
3966 const char *PathPtr =
3967 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
3968 if (PathPtr != PathBegin) {
3969 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
3970 Changed = true;
3971 }
3972
3973 return Changed;
3974}
3975
3976void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
3977 SmallString<128> FilePath(Path);
3978 PreparePathForOutput(FilePath);
3979 AddString(FilePath, Record);
3980}
3981
3982void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
3983 StringRef Path) {
3984 SmallString<128> FilePath(Path);
3985 PreparePathForOutput(FilePath);
3986 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
3987}
3988
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003989void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3990 RecordDataImpl &Record) {
3991 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003992 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003993 Record.push_back(*Minor + 1);
3994 else
3995 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003996 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003997 Record.push_back(*Subminor + 1);
3998 else
3999 Record.push_back(0);
4000}
4001
Douglas Gregore84a9da2009-04-20 20:36:09 +00004002/// \brief Note that the identifier II occurs at the given offset
4003/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004004void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004005 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004006 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004007 // up earlier in the chain and thus don't need an offset.
4008 if (ID >= FirstIdentID)
4009 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004010}
4011
Douglas Gregor95c13f52009-04-25 17:48:32 +00004012/// \brief Note that the selector Sel occurs at the given offset
4013/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004014void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004015 unsigned ID = SelectorIDs[Sel];
4016 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004017 // Don't record offsets for selectors that are also available in a different
4018 // file.
4019 if (ID < FirstSelectorID)
4020 return;
4021 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004022}
4023
Richard Smithe75ee0f2015-08-17 07:13:32 +00004024ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream, bool IncludeTimestamps)
Richard Smith01b2cb42014-07-26 06:37:51 +00004025 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004026 WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps),
4027 WritingAST(false), DoneWritingDeclsAndTypes(false),
4028 ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS),
4029 NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS),
4030 NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS),
4031 NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS),
4032 NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
Richard Smith01b2cb42014-07-26 06:37:51 +00004033 NextSubmoduleID(FirstSubmoduleID),
4034 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4035 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4036 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00004037 NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
Richard Smithe75ee0f2015-08-17 07:13:32 +00004038 TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004039 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004040 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004041 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004042 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4043 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4044 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004045
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004046ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004047 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004048}
4049
Richard Smithb3761912015-02-05 23:08:52 +00004050const LangOptions &ASTWriter::getLangOpts() const {
4051 assert(WritingAST && "can't determine lang opts when not writing AST");
4052 return Context->getLangOpts();
4053}
4054
Richard Smithe75ee0f2015-08-17 07:13:32 +00004055time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4056 return IncludeTimestamps ? E->getModificationTime() : 0;
4057}
4058
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004059void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004060 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004061 Module *WritingModule, StringRef isysroot,
4062 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004063 WritingAST = true;
4064
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004065 ASTHasCompilerErrors = hasErrors;
4066
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004067 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004068 Stream.Emit((unsigned)'C', 8);
4069 Stream.Emit((unsigned)'P', 8);
4070 Stream.Emit((unsigned)'C', 8);
4071 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004072
Chris Lattner28fa4e62009-04-26 22:26:21 +00004073 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004074
Douglas Gregoreda8e122011-08-09 15:13:55 +00004075 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004076 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004077 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004078 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004079 Context = nullptr;
4080 PP = nullptr;
4081 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004082 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004083
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004084 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004085}
4086
Douglas Gregora94a1542011-07-27 21:45:57 +00004087template<typename Vector>
4088static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4089 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004090 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4091 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004092 Writer.AddDeclRef(*I, Record);
4093 }
4094}
4095
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004096void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004097 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004098 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004099 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004100 using namespace llvm;
4101
Craig Toppera13603a2014-05-22 05:54:18 +00004102 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004103
Douglas Gregorcf68c582011-12-01 22:20:10 +00004104 // Make sure that the AST reader knows to finalize itself.
4105 if (Chain)
4106 Chain->finalizeForWriting();
4107
Sebastian Redl143413f2010-07-12 22:02:52 +00004108 ASTContext &Context = SemaRef.Context;
4109 Preprocessor &PP = SemaRef.PP;
4110
Douglas Gregordab42432011-08-12 00:15:20 +00004111 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004112 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4113 if (D) {
4114 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4115 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004116 }
4117 };
4118 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4119 PREDEF_DECL_TRANSLATION_UNIT_ID);
4120 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4121 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4122 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4123 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4124 PREDEF_DECL_OBJC_PROTOCOL_ID);
4125 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4126 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4127 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4128 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4129 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004130 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Richard Smith5fc18a92015-07-12 23:43:21 +00004131 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004132
Chris Lattner0c797362009-09-08 18:19:27 +00004133 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004134 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004135 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004136 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004137 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004138
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004139 // Build a record containing all of the file scoped decls in this file.
4140 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004141 if (!isModule)
4142 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4143 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004144
Douglas Gregor851443c2011-08-12 01:39:19 +00004145 // Build a record containing all of the delegating constructors we still need
4146 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004147 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004148 if (!isModule)
4149 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004150
Douglas Gregor851443c2011-08-12 01:39:19 +00004151 // Write the set of weak, undeclared identifiers. We always write the
4152 // entire table, since later PCH files in a PCH chain are only interested in
4153 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004154 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004155 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4156 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4157 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4158 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4159 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4160 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4161 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004162 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004163
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004164 // Build a record containing all of the ext_vector declarations.
4165 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004166 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004167
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004168 // Build a record containing all of the VTable uses information.
4169 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004170 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004171 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4172 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4173 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4174 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4175 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004176 }
4177
Nico Weber72889432014-09-06 01:25:55 +00004178 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4179 RecordData UnusedLocalTypedefNameCandidates;
4180 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4181 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4182
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004183 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004184 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004185 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004186 I = SemaRef.PendingInstantiations.begin(),
4187 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4188 AddDeclRef(I->first, PendingInstantiations);
4189 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004190 }
4191 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4192 "There are local ones at end of translation unit!");
4193
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004194 // Build a record containing some declaration references.
4195 RecordData SemaDeclRefs;
4196 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4197 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4198 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4199 }
4200
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004201 RecordData CUDASpecialDeclRefs;
4202 if (Context.getcudaConfigureCallDecl()) {
4203 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4204 }
4205
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004206 // Build a record containing all of the known namespaces.
4207 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004208 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004209 I = SemaRef.KnownNamespaces.begin(),
4210 IEnd = SemaRef.KnownNamespaces.end();
4211 I != IEnd; ++I) {
4212 if (!I->second)
4213 AddDeclRef(I->first, KnownNamespaces);
4214 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004215
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004216 // Build a record of all used, undefined objects that require definitions.
4217 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004218
4219 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004220 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004221 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4222 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004223 AddDeclRef(I->first, UndefinedButUsed);
4224 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004225 }
4226
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004227 // Build a record containing all delete-expressions that we would like to
4228 // analyze later in AST.
4229 RecordData DeleteExprsToAnalyze;
4230
4231 for (const auto &DeleteExprsInfo :
4232 SemaRef.getMismatchingDeleteExpressions()) {
4233 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4234 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4235 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4236 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4237 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4238 }
4239 }
4240
Douglas Gregor112b9072012-10-18 05:31:06 +00004241 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004242 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004244 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004245 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004246 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004247
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004248 // This is so that older clang versions, before the introduction
4249 // of the control block, can read and reject the newer PCH format.
4250 Record.clear();
4251 Record.push_back(VERSION_MAJOR);
4252 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4253
Douglas Gregor851443c2011-08-12 01:39:19 +00004254 // Create a lexical update block containing all of the declarations in the
4255 // translation unit that do not come from other AST files.
4256 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004257 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4258 for (const auto *D : TU->noload_decls()) {
4259 if (!D->isFromASTFile()) {
4260 NewGlobalKindDeclPairs.push_back(D->getKind());
4261 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4262 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004263 }
4264
4265 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4266 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4267 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4268 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4269 Record.clear();
4270 Record.push_back(TU_UPDATE_LEXICAL);
4271 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Richard Smith82f8fcd2015-08-06 22:07:25 +00004272 bytes(NewGlobalKindDeclPairs));
Douglas Gregor851443c2011-08-12 01:39:19 +00004273
4274 // And a visible updates block for the translation unit.
4275 Abv = new llvm::BitCodeAbbrev();
4276 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4277 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004278 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4279 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4280 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004281
4282 // If we have any extern "C" names, write out a visible update for them.
4283 if (Context.ExternCContext)
4284 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004285
4286 // If the translation unit has an anonymous namespace, and we don't already
4287 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004288 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004289 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4290 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004291 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004292 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004293 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004294
Richard Smith5652c0f2014-03-21 01:48:23 +00004295 // Add update records for all mangling numbers and static local numbers.
4296 // These aren't really update records, but this is a convenient way of
4297 // tagging this rare extra data onto the declarations.
4298 for (const auto &Number : Context.MangleNumbers)
4299 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004300 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4301 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004302 for (const auto &Number : Context.StaticLocalNumbers)
4303 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004304 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4305 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004306
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004307 // Make sure visible decls, added to DeclContexts previously loaded from
4308 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004309 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004310 I = UpdatingVisibleDecls.begin(),
4311 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4312 GetDeclRef(*I);
4313 }
4314
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004315 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004316 // serialization, if we're storing decls with identifiers.
4317 if (!WritingModule || !getLangOpts().CPlusPlus) {
4318 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4319 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4320 IDEnd = PP.getIdentifierTable().end();
4321 ID != IDEnd; ++ID) {
4322 const IdentifierInfo *II = ID->second;
4323 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4324 IIs.push_back(II);
4325 }
4326 // Sort the identifiers to visit based on their name.
4327 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4328 for (const IdentifierInfo *II : IIs) {
4329 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4330 DEnd = SemaRef.IdResolver.end();
4331 D != DEnd; ++D) {
4332 GetDeclRef(*D);
4333 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004334 }
4335 }
4336
Douglas Gregor5204bde2011-08-02 16:26:37 +00004337 // Form the record of special types.
4338 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004339 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004340 AddTypeRef(Context.getFILEType(), SpecialTypes);
4341 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4342 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4343 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4344 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004345 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004346 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004347
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004348 if (Chain) {
4349 // Write the mapping information describing our module dependencies and how
4350 // each of those modules were mapped into our own offset/ID space, so that
4351 // the reader can build the appropriate mapping to its own offset/ID space.
4352 // The map consists solely of a blob with the following format:
4353 // *(module-name-len:i16 module-name:len*i8
4354 // source-location-offset:i32
4355 // identifier-id:i32
4356 // preprocessed-entity-id:i32
4357 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004358 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004359 // selector-id:i32
4360 // declaration-id:i32
4361 // c++-base-specifiers-id:i32
4362 // type-id:i32)
4363 //
4364 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4365 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4367 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004368 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004369 {
4370 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004371 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004372 using namespace llvm::support;
4373 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004374 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004375 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004376 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004377
Ben Langmuir785180e2014-10-20 16:27:30 +00004378 // Note: if a base ID was uint max, it would not be possible to load
4379 // another module after it or have more than one entity inside it.
4380 uint32_t None = std::numeric_limits<uint32_t>::max();
4381
4382 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4383 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4384 if (ShouldWrite)
4385 LE.write<uint32_t>(BaseID);
4386 else
4387 LE.write<uint32_t>(None);
4388 };
4389
Ben Langmuirfe971d92014-08-16 04:54:18 +00004390 // These values should be unique within a chain, since they will be read
4391 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004392 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4393 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4394 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4395 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4396 M->NumPreprocessedEntities);
4397 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4398 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4399 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4400 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004401 }
4402 }
4403 Record.clear();
4404 Record.push_back(MODULE_OFFSET_MAP);
4405 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4406 Buffer.data(), Buffer.size());
4407 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004408
Richard Smithb9eab6d2014-03-20 19:44:17 +00004409 RecordData DeclUpdatesOffsetsRecord;
4410
Richard Smith59442b42014-03-20 20:07:19 +00004411 // Keep writing types, declarations, and declaration update records
4412 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004413 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4414 WriteTypeAbbrevs();
4415 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004416 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4417 E = DeclsToRewrite.end();
4418 I != E; ++I)
4419 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004420 do {
4421 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4422 while (!DeclTypesToEmit.empty()) {
4423 DeclOrType DOT = DeclTypesToEmit.front();
4424 DeclTypesToEmit.pop();
4425 if (DOT.isType())
4426 WriteType(DOT.getType());
4427 else
4428 WriteDecl(Context, DOT.getDecl());
4429 }
4430 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004431 Stream.ExitBlock();
4432
Richard Smithb9eab6d2014-03-20 19:44:17 +00004433 DoneWritingDeclsAndTypes = true;
4434
4435 // These things can only be done once we've written out decls and types.
4436 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004437 if (!DeclUpdatesOffsetsRecord.empty())
4438 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004439 WriteCXXBaseSpecifiersOffsets();
Richard Smithc2bb8182015-03-24 06:36:48 +00004440 WriteCXXCtorInitializersOffsets();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004441 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004442 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004443
4444 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004445 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004446 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004447 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004448 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004449 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004450 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004451 WriteFPPragmaOptions(SemaRef.getFPOptions());
4452 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004453 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004454
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004455 // If we're emitting a module, write out the submodule information.
4456 if (WritingModule)
4457 WriteSubmodules(WritingModule);
4458
Douglas Gregor5204bde2011-08-02 16:26:37 +00004459 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4460
Douglas Gregord4df8652009-04-22 22:02:47 +00004461 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004462 if (!EagerlyDeserializedDecls.empty())
4463 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004464
4465 // Write the record containing tentative definitions.
4466 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004467 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004468
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004469 // Write the record containing unused file scoped decls.
4470 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004471 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004472
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004473 // Write the record containing weak undeclared identifiers.
4474 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004475 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004476 WeakUndeclaredIdentifiers);
4477
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004478 // Write the record containing ext_vector type names.
4479 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004480 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004481
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004482 // Write the record containing VTable uses information.
4483 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004484 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004485
Nico Weber72889432014-09-06 01:25:55 +00004486 // Write the record containing potentially unused local typedefs.
4487 if (!UnusedLocalTypedefNameCandidates.empty())
4488 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4489 UnusedLocalTypedefNameCandidates);
4490
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004491 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004492 if (!PendingInstantiations.empty())
4493 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004494
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004495 // Write the record containing declaration references of Sema.
4496 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004497 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004498
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004499 // Write the record containing CUDA-specific declaration references.
4500 if (!CUDASpecialDeclRefs.empty())
4501 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004502
4503 // Write the delegating constructors.
4504 if (!DelegatingCtorDecls.empty())
4505 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004506
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004507 // Write the known namespaces.
4508 if (!KnownNamespaces.empty())
4509 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004510
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004511 // Write the undefined internal functions and variables, and inline functions.
4512 if (!UndefinedButUsed.empty())
4513 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004514
4515 if (!DeleteExprsToAnalyze.empty())
4516 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4517
Douglas Gregor851443c2011-08-12 01:39:19 +00004518 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004519 for (auto *DC : UpdatedDeclContexts)
4520 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004521
Douglas Gregor959bb062011-12-03 01:15:29 +00004522 if (!WritingModule) {
4523 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004524 struct ModuleInfo {
4525 uint64_t ID;
4526 Module *M;
4527 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4528 };
Richard Smith56be7542014-03-21 00:33:59 +00004529 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004530 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004531 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004532 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4533 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004534 }
Richard Smith56be7542014-03-21 00:33:59 +00004535
4536 if (!Imports.empty()) {
4537 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4538 return A.ID < B.ID;
4539 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004540 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4541 return A.ID == B.ID;
4542 };
Richard Smith56be7542014-03-21 00:33:59 +00004543
4544 // Sort and deduplicate module IDs.
4545 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004546 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004547 Imports.end());
4548
4549 RecordData ImportedModules;
4550 for (const auto &Import : Imports) {
4551 ImportedModules.push_back(Import.ID);
4552 // FIXME: If the module has macros imported then later has declarations
4553 // imported, this location won't be the right one as a location for the
4554 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004555 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004556 }
4557
Douglas Gregor959bb062011-12-03 01:15:29 +00004558 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4559 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004560 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004561
Douglas Gregor851443c2011-08-12 01:39:19 +00004562 WriteDeclReplacementsBlock();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004563 WriteObjCCategories();
Dario Domizioli13a0a382014-05-23 12:13:25 +00004564 if(!WritingModule)
4565 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004566
Douglas Gregor08f01292009-04-17 22:13:46 +00004567 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004568 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004569 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004570 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004571 Record.push_back(NumLexicalDeclContexts);
4572 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004573 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004574 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004575}
4576
Richard Smithb9eab6d2014-03-20 19:44:17 +00004577void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004578 if (DeclUpdates.empty())
4579 return;
4580
Richard Smith59442b42014-03-20 20:07:19 +00004581 DeclUpdateMap LocalUpdates;
4582 LocalUpdates.swap(DeclUpdates);
4583
Richard Smith6ef42932014-03-20 21:02:00 +00004584 for (auto &DeclUpdate : LocalUpdates) {
4585 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004586 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004587 continue; // The decl will be written completely,no need to store updates.
4588
Richard Smithd28ac5b2014-03-22 23:33:22 +00004589 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004590 RecordData Record;
4591 for (auto &Update : DeclUpdate.second) {
4592 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4593
4594 Record.push_back(Kind);
4595 switch (Kind) {
4596 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4597 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4598 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004599 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004600 Record.push_back(GetDeclRef(Update.getDecl()));
4601 break;
4602
Richard Smith4d235792014-08-07 18:53:08 +00004603 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004604 // An updated body is emitted last, so that the reader doesn't need
4605 // to skip over the lazy body to reach statements for other records.
4606 Record.pop_back();
4607 HasUpdatedBody = true;
4608 break;
4609
Richard Smith4d235792014-08-07 18:53:08 +00004610 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4611 AddSourceLocation(Update.getLoc(), Record);
4612 break;
4613
Richard Smithcd45dbc2014-04-19 03:48:30 +00004614 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4615 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004616 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004617 AddCXXDefinitionData(RD, Record);
4618 Record.push_back(WriteDeclContextLexicalBlock(
4619 *Context, const_cast<CXXRecordDecl *>(RD)));
4620
4621 // This state is sometimes updated by template instantiation, when we
4622 // switch from the specialization referring to the template declaration
4623 // to it referring to the template definition.
4624 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4625 Record.push_back(MSInfo->getTemplateSpecializationKind());
4626 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4627 } else {
4628 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4629 Record.push_back(Spec->getTemplateSpecializationKind());
4630 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004631
4632 // The instantiation might have been resolved to a partial
4633 // specialization. If so, record which one.
4634 auto From = Spec->getInstantiatedFrom();
4635 if (auto PartialSpec =
4636 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4637 Record.push_back(true);
4638 AddDeclRef(PartialSpec, Record);
4639 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4640 Record);
4641 } else {
4642 Record.push_back(false);
4643 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004644 }
4645 Record.push_back(RD->getTagKind());
4646 AddSourceLocation(RD->getLocation(), Record);
4647 AddSourceLocation(RD->getLocStart(), Record);
4648 AddSourceLocation(RD->getRBraceLoc(), Record);
4649
4650 // Instantiation may change attributes; write them all out afresh.
4651 Record.push_back(D->hasAttrs());
4652 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004653 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4654 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004655
4656 // FIXME: Ensure we don't get here for explicit instantiations.
4657 break;
4658 }
4659
Richard Smithf8134002015-03-10 01:41:22 +00004660 case UPD_CXX_RESOLVED_DTOR_DELETE:
4661 AddDeclRef(Update.getDecl(), Record);
4662 break;
4663
Richard Smith564417a2014-03-20 21:47:22 +00004664 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4665 addExceptionSpec(
4666 *this,
4667 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4668 Record);
4669 break;
4670
Richard Smith6ef42932014-03-20 21:02:00 +00004671 case UPD_CXX_DEDUCED_RETURN_TYPE:
4672 Record.push_back(GetOrCreateTypeID(Update.getType()));
4673 break;
4674
4675 case UPD_DECL_MARKED_USED:
4676 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004677
4678 case UPD_MANGLING_NUMBER:
4679 case UPD_STATIC_LOCAL_NUMBER:
4680 Record.push_back(Update.getNumber());
4681 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004682
Alexey Bataev97720002014-11-11 04:05:39 +00004683 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4684 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4685 Record);
4686 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004687
4688 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004689 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004690 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004691
4692 case UPD_ADDED_ATTR_TO_RECORD:
4693 WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record);
4694 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004695 }
4696 }
4697
Richard Smithd28ac5b2014-03-22 23:33:22 +00004698 if (HasUpdatedBody) {
4699 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004700 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004701 Record.push_back(Def->isInlined());
4702 AddSourceLocation(Def->getInnerLocStart(), Record);
4703 AddFunctionDefinition(Def, Record);
4704 }
4705
Richard Smithcd45dbc2014-04-19 03:48:30 +00004706 OffsetsRecord.push_back(GetDeclRef(D));
4707 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4708
Richard Smith6ef42932014-03-20 21:02:00 +00004709 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004710
Richard Smithc2bb8182015-03-24 06:36:48 +00004711 FlushPendingAfterDecl();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004712 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004713}
4714
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004715void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004716 if (ReplacedDecls.empty())
4717 return;
4718
4719 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004720 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4721 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004722 Record.push_back(I->ID);
4723 Record.push_back(I->Offset);
4724 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004725 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004726 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004727}
4728
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004729void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004730 Record.push_back(Loc.getRawEncoding());
4731}
4732
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004733void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004734 AddSourceLocation(Range.getBegin(), Record);
4735 AddSourceLocation(Range.getEnd(), Record);
4736}
4737
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004738void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004739 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004740 const uint64_t *Words = Value.getRawData();
4741 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004742}
4743
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004744void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004745 Record.push_back(Value.isUnsigned());
4746 AddAPInt(Value, Record);
4747}
4748
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004749void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004750 AddAPInt(Value.bitcastToAPInt(), Record);
4751}
4752
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004753void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004754 Record.push_back(getIdentifierRef(II));
4755}
4756
Sebastian Redl539c5062010-08-18 23:57:32 +00004757IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004758 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004759 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004760
Sebastian Redl539c5062010-08-18 23:57:32 +00004761 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004762 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004763 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004764 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004765}
4766
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004767MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004768 // Don't emit builtin macros like __LINE__ to the AST file unless they
4769 // have been redefined by the header (in which case they are not
4770 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004771 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004772 return 0;
4773
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004774 MacroID &ID = MacroIDs[MI];
4775 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004776 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004777 MacroInfoToEmitData Info = { Name, MI, ID };
4778 MacroInfosToEmit.push_back(Info);
4779 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004780 return ID;
4781}
4782
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004783MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004784 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004785 return 0;
4786
4787 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4788 return MacroIDs[MI];
4789}
4790
4791uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004792 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004793}
4794
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004795void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004796 Record.push_back(getSelectorRef(SelRef));
4797}
4798
Sebastian Redl539c5062010-08-18 23:57:32 +00004799SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004800 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004801 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004802 }
4803
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004804 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004805 if (SID == 0 && Chain) {
4806 // This might trigger a ReadSelector callback, which will set the ID for
4807 // this selector.
4808 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004809 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004810 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004811 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004812 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004813 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004814 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004815 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004816}
4817
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004818void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004819 AddDeclRef(Temp->getDestructor(), Record);
4820}
4821
Richard Smithc2bb8182015-03-24 06:36:48 +00004822void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
4823 RecordDataImpl &Record) {
4824 assert(!Inits.empty() && "Empty ctor initializer sets are not recorded");
4825 CXXCtorInitializersToWrite.push_back(
4826 QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits));
4827 Record.push_back(NextCXXCtorInitializersID++);
4828}
4829
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004830void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
Richard Smithc2bb8182015-03-24 06:36:48 +00004831 CXXBaseSpecifier const *BasesEnd,
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004832 RecordDataImpl &Record) {
4833 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4834 CXXBaseSpecifiersToWrite.push_back(
4835 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4836 Bases, BasesEnd));
4837 Record.push_back(NextCXXBaseSpecifiersID++);
4838}
4839
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004840void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004841 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004842 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004843 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004844 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004845 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004846 break;
4847 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004848 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004849 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004850 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004851 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004852 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004853 break;
4854 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004855 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004856 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004857 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004858 break;
John McCall0ad16662009-10-29 08:12:44 +00004859 case TemplateArgument::Null:
4860 case TemplateArgument::Integral:
4861 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004862 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004863 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004864 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004865 break;
4866 }
4867}
4868
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004869void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004870 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004871 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004872
4873 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4874 bool InfoHasSameExpr
4875 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4876 Record.push_back(InfoHasSameExpr);
4877 if (InfoHasSameExpr)
4878 return; // Avoid storing the same expr twice.
4879 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004880 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4881 Record);
4882}
4883
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004884void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4885 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004886 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004887 AddTypeRef(QualType(), Record);
4888 return;
4889 }
4890
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004891 AddTypeLoc(TInfo->getTypeLoc(), Record);
4892}
4893
4894void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4895 AddTypeRef(TL.getType(), Record);
4896
John McCall8f115c62009-10-16 21:56:05 +00004897 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004898 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004899 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004900}
4901
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004902void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004903 Record.push_back(GetOrCreateTypeID(T));
4904}
4905
Richard Smith2095ffe2015-03-16 20:11:03 +00004906TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004907 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004908 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4909 if (T.isNull())
4910 return TypeIdx();
4911 assert(!T.getLocalFastQualifiers());
4912
4913 TypeIdx &Idx = TypeIdxs[T];
4914 if (Idx.getIndex() == 0) {
4915 if (DoneWritingDeclsAndTypes) {
4916 assert(0 && "New type seen after serializing all the types to emit!");
4917 return TypeIdx();
4918 }
4919
4920 // We haven't seen this type before. Assign it a new ID and put it
4921 // into the queue of types to emit.
4922 Idx = TypeIdx(NextTypeID++);
4923 DeclTypesToEmit.push(T);
4924 }
4925 return Idx;
4926 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004927}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004928
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004929TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004930 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004931 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4932 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004933 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00004934 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004935
Richard Smith2095ffe2015-03-16 20:11:03 +00004936 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4937 assert(I != TypeIdxs.end() && "Type not emitted!");
4938 return I->second;
4939 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004940}
4941
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004942void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004943 Record.push_back(GetDeclRef(D));
4944}
4945
Sebastian Redl539c5062010-08-18 23:57:32 +00004946DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004947 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00004948
4949 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004950 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004951 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004952
4953 // If D comes from an AST file, its declaration ID is already known and
4954 // fixed.
4955 if (D->isFromASTFile())
4956 return D->getGlobalID();
4957
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004958 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004959 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004960 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004961 if (DoneWritingDeclsAndTypes) {
4962 assert(0 && "New decl seen after serializing all the decls to emit!");
4963 return 0;
4964 }
4965
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004966 // We haven't seen this declaration before. Give it a new ID and
4967 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004968 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004969 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004970 }
4971
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004972 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004973}
4974
Sebastian Redl539c5062010-08-18 23:57:32 +00004975DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00004976 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00004977 return 0;
4978
Douglas Gregorb3163e52012-01-05 22:33:30 +00004979 // If D comes from an AST file, its declaration ID is already known and
4980 // fixed.
4981 if (D->isFromASTFile())
4982 return D->getGlobalID();
4983
Douglas Gregore84a9da2009-04-20 20:36:09 +00004984 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4985 return DeclIDs[D];
4986}
4987
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004988void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004989 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004990 assert(D);
4991
4992 SourceLocation Loc = D->getLocation();
4993 if (Loc.isInvalid())
4994 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004995
4996 // We only keep track of the file-level declarations of each file.
4997 if (!D->getLexicalDeclContext()->isFileContext())
4998 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004999 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5000 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005001 if (isa<ParmVarDecl>(D))
5002 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005003
5004 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005005 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005006 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005007 FileID FID;
5008 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005009 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005010 if (FID.isInvalid())
5011 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005012 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005013
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005014 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005015 if (!Info)
5016 Info = new DeclIDInFileInfo();
5017
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005018 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005019 LocDeclIDsTy &Decls = Info->DeclIDs;
5020
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005021 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005022 Decls.push_back(LocDecl);
5023 return;
5024 }
5025
Benjamin Kramer45025c02013-08-24 13:22:59 +00005026 LocDeclIDsTy::iterator I =
5027 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005028
5029 Decls.insert(I, LocDecl);
5030}
5031
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005032void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005033 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005034 Record.push_back(Name.getNameKind());
5035 switch (Name.getNameKind()) {
5036 case DeclarationName::Identifier:
5037 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5038 break;
5039
5040 case DeclarationName::ObjCZeroArgSelector:
5041 case DeclarationName::ObjCOneArgSelector:
5042 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005043 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005044 break;
5045
5046 case DeclarationName::CXXConstructorName:
5047 case DeclarationName::CXXDestructorName:
5048 case DeclarationName::CXXConversionFunctionName:
5049 AddTypeRef(Name.getCXXNameType(), Record);
5050 break;
5051
5052 case DeclarationName::CXXOperatorName:
5053 Record.push_back(Name.getCXXOverloadedOperator());
5054 break;
5055
Alexis Hunt3d221f22009-11-29 07:34:05 +00005056 case DeclarationName::CXXLiteralOperatorName:
5057 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5058 break;
5059
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005060 case DeclarationName::CXXUsingDirective:
5061 // No extra data to emit
5062 break;
5063 }
5064}
Chris Lattnerca025db2010-05-07 21:43:38 +00005065
Richard Smithd08aeb62014-08-28 01:33:39 +00005066unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5067 assert(needsAnonymousDeclarationNumber(D) &&
5068 "expected an anonymous declaration");
5069
5070 // Number the anonymous declarations within this context, if we've not
5071 // already done so.
5072 auto It = AnonymousDeclarationNumbers.find(D);
5073 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005074 auto *DC = D->getLexicalDeclContext();
5075 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5076 AnonymousDeclarationNumbers[ND] = Number;
5077 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005078
5079 It = AnonymousDeclarationNumbers.find(D);
5080 assert(It != AnonymousDeclarationNumbers.end() &&
5081 "declaration not found within its lexical context");
5082 }
5083
5084 return It->second;
5085}
5086
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005087void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005088 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005089 switch (Name.getNameKind()) {
5090 case DeclarationName::CXXConstructorName:
5091 case DeclarationName::CXXDestructorName:
5092 case DeclarationName::CXXConversionFunctionName:
5093 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5094 break;
5095
5096 case DeclarationName::CXXOperatorName:
5097 AddSourceLocation(
5098 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5099 Record);
5100 AddSourceLocation(
5101 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5102 Record);
5103 break;
5104
5105 case DeclarationName::CXXLiteralOperatorName:
5106 AddSourceLocation(
5107 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5108 Record);
5109 break;
5110
5111 case DeclarationName::Identifier:
5112 case DeclarationName::ObjCZeroArgSelector:
5113 case DeclarationName::ObjCOneArgSelector:
5114 case DeclarationName::ObjCMultiArgSelector:
5115 case DeclarationName::CXXUsingDirective:
5116 break;
5117 }
5118}
5119
5120void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005121 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005122 AddDeclarationName(NameInfo.getName(), Record);
5123 AddSourceLocation(NameInfo.getLoc(), Record);
5124 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5125}
5126
5127void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005128 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005129 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005130 Record.push_back(Info.NumTemplParamLists);
5131 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5132 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5133}
5134
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005135void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005136 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005137 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005138 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005139 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005140
5141 // Push each of the NNS's onto a stack for serialization in reverse order.
5142 while (NNS) {
5143 NestedNames.push_back(NNS);
5144 NNS = NNS->getPrefix();
5145 }
5146
5147 Record.push_back(NestedNames.size());
5148 while(!NestedNames.empty()) {
5149 NNS = NestedNames.pop_back_val();
5150 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5151 Record.push_back(Kind);
5152 switch (Kind) {
5153 case NestedNameSpecifier::Identifier:
5154 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5155 break;
5156
5157 case NestedNameSpecifier::Namespace:
5158 AddDeclRef(NNS->getAsNamespace(), Record);
5159 break;
5160
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005161 case NestedNameSpecifier::NamespaceAlias:
5162 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5163 break;
5164
Chris Lattnerca025db2010-05-07 21:43:38 +00005165 case NestedNameSpecifier::TypeSpec:
5166 case NestedNameSpecifier::TypeSpecWithTemplate:
5167 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5168 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5169 break;
5170
5171 case NestedNameSpecifier::Global:
5172 // Don't need to write an associated value.
5173 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005174
5175 case NestedNameSpecifier::Super:
5176 AddDeclRef(NNS->getAsRecordDecl(), Record);
5177 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005178 }
5179 }
5180}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005181
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005182void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5183 RecordDataImpl &Record) {
5184 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005185 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005186 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005187
5188 // Push each of the nested-name-specifiers's onto a stack for
5189 // serialization in reverse order.
5190 while (NNS) {
5191 NestedNames.push_back(NNS);
5192 NNS = NNS.getPrefix();
5193 }
5194
5195 Record.push_back(NestedNames.size());
5196 while(!NestedNames.empty()) {
5197 NNS = NestedNames.pop_back_val();
5198 NestedNameSpecifier::SpecifierKind Kind
5199 = NNS.getNestedNameSpecifier()->getKind();
5200 Record.push_back(Kind);
5201 switch (Kind) {
5202 case NestedNameSpecifier::Identifier:
5203 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5204 AddSourceRange(NNS.getLocalSourceRange(), Record);
5205 break;
5206
5207 case NestedNameSpecifier::Namespace:
5208 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5209 AddSourceRange(NNS.getLocalSourceRange(), Record);
5210 break;
5211
5212 case NestedNameSpecifier::NamespaceAlias:
5213 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5214 AddSourceRange(NNS.getLocalSourceRange(), Record);
5215 break;
5216
5217 case NestedNameSpecifier::TypeSpec:
5218 case NestedNameSpecifier::TypeSpecWithTemplate:
5219 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5220 AddTypeLoc(NNS.getTypeLoc(), Record);
5221 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5222 break;
5223
5224 case NestedNameSpecifier::Global:
5225 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5226 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005227
5228 case NestedNameSpecifier::Super:
5229 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5230 AddSourceRange(NNS.getLocalSourceRange(), Record);
5231 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005232 }
5233 }
5234}
5235
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005236void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005237 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005238 Record.push_back(Kind);
5239 switch (Kind) {
5240 case TemplateName::Template:
5241 AddDeclRef(Name.getAsTemplateDecl(), Record);
5242 break;
5243
5244 case TemplateName::OverloadedTemplate: {
5245 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5246 Record.push_back(OvT->size());
5247 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5248 I != E; ++I)
5249 AddDeclRef(*I, Record);
5250 break;
5251 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005252
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005253 case TemplateName::QualifiedTemplate: {
5254 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5255 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5256 Record.push_back(QualT->hasTemplateKeyword());
5257 AddDeclRef(QualT->getTemplateDecl(), Record);
5258 break;
5259 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005260
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005261 case TemplateName::DependentTemplate: {
5262 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5263 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5264 Record.push_back(DepT->isIdentifier());
5265 if (DepT->isIdentifier())
5266 AddIdentifierRef(DepT->getIdentifier(), Record);
5267 else
5268 Record.push_back(DepT->getOperator());
5269 break;
5270 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005271
5272 case TemplateName::SubstTemplateTemplateParm: {
5273 SubstTemplateTemplateParmStorage *subst
5274 = Name.getAsSubstTemplateTemplateParm();
5275 AddDeclRef(subst->getParameter(), Record);
5276 AddTemplateName(subst->getReplacement(), Record);
5277 break;
5278 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005279
5280 case TemplateName::SubstTemplateTemplateParmPack: {
5281 SubstTemplateTemplateParmPackStorage *SubstPack
5282 = Name.getAsSubstTemplateTemplateParmPack();
5283 AddDeclRef(SubstPack->getParameterPack(), Record);
5284 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5285 break;
5286 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005287 }
5288}
5289
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005290void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005291 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005292 Record.push_back(Arg.getKind());
5293 switch (Arg.getKind()) {
5294 case TemplateArgument::Null:
5295 break;
5296 case TemplateArgument::Type:
5297 AddTypeRef(Arg.getAsType(), Record);
5298 break;
5299 case TemplateArgument::Declaration:
5300 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005301 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005302 break;
5303 case TemplateArgument::NullPtr:
5304 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005305 break;
5306 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005307 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005308 AddTypeRef(Arg.getIntegralType(), Record);
5309 break;
5310 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005311 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5312 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005313 case TemplateArgument::TemplateExpansion:
5314 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005315 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005316 Record.push_back(*NumExpansions + 1);
5317 else
5318 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005319 break;
5320 case TemplateArgument::Expression:
5321 AddStmt(Arg.getAsExpr());
5322 break;
5323 case TemplateArgument::Pack:
5324 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005325 for (const auto &P : Arg.pack_elements())
5326 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005327 break;
5328 }
5329}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005330
5331void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005332ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005333 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005334 assert(TemplateParams && "No TemplateParams!");
5335 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5336 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5337 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5338 Record.push_back(TemplateParams->size());
5339 for (TemplateParameterList::const_iterator
5340 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5341 P != PEnd; ++P)
5342 AddDeclRef(*P, Record);
5343}
5344
5345/// \brief Emit a template argument list.
5346void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005347ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005348 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005349 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005350 Record.push_back(TemplateArgs->size());
5351 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005352 AddTemplateArgument(TemplateArgs->get(i), Record);
5353}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005354
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005355void
5356ASTWriter::AddASTTemplateArgumentListInfo
5357(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5358 assert(ASTTemplArgList && "No ASTTemplArgList!");
5359 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5360 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5361 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5362 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5363 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5364 AddTemplateArgumentLoc(TemplArgs[i], Record);
5365}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005366
5367void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005368ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005369 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005370 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005371 I = Set.begin(), E = Set.end(); I != E; ++I) {
5372 AddDeclRef(I.getDecl(), Record);
5373 Record.push_back(I.getAccess());
5374 }
5375}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005376
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005377void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005378 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005379 Record.push_back(Base.isVirtual());
5380 Record.push_back(Base.isBaseOfClass());
5381 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005382 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005383 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005384 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005385 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5386 : SourceLocation(),
5387 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005388}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005389
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005390void ASTWriter::FlushCXXBaseSpecifiers() {
5391 RecordData Record;
Richard Smithc2bb8182015-03-24 06:36:48 +00005392 unsigned N = CXXBaseSpecifiersToWrite.size();
5393 for (unsigned I = 0; I != N; ++I) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005394 Record.clear();
5395
5396 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005397 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005398 if (Index == CXXBaseSpecifiersOffsets.size())
5399 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5400 else {
5401 if (Index > CXXBaseSpecifiersOffsets.size())
5402 CXXBaseSpecifiersOffsets.resize(Index + 1);
5403 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5404 }
5405
5406 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5407 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5408 Record.push_back(BEnd - B);
5409 for (; B != BEnd; ++B)
5410 AddCXXBaseSpecifier(*B, Record);
5411 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005412
5413 // Flush any expressions that were written as part of the base specifiers.
5414 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005415 }
5416
Richard Smithc2bb8182015-03-24 06:36:48 +00005417 assert(N == CXXBaseSpecifiersToWrite.size() &&
5418 "added more base specifiers while writing base specifiers");
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005419 CXXBaseSpecifiersToWrite.clear();
5420}
5421
Alexis Hunt1d792652011-01-08 20:30:50 +00005422void ASTWriter::AddCXXCtorInitializers(
5423 const CXXCtorInitializer * const *CtorInitializers,
5424 unsigned NumCtorInitializers,
5425 RecordDataImpl &Record) {
5426 Record.push_back(NumCtorInitializers);
5427 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5428 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005429
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005430 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005431 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005432 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005433 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005434 } else if (Init->isDelegatingInitializer()) {
5435 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005436 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005437 } else if (Init->isMemberInitializer()){
5438 Record.push_back(CTOR_INITIALIZER_MEMBER);
5439 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005440 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005441 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5442 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005443 }
Francois Pichetd583da02010-12-04 09:14:42 +00005444
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005445 AddSourceLocation(Init->getMemberLocation(), Record);
5446 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005447 AddSourceLocation(Init->getLParenLoc(), Record);
5448 AddSourceLocation(Init->getRParenLoc(), Record);
5449 Record.push_back(Init->isWritten());
5450 if (Init->isWritten()) {
5451 Record.push_back(Init->getSourceOrder());
5452 } else {
5453 Record.push_back(Init->getNumArrayIndices());
5454 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5455 AddDeclRef(Init->getArrayIndex(i), Record);
5456 }
5457 }
5458}
5459
Richard Smithc2bb8182015-03-24 06:36:48 +00005460void ASTWriter::FlushCXXCtorInitializers() {
5461 RecordData Record;
5462
5463 unsigned N = CXXCtorInitializersToWrite.size();
Daniel Jasperc77b0a12015-03-24 08:06:38 +00005464 (void)N; // Silence unused warning in non-assert builds.
Richard Smithc2bb8182015-03-24 06:36:48 +00005465 for (auto &Init : CXXCtorInitializersToWrite) {
5466 Record.clear();
5467
5468 // Record the offset of this mem-initializer list.
5469 unsigned Index = Init.ID - 1;
5470 if (Index == CXXCtorInitializersOffsets.size())
5471 CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
5472 else {
5473 if (Index > CXXCtorInitializersOffsets.size())
5474 CXXCtorInitializersOffsets.resize(Index + 1);
5475 CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
5476 }
5477
5478 AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
5479 Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
5480
5481 // Flush any expressions that were written as part of the initializers.
5482 FlushStmts();
5483 }
5484
5485 assert(N == CXXCtorInitializersToWrite.size() &&
5486 "added more ctor initializers while writing ctor initializers");
5487 CXXCtorInitializersToWrite.clear();
5488}
5489
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005490void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005491 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005492 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005493 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005494 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005495 Record.push_back(Data.Aggregate);
5496 Record.push_back(Data.PlainOldData);
5497 Record.push_back(Data.Empty);
5498 Record.push_back(Data.Polymorphic);
5499 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005500 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005501 Record.push_back(Data.HasNoNonEmptyBases);
5502 Record.push_back(Data.HasPrivateFields);
5503 Record.push_back(Data.HasProtectedFields);
5504 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005505 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005506 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005507 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005508 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005509 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005510 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5511 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5512 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5513 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5514 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5515 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005516 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005517 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005518 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005519 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005520 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005521 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005522 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005523 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005524 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005525 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005526 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5527 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5528 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5529 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005530 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005531
5532 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005533 if (Data.NumBases > 0)
5534 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5535 Record);
5536
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005537 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5538 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005539 if (Data.NumVBases > 0)
5540 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5541 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005542
Richard Smitha4ba74c2013-08-30 04:46:40 +00005543 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5544 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005545 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005546 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005547
5548 // Add lambda-specific data.
5549 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005550 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005551 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005552 Record.push_back(Lambda.IsGenericLambda);
5553 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005554 Record.push_back(Lambda.NumCaptures);
5555 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005556 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005557 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005558 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005559 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005560 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005561 AddSourceLocation(Capture.getLocation(), Record);
5562 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005563 Record.push_back(Capture.getCaptureKind());
5564 switch (Capture.getCaptureKind()) {
5565 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005566 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005567 break;
5568 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005569 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005570 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005571 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005572 AddDeclRef(Var, Record);
5573 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5574 : SourceLocation(),
5575 Record);
5576 break;
5577 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005578 }
5579 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005580}
5581
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005582void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005583 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005584 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005585 assert(FirstDeclID == NextDeclID &&
5586 FirstTypeID == NextTypeID &&
5587 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005588 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005589 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005590 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005591 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005592
Sebastian Redl07a89a82010-07-30 00:29:29 +00005593 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005594
Richard Smith7f330cd2015-03-18 01:42:29 +00005595 // Note, this will get called multiple times, once one the reader starts up
5596 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005597 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5598 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5599 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005600 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005601 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005602 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005603 NextDeclID = FirstDeclID;
5604 NextTypeID = FirstTypeID;
5605 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005606 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005607 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005608 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005609}
5610
Sebastian Redl539c5062010-08-18 23:57:32 +00005611void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005612 // Always keep the highest ID. See \p TypeRead() for more information.
5613 IdentID &StoredID = IdentifierIDs[II];
5614 if (ID > StoredID)
5615 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005616}
5617
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005618void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005619 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005620 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005621 if (ID > StoredID)
5622 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005623}
5624
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005625void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005626 // Always take the highest-numbered type index. This copes with an interesting
5627 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005628 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005629 // keep the higher-numbered entry so that we can properly write it out to
5630 // the AST file.
5631 TypeIdx &StoredIdx = TypeIdxs[T];
5632 if (Idx.getIndex() >= StoredIdx.getIndex())
5633 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005634}
5635
Sebastian Redl539c5062010-08-18 23:57:32 +00005636void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005637 // Always keep the highest ID. See \p TypeRead() for more information.
5638 SelectorID &StoredID = SelectorIDs[S];
5639 if (ID > StoredID)
5640 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005641}
Douglas Gregor91096292010-10-02 19:29:26 +00005642
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005643void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005644 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005645 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005646 MacroDefinitions[MD] = ID;
5647}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005648
Douglas Gregore37a85a2011-12-02 17:30:13 +00005649void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5650 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5651 SubmoduleIDs[Mod] = ID;
5652}
5653
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005654void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005655 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005656 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005657 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5658 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005659 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005660 // A forward reference was mutated into a definition. Rewrite it.
5661 // FIXME: This happens during template instantiation, should we
5662 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005663 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5664 "completed a tag from another module but not by instantiation?");
5665 DeclUpdates[RD].push_back(
5666 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005667 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005668 }
5669}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005670
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005671void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5672 // TU and namespaces are handled elsewhere.
5673 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5674 return;
5675
Douglas Gregorb3722e22011-09-09 23:01:35 +00005676 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005677 return; // Not a source decl added to a DeclContext from PCH.
5678
Richard Smith0f4e2c42015-08-06 04:23:48 +00005679 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005680 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005681 assert(!WritingAST && "Already writing the AST!");
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005682 UpdatedDeclContexts.insert(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005683 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005684}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005685
5686void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5687 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005688 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005689 return; // Not a source member added to a class from PCH.
5690 if (!isa<CXXMethodDecl>(D))
5691 return; // We are interested in lazily declared implicit methods.
5692
5693 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005694 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005695 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005696 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005697}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005698
Richard Smith564417a2014-03-20 21:47:22 +00005699void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005700 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5701 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005702 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005703 // If we don't already know the exception specification for this redecl
5704 // chain, add an update record for it.
5705 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5706 ->getType()
5707 ->castAs<FunctionProtoType>()
5708 ->getExceptionSpecType()))
5709 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5710 });
Richard Smith564417a2014-03-20 21:47:22 +00005711}
5712
Richard Smith1fa5d642013-05-11 05:45:24 +00005713void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5714 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005715 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005716 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005717 DeclUpdates[D].push_back(
5718 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5719 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005720}
5721
Richard Smithf8134002015-03-10 01:41:22 +00005722void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5723 const FunctionDecl *Delete) {
5724 assert(!WritingAST && "Already writing the AST!");
5725 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005726 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005727 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005728 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5729 });
Richard Smithf8134002015-03-10 01:41:22 +00005730}
5731
Sebastian Redlab238a72011-04-24 16:28:06 +00005732void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005733 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005734 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005735 return; // Declaration not imported from PCH.
5736
Richard Smith4d235792014-08-07 18:53:08 +00005737 // Implicit function decl from a PCH was defined.
5738 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005739}
5740
Richard Smithd28ac5b2014-03-22 23:33:22 +00005741void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5742 assert(!WritingAST && "Already writing the AST!");
5743 if (!D->isFromASTFile())
5744 return;
5745
Richard Smith9e2341d2015-03-23 03:25:59 +00005746 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005747}
5748
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005749void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005750 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005751 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005752 return;
5753
5754 // Since the actual instantiation is delayed, this really means that we need
5755 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005756 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005757 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5758 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005759}
5760
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005761void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5762 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005763 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005764 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005765 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005766
5767 assert(IFD->getDefinition() && "Category on a class without a definition?");
5768 ObjCClassesWithCategories.insert(
5769 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005770}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005771
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005772
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005773void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5774 const ObjCPropertyDecl *OrigProp,
5775 const ObjCCategoryDecl *ClassExt) {
5776 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5777 if (!D)
5778 return;
5779
5780 assert(!WritingAST && "Already writing the AST!");
5781 if (!D->isFromASTFile())
5782 return; // Declaration not imported from PCH.
5783
5784 RewriteDecl(D);
5785}
Eli Friedman276dd182013-09-05 00:02:25 +00005786
5787void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5788 assert(!WritingAST && "Already writing the AST!");
5789 if (!D->isFromASTFile())
5790 return;
5791
Aaron Ballman4f45b712014-03-21 15:22:56 +00005792 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005793}
Alexey Bataev97720002014-11-11 04:05:39 +00005794
5795void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5796 assert(!WritingAST && "Already writing the AST!");
5797 if (!D->isFromASTFile())
5798 return;
5799
5800 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5801}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005802
Richard Smith4caa4492015-05-15 02:34:32 +00005803void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00005804 assert(!WritingAST && "Already writing the AST!");
5805 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005806 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005807}
Alex Denisovfde64952015-06-26 05:28:36 +00005808
5809void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5810 const RecordDecl *Record) {
5811 assert(!WritingAST && "Already writing the AST!");
5812 if (!Record->isFromASTFile())
5813 return;
5814 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5815}