blob: 331fa385905aba91d022c7909bcd5adca35dcb07 [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);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000884 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000885 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000886 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000887 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +0000888
889 BLOCK(OPTIONS_BLOCK);
890 RECORD(LANGUAGE_OPTIONS);
891 RECORD(TARGET_OPTIONS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000892 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000893 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000894 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000895 RECORD(PREPROCESSOR_OPTIONS);
896
Douglas Gregor108cb222012-10-19 00:45:00 +0000897 BLOCK(INPUT_FILES_BLOCK);
898 RECORD(INPUT_FILE);
899
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000900 // AST Top-Level Block.
901 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000902 RECORD(TYPE_OFFSET);
903 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000904 RECORD(IDENTIFIER_OFFSET);
905 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000906 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000907 RECORD(SPECIAL_TYPES);
908 RECORD(STATISTICS);
909 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000910 RECORD(SELECTOR_OFFSETS);
911 RECORD(METHOD_POOL);
912 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000913 RECORD(SOURCE_LOCATION_OFFSETS);
914 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000915 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +0000916 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000917 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +0000918 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000919 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000920 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000921 RECORD(SEMA_DECL_REFS);
922 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
923 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
924 RECORD(DECL_REPLACEMENTS);
925 RECORD(UPDATE_VISIBLE);
926 RECORD(DECL_UPDATE_OFFSETS);
927 RECORD(DECL_UPDATES);
928 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
929 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000930 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000931 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000932 RECORD(FP_PRAGMA_OPTIONS);
933 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000934 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000935 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000936 RECORD(MODULE_OFFSET_MAP);
937 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000938 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000939 RECORD(FILE_SORTED_DECLS);
940 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000941 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000942 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +0000943 RECORD(INTERESTING_IDENTIFIERS);
944 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000945 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000946 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +0000947 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
948 RECORD(CXX_CTOR_INITIALIZERS_OFFSETS);
949 RECORD(DELETE_EXPRS_TO_ANALYZE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000950
Chris Lattner28fa4e62009-04-26 22:26:21 +0000951 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000952 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000953 RECORD(SM_SLOC_FILE_ENTRY);
954 RECORD(SM_SLOC_BUFFER_ENTRY);
955 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000956 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000957
Chris Lattner28fa4e62009-04-26 22:26:21 +0000958 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000959 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +0000960 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000961 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +0000962 RECORD(PP_MACRO_OBJECT_LIKE);
963 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000964 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +0000965
Richard Smithfa715652015-08-31 22:43:10 +0000966 // Submodule Block.
967 BLOCK(SUBMODULE_BLOCK);
968 RECORD(SUBMODULE_METADATA);
969 RECORD(SUBMODULE_DEFINITION);
970 RECORD(SUBMODULE_UMBRELLA_HEADER);
971 RECORD(SUBMODULE_HEADER);
972 RECORD(SUBMODULE_TOPHEADER);
973 RECORD(SUBMODULE_UMBRELLA_DIR);
974 RECORD(SUBMODULE_IMPORTS);
975 RECORD(SUBMODULE_EXPORTS);
976 RECORD(SUBMODULE_REQUIRES);
977 RECORD(SUBMODULE_EXCLUDED_HEADER);
978 RECORD(SUBMODULE_LINK_LIBRARY);
979 RECORD(SUBMODULE_CONFIG_MACRO);
980 RECORD(SUBMODULE_CONFLICT);
981 RECORD(SUBMODULE_PRIVATE_HEADER);
982 RECORD(SUBMODULE_TEXTUAL_HEADER);
983 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
984
985 // Comments Block.
986 BLOCK(COMMENTS_BLOCK);
987 RECORD(COMMENTS_RAW_COMMENT);
988
Douglas Gregor12bfa382009-10-17 00:13:19 +0000989 // Decls and Types block.
990 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000991 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000992 RECORD(TYPE_COMPLEX);
993 RECORD(TYPE_POINTER);
994 RECORD(TYPE_BLOCK_POINTER);
995 RECORD(TYPE_LVALUE_REFERENCE);
996 RECORD(TYPE_RVALUE_REFERENCE);
997 RECORD(TYPE_MEMBER_POINTER);
998 RECORD(TYPE_CONSTANT_ARRAY);
999 RECORD(TYPE_INCOMPLETE_ARRAY);
1000 RECORD(TYPE_VARIABLE_ARRAY);
1001 RECORD(TYPE_VECTOR);
1002 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001003 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001004 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001005 RECORD(TYPE_TYPEDEF);
1006 RECORD(TYPE_TYPEOF_EXPR);
1007 RECORD(TYPE_TYPEOF);
1008 RECORD(TYPE_RECORD);
1009 RECORD(TYPE_ENUM);
1010 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001011 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001012 RECORD(TYPE_DECLTYPE);
1013 RECORD(TYPE_ELABORATED);
1014 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1015 RECORD(TYPE_UNRESOLVED_USING);
1016 RECORD(TYPE_INJECTED_CLASS_NAME);
1017 RECORD(TYPE_OBJC_OBJECT);
1018 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1019 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1020 RECORD(TYPE_DEPENDENT_NAME);
1021 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1022 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1023 RECORD(TYPE_PAREN);
1024 RECORD(TYPE_PACK_EXPANSION);
1025 RECORD(TYPE_ATTRIBUTED);
1026 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001027 RECORD(TYPE_AUTO);
1028 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001029 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001030 RECORD(TYPE_DECAYED);
1031 RECORD(TYPE_ADJUSTED);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001032 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001033 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001034 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001035 RECORD(DECL_ENUM);
1036 RECORD(DECL_RECORD);
1037 RECORD(DECL_ENUM_CONSTANT);
1038 RECORD(DECL_FUNCTION);
1039 RECORD(DECL_OBJC_METHOD);
1040 RECORD(DECL_OBJC_INTERFACE);
1041 RECORD(DECL_OBJC_PROTOCOL);
1042 RECORD(DECL_OBJC_IVAR);
1043 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001044 RECORD(DECL_OBJC_CATEGORY);
1045 RECORD(DECL_OBJC_CATEGORY_IMPL);
1046 RECORD(DECL_OBJC_IMPLEMENTATION);
1047 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1048 RECORD(DECL_OBJC_PROPERTY);
1049 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001050 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001051 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001052 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001053 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001054 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001055 RECORD(DECL_FILE_SCOPE_ASM);
1056 RECORD(DECL_BLOCK);
1057 RECORD(DECL_CONTEXT_LEXICAL);
1058 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001059 RECORD(DECL_NAMESPACE);
1060 RECORD(DECL_NAMESPACE_ALIAS);
1061 RECORD(DECL_USING);
1062 RECORD(DECL_USING_SHADOW);
1063 RECORD(DECL_USING_DIRECTIVE);
1064 RECORD(DECL_UNRESOLVED_USING_VALUE);
1065 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1066 RECORD(DECL_LINKAGE_SPEC);
1067 RECORD(DECL_CXX_RECORD);
1068 RECORD(DECL_CXX_METHOD);
1069 RECORD(DECL_CXX_CONSTRUCTOR);
1070 RECORD(DECL_CXX_DESTRUCTOR);
1071 RECORD(DECL_CXX_CONVERSION);
1072 RECORD(DECL_ACCESS_SPEC);
1073 RECORD(DECL_FRIEND);
1074 RECORD(DECL_FRIEND_TEMPLATE);
1075 RECORD(DECL_CLASS_TEMPLATE);
1076 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1077 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001078 RECORD(DECL_VAR_TEMPLATE);
1079 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1080 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001081 RECORD(DECL_FUNCTION_TEMPLATE);
1082 RECORD(DECL_TEMPLATE_TYPE_PARM);
1083 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1084 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1085 RECORD(DECL_STATIC_ASSERT);
1086 RECORD(DECL_CXX_BASE_SPECIFIERS);
1087 RECORD(DECL_INDIRECTFIELD);
1088 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1089
Douglas Gregor03412ba2011-06-03 02:27:19 +00001090 // Statements and Exprs can occur in the Decls and Types block.
1091 AddStmtsExprs(Stream, Record);
1092
Douglas Gregor92a96f52011-02-08 21:58:10 +00001093 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001094 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001095 RECORD(PPD_MACRO_DEFINITION);
1096 RECORD(PPD_INCLUSION_DIRECTIVE);
1097
Chris Lattner28fa4e62009-04-26 22:26:21 +00001098#undef RECORD
1099#undef BLOCK
1100 Stream.ExitBlock();
1101}
1102
Richard Smith54cc3c22014-12-11 20:50:24 +00001103/// \brief Prepares a path for being written to an AST file by converting it
1104/// to an absolute path and removing nested './'s.
1105///
1106/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001107static bool cleanPathForOutput(FileManager &FileMgr,
1108 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001109 bool Changed = FileMgr.makeAbsolutePath(Path);
1110 return Changed | FileMgr.removeDotPaths(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001111}
1112
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001113/// \brief Adjusts the given filename to only write out the portion of the
1114/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001115///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001116/// \param Filename the file name to adjust.
1117///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001118/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1119/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001120///
1121/// \returns either the original filename (if it needs no adjustment) or the
1122/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001123static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001124adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001125 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001126
Richard Smith7ed1bc92014-12-05 22:42:13 +00001127 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001128 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001129
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001130 // Verify that the filename and the system root have the same prefix.
1131 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001132 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1133 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001134 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001135
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001136 // We hit the end of the filename before we hit the end of the system root.
1137 if (!Filename[Pos])
1138 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001139
Richard Smith7ed1bc92014-12-05 22:42:13 +00001140 // If there's not a path separator at the end of the base directory nor
1141 // immediately after it, then this isn't within the base directory.
1142 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1143 if (!llvm::sys::path::is_separator(BaseDir.back()))
1144 return Filename;
1145 } else {
1146 // If the file name has a '/' at the current position, skip over the '/'.
1147 // We distinguish relative paths from absolute paths by the
1148 // absence of '/' at the beginning of relative paths.
1149 //
1150 // FIXME: This is wrong. We distinguish them by asking if the path is
1151 // absolute, which isn't the same thing. And there might be multiple '/'s
1152 // in a row. Use a better mechanism to indicate whether we have emitted an
1153 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001154 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001157 return Filename + Pos;
1158}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001159
Ben Langmuir487ea142014-10-23 18:05:36 +00001160static ASTFileSignature getSignature() {
1161 while (1) {
1162 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1163 return S;
1164 // Rely on GetRandomNumber to eventually return non-zero...
1165 }
1166}
1167
Douglas Gregor112b9072012-10-18 05:31:06 +00001168/// \brief Write the control block.
Adrian Prantladbd2b12015-09-22 23:26:31 +00001169uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1170 ASTContext &Context,
1171 StringRef isysroot,
1172 const std::string &OutputFile) {
1173 ASTFileSignature Signature = 0;
1174
Douglas Gregorbfbde532009-04-10 21:16:55 +00001175 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001176 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001177 RecordData Record;
1178
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001179 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001180 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1181 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1182 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1183 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1184 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1185 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1186 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001187 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001188 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1189 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1190 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001191 assert((!WritingModule || isysroot.empty()) &&
1192 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001193 {
1194 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1195 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1196 !isysroot.empty(), IncludeTimestamps,
1197 ASTHasCompilerErrors};
1198 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1199 getClangFullRepositoryVersion());
1200 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001201 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001202 // For implicit modules we output a signature that we can use to ensure
1203 // duplicate module builds don't collide in the cache as their output order
1204 // is non-deterministic.
1205 // FIXME: Remove this when output is deterministic.
1206 if (Context.getLangOpts().ImplicitModules) {
Adrian Prantladbd2b12015-09-22 23:26:31 +00001207 Signature = getSignature();
1208 RecordData::value_type Record[] = {Signature};
Chandler Carruth885e78c2015-03-24 21:18:10 +00001209 Stream.EmitRecord(SIGNATURE, Record);
1210 }
1211
Richard Smith7ed1bc92014-12-05 22:42:13 +00001212 // Module name
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001213 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1214 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1216 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00001217 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001218 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1219 }
1220
Richard Smith7ed1bc92014-12-05 22:42:13 +00001221 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001222 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001223 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001224
1225 // If the home of the module is the current working directory, then we
1226 // want to pick up the cwd of the build process loading the module, not
1227 // our cwd, when we load this module.
1228 if (!PP.getHeaderSearchInfo()
1229 .getHeaderSearchOpts()
1230 .ModuleMapFileHomeIsCwd ||
1231 WritingModule->Directory->getName() != StringRef(".")) {
1232 // Module directory.
1233 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1234 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1236 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1237
Mehdi Amini57a41912015-09-10 01:46:39 +00001238 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001239 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) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +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();
Mehdi Amini5ae4a852015-09-09 20:35:37 +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
Richard Smith0516b182015-09-08 19:40:14 +00001291 // Write the options block.
1292 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1293
Douglas Gregor112b9072012-10-18 05:31:06 +00001294 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001295 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001296 const LangOptions &LangOpts = Context.getLangOpts();
1297#define LANGOPT(Name, Bits, Default, Description) \
1298 Record.push_back(LangOpts.Name);
1299#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1300 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001301#include "clang/Basic/LangOptions.def"
1302#define SANITIZER(NAME, ID) \
1303 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001304#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001305
Ben Langmuircd98cb72015-06-23 18:20:18 +00001306 Record.push_back(LangOpts.ModuleFeatures.size());
1307 for (StringRef Feature : LangOpts.ModuleFeatures)
1308 AddString(Feature, Record);
1309
Douglas Gregor112b9072012-10-18 05:31:06 +00001310 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1311 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001312
1313 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001314
1315 // Comment options.
1316 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1317 for (CommentOptions::BlockCommandNamesTy::const_iterator
1318 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1319 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1320 I != IEnd; ++I) {
1321 AddString(*I, Record);
1322 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001323 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001324
Douglas Gregor112b9072012-10-18 05:31:06 +00001325 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1326
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001327 // Target options.
1328 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001329 const TargetInfo &Target = Context.getTargetInfo();
1330 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001331 AddString(TargetOpts.Triple, Record);
1332 AddString(TargetOpts.CPU, Record);
1333 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001334 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1335 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1336 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1337 }
1338 Record.push_back(TargetOpts.Features.size());
1339 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1340 AddString(TargetOpts.Features[I], Record);
1341 }
1342 Stream.EmitRecord(TARGET_OPTIONS, Record);
1343
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001344 // Diagnostic options.
1345 Record.clear();
1346 const DiagnosticOptions &DiagOpts
1347 = Context.getDiagnostics().getDiagnosticOptions();
1348#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1349#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1350 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1351#include "clang/Basic/DiagnosticOptions.def"
1352 Record.push_back(DiagOpts.Warnings.size());
1353 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1354 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001355 Record.push_back(DiagOpts.Remarks.size());
1356 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1357 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001358 // Note: we don't serialize the log or serialization file names, because they
1359 // are generally transient files and will almost always be overridden.
1360 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1361
Douglas Gregorc6317db2012-10-24 15:49:58 +00001362 // File system options.
1363 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001364 const FileSystemOptions &FSOpts =
1365 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001366 AddString(FSOpts.WorkingDir, Record);
1367 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1368
Douglas Gregor2d302362012-10-24 16:50:34 +00001369 // Header search options.
1370 Record.clear();
1371 const HeaderSearchOptions &HSOpts
1372 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1373 AddString(HSOpts.Sysroot, Record);
1374
1375 // Include entries.
1376 Record.push_back(HSOpts.UserEntries.size());
1377 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1378 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1379 AddString(Entry.Path, Record);
1380 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001381 Record.push_back(Entry.IsFramework);
1382 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001383 }
1384
1385 // System header prefixes.
1386 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1387 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1388 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1389 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1390 }
1391
1392 AddString(HSOpts.ResourceDir, Record);
1393 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001394 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001395 Record.push_back(HSOpts.DisableModuleHash);
1396 Record.push_back(HSOpts.UseBuiltinIncludes);
1397 Record.push_back(HSOpts.UseStandardSystemIncludes);
1398 Record.push_back(HSOpts.UseStandardCXXIncludes);
1399 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001400 // Write out the specific module cache path that contains the module files.
1401 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001402 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1403
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001404 // Preprocessor options.
1405 Record.clear();
1406 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1407
1408 // Macro definitions.
1409 Record.push_back(PPOpts.Macros.size());
1410 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1411 AddString(PPOpts.Macros[I].first, Record);
1412 Record.push_back(PPOpts.Macros[I].second);
1413 }
1414
1415 // Includes
1416 Record.push_back(PPOpts.Includes.size());
1417 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1418 AddString(PPOpts.Includes[I], Record);
1419
1420 // Macro includes
1421 Record.push_back(PPOpts.MacroIncludes.size());
1422 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1423 AddString(PPOpts.MacroIncludes[I], Record);
1424
Douglas Gregorb6368752012-10-24 23:41:50 +00001425 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001426 // Detailed record is important since it is used for the module cache hash.
1427 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001428 AddString(PPOpts.ImplicitPCHInclude, Record);
1429 AddString(PPOpts.ImplicitPTHInclude, Record);
1430 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1431 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1432
Richard Smith0516b182015-09-08 19:40:14 +00001433 // Leave the options block.
1434 Stream.ExitBlock();
1435
Douglas Gregora3b20262011-05-06 21:43:30 +00001436 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001437 SourceManager &SM = Context.getSourceManager();
1438 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1439 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001440 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1441 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001442 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1443 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1444
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001445 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001446 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001447 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001448 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001449 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001450
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001451 Record.clear();
1452 Record.push_back(SM.getMainFileID().getOpaqueValue());
1453 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1454
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001455 // Original PCH directory
1456 if (!OutputFile.empty() && OutputFile != "-") {
1457 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1458 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1460 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1461
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001462 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001463
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001464 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001465 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1466
Mehdi Amini57a41912015-09-10 01:46:39 +00001467 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001468 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1469 }
1470
Douglas Gregor49491f72013-03-15 22:15:07 +00001471 WriteInputFiles(Context.SourceMgr,
1472 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001473 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001474 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00001475 return Signature;
Douglas Gregor72be3902012-10-19 00:38:02 +00001476}
1477
Douglas Gregor49491f72013-03-15 22:15:07 +00001478namespace {
1479 /// \brief An input file.
1480 struct InputFileEntry {
1481 const FileEntry *File;
1482 bool IsSystemFile;
1483 bool BufferOverridden;
1484 };
1485}
1486
1487void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1488 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001489 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001490 using namespace llvm;
1491 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001492
Douglas Gregor72be3902012-10-19 00:38:02 +00001493 // Create input-file abbreviation.
1494 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1495 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001496 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001497 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1498 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001499 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001500 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1501 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1502
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001503 // Get all ContentCache objects for files, sorted by whether the file is a
1504 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001505 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001506 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1507 // Get this source location entry.
1508 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001509 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001510
1511 // We only care about file entries that were not overridden.
1512 if (!SLoc->isFile())
1513 continue;
1514 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001515 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001516 continue;
1517
Douglas Gregor49491f72013-03-15 22:15:07 +00001518 InputFileEntry Entry;
1519 Entry.File = Cache->OrigEntry;
1520 Entry.IsSystemFile = Cache->IsSystemFile;
1521 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001522 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001523 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001524 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001525 SortedFiles.push_front(Entry);
1526 }
1527
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001528 unsigned UserFilesNum = 0;
1529 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001530 std::vector<uint64_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001531 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001532 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001533 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001534
Douglas Gregor49491f72013-03-15 22:15:07 +00001535 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001536 if (InputFileID != 0)
1537 continue; // already recorded this file.
1538
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001539 // Record this entry's offset.
1540 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001541
1542 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001543
Douglas Gregor49491f72013-03-15 22:15:07 +00001544 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001545 ++UserFilesNum;
1546
Douglas Gregor72be3902012-10-19 00:38:02 +00001547 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001548 // And whether this file was overridden.
1549 RecordData::value_type Record[] = {
1550 INPUT_FILE, InputFileOffsets.size(), (uint64_t)Entry.File->getSize(),
1551 (uint64_t)getTimestampForOutput(Entry.File), 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.
Mehdi Amini57a41912015-09-10 01:46:39 +00001568 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1569 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001570 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001571}
1572
Douglas Gregora7f71a92009-04-10 03:52:48 +00001573//===----------------------------------------------------------------------===//
1574// Source Manager Serialization
1575//===----------------------------------------------------------------------===//
1576
1577/// \brief Create an abbreviation for the SLocEntry that refers to a
1578/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001579static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001580 using namespace llvm;
1581 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001582 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001587 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001592 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001593}
1594
1595/// \brief Create an abbreviation for the SLocEntry that refers to a
1596/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001597static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001598 using namespace llvm;
1599 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001600 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001606 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001607}
1608
1609/// \brief Create an abbreviation for the SLocEntry that refers to a
1610/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001611static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001612 using namespace llvm;
1613 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001614 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001616 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001617}
1618
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001619/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1620/// expansion.
1621static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001622 using namespace llvm;
1623 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001624 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001630 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001631}
1632
Douglas Gregor09b69892011-02-10 17:09:37 +00001633namespace {
1634 // Trait used for the on-disk hash table of header search information.
1635 class HeaderFileInfoTrait {
1636 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001637 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001638
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001639 // Keep track of the framework names we've used during serialization.
1640 SmallVector<char, 128> FrameworkStringData;
1641 llvm::StringMap<unsigned> FrameworkNameOffset;
1642
Douglas Gregor09b69892011-02-10 17:09:37 +00001643 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001644 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1645 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001646
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001647 struct key_type {
1648 const FileEntry *FE;
1649 const char *Filename;
1650 };
1651 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001652
1653 typedef HeaderFileInfo data_type;
1654 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001655 typedef unsigned hash_value_type;
1656 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001657
Richard Smithe75ee0f2015-08-17 07:13:32 +00001658 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001659 // The hash is based only on size/time of the file, so that the reader can
1660 // match even when symlinking or excess path elements ("foo/../", "../")
1661 // change the form of the name. However, complete path is still the key.
1662 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001663 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001664 }
1665
1666 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001667 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001668 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001669 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001670 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001671 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001672 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001673 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1674 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1675 DataLen += 4;
1676 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001677 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001678 }
1679
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001680 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001681 using namespace llvm::support;
1682 endian::Writer<little> LE(Out);
1683 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001684 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001685 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001686 KeyLen -= 8;
1687 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001688 }
1689
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001690 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001691 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001692 using namespace llvm::support;
1693 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001694 uint64_t Start = Out.tell(); (void)Start;
1695
Richard Smith386bb072015-08-18 23:42:23 +00001696 unsigned char Flags = (Data.isImport << 4)
1697 | (Data.isPragmaOnce << 3)
1698 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001699 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001700 LE.write<uint8_t>(Flags);
1701 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001702
1703 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001704 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001705 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001706 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001707
1708 unsigned Offset = 0;
1709 if (!Data.Framework.empty()) {
1710 // If this header refers into a framework, save the framework name.
1711 llvm::StringMap<unsigned>::iterator Pos
1712 = FrameworkNameOffset.find(Data.Framework);
1713 if (Pos == FrameworkNameOffset.end()) {
1714 Offset = FrameworkStringData.size() + 1;
1715 FrameworkStringData.append(Data.Framework.begin(),
1716 Data.Framework.end());
1717 FrameworkStringData.push_back(0);
1718
1719 FrameworkNameOffset[Data.Framework] = Offset;
1720 } else
1721 Offset = Pos->second;
1722 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001723 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001724
Richard Smith386bb072015-08-18 23:42:23 +00001725 // FIXME: If the header is excluded, we should write out some
1726 // record of that fact.
1727 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1728 if (uint32_t ModID =
1729 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1730 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1731 assert((Value >> 2) == ModID && "overflow in header module info");
1732 LE.write<uint32_t>(Value);
1733 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001734 }
1735
Douglas Gregor09b69892011-02-10 17:09:37 +00001736 assert(Out.tell() - Start == DataLen && "Wrong data length");
1737 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001738
1739 const char *strings_begin() const { return FrameworkStringData.begin(); }
1740 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001741 };
1742} // end anonymous namespace
1743
1744/// \brief Write the header search block for the list of files that
1745///
1746/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001747void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001748 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001749 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1750
1751 if (FilesByUID.size() > HS.header_file_size())
1752 FilesByUID.resize(HS.header_file_size());
1753
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001754 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001755 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001756 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001757 unsigned NumHeaderSearchEntries = 0;
1758 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1759 const FileEntry *File = FilesByUID[UID];
1760 if (!File)
1761 continue;
1762
Richard Smith386bb072015-08-18 23:42:23 +00001763 // Get the file info. This will load info from the external source if
1764 // necessary. Skip emitting this file if we have no information on it
1765 // as a header file (in which case HFI will be null) or if it hasn't
1766 // changed since it was loaded. Also skip it if it's for a modular header
1767 // from a different module; in that case, we rely on the module(s)
1768 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001769 const HeaderFileInfo *HFI =
1770 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1771 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001772 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001773
Richard Smith7ed1bc92014-12-05 22:42:13 +00001774 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001775 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001776 SmallString<128> FilenameTmp(Filename);
1777 if (PreparePathForOutput(FilenameTmp)) {
1778 // If we performed any translation on the file name at all, we need to
1779 // save this string, since the generator will refer to it later.
1780 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001781 SavedStrings.push_back(Filename);
1782 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001783
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001784 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00001785 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001786 ++NumHeaderSearchEntries;
1787 }
1788
1789 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001790 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001791 uint32_t BucketOffset;
1792 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001793 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001794 llvm::raw_svector_ostream Out(TableData);
1795 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001796 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001797 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1798 }
1799
1800 // Create a blob abbreviation
1801 using namespace llvm;
1802 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1803 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1808 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1809
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001810 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00001811 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1812 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001813 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00001814 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00001815
1816 // Free all of the strings we had to duplicate.
1817 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001818 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001819}
1820
Douglas Gregora7f71a92009-04-10 03:52:48 +00001821/// \brief Writes the block containing the serialized form of the
1822/// source manager.
1823///
1824/// TODO: We should probably use an on-disk hash table (stored in a
1825/// blob), indexed based on the file name, so that we only create
1826/// entries for files that we actually need. In the common case (no
1827/// errors), we probably won't have to create file entries for any of
1828/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001829void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001830 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001831 RecordData Record;
1832
Chris Lattner0910e3b2009-04-10 17:16:57 +00001833 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001834 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001835
1836 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001837 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1838 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1839 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001840 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001841
Douglas Gregor258ae542009-04-27 06:38:32 +00001842 // Write out the source location entry table. We skip the first
1843 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001844 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001845 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001846 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1847 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001848 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001849 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001850 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001851 FileID FID = FileID::get(I);
1852 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001853
Douglas Gregor258ae542009-04-27 06:38:32 +00001854 // Record the offset of this source-location entry.
1855 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1856
1857 // Figure out which record code to use.
1858 unsigned Code;
1859 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001860 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1861 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001862 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001863 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001864 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001865 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001866 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001867 Record.clear();
1868 Record.push_back(Code);
1869
Douglas Gregor925296b2011-07-19 16:10:42 +00001870 // Starting offset of this entry within this module, so skip the dummy.
1871 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001872 if (SLoc->isFile()) {
1873 const SrcMgr::FileInfo &File = SLoc->getFile();
1874 Record.push_back(File.getIncludeLoc().getRawEncoding());
1875 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1876 Record.push_back(File.hasLineDirectives());
1877
1878 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001879 if (Content->OrigEntry) {
1880 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001881 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001882
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001883 // The source location entry is a file. Emit input file ID.
1884 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1885 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001886
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001887 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001888
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001889 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001890 if (FDI != FileDeclIDs.end()) {
1891 Record.push_back(FDI->second->FirstDeclIndex);
1892 Record.push_back(FDI->second->DeclIDs.size());
1893 } else {
1894 Record.push_back(0);
1895 Record.push_back(0);
1896 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001897
1898 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
1899
Douglas Gregor9dc32122011-11-16 20:05:18 +00001900 if (Content->BufferOverridden) {
Mehdi Amini57a41912015-09-10 01:46:39 +00001901 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
Douglas Gregor9dc32122011-11-16 20:05:18 +00001902 const llvm::MemoryBuffer *Buffer
1903 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1904 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1905 StringRef(Buffer->getBufferStart(),
1906 Buffer->getBufferSize() + 1));
1907 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001908 } else {
1909 // The source location entry is a buffer. The blob associated
1910 // with this entry contains the contents of the buffer.
1911
1912 // We add one to the size so that we capture the trailing NULL
1913 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1914 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001915 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001916 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001917 const char *Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001918 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001919 StringRef(Name, strlen(Name) + 1));
Mehdi Amini57a41912015-09-10 01:46:39 +00001920 RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
Douglas Gregor258ae542009-04-27 06:38:32 +00001921 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001922 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001923 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001924
Douglas Gregor925296b2011-07-19 16:10:42 +00001925 if (strcmp(Name, "<built-in>") == 0) {
1926 PreloadSLocs.push_back(SLocEntryOffsets.size());
1927 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001928 }
1929 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001930 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001931 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001932 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1933 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001934 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1935 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001936
1937 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001938 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001939 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001940 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001941 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001942 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001943 }
1944 }
1945
Douglas Gregor8f45df52009-04-16 22:23:12 +00001946 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001947
1948 if (SLocEntryOffsets.empty())
1949 return;
1950
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001951 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001952 // table is used for lazily loading source-location information.
1953 using namespace llvm;
1954 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001955 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1959 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00001960 {
1961 RecordData::value_type Record[] = {
1962 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
1963 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
1964 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
1965 bytes(SLocEntryOffsets));
1966 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001967 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001968 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001969 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001970
1971 // Write the line table. It depends on remapping working, so it must come
1972 // after the source location offsets.
1973 if (SourceMgr.hasLineTable()) {
1974 LineTableInfo &LineTable = SourceMgr.getLineTable();
1975
1976 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00001977
1978 // Emit the needed file names.
1979 llvm::DenseMap<int, int> FilenameMap;
1980 for (const auto &L : LineTable) {
1981 if (L.first.ID < 0)
1982 continue;
1983 for (auto &LE : L.second) {
1984 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
1985 FilenameMap.size())).second)
1986 AddPath(LineTable.getFilename(LE.FilenameID), Record);
1987 }
1988 }
1989 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00001990
1991 // Emit the line entries
1992 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1993 L != LEnd; ++L) {
1994 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001995 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001996 continue;
1997
1998 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001999 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002000
2001 // Emit the line entries
2002 Record.push_back(L->second.size());
2003 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
2004 LEEnd = L->second.end();
2005 LE != LEEnd; ++LE) {
2006 Record.push_back(LE->FileOffset);
2007 Record.push_back(LE->LineNo);
Richard Smith63078492015-09-01 07:41:55 +00002008 Record.push_back(FilenameMap[LE->FilenameID]);
Douglas Gregor925296b2011-07-19 16:10:42 +00002009 Record.push_back((unsigned)LE->FileKind);
2010 Record.push_back(LE->IncludeOffset);
2011 }
2012 }
Richard Smith63078492015-09-01 07:41:55 +00002013
Douglas Gregor925296b2011-07-19 16:10:42 +00002014 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2015 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002016}
2017
Douglas Gregorc5046832009-04-27 18:38:38 +00002018//===----------------------------------------------------------------------===//
2019// Preprocessor Serialization
2020//===----------------------------------------------------------------------===//
2021
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002022static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2023 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002024 if (MacroInfo *MI = MD->getMacroInfo())
2025 if (MI->isBuiltinMacro())
2026 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002027
2028 if (IsModule) {
2029 SourceLocation Loc = MD->getLocation();
2030 if (Loc.isInvalid())
2031 return true;
2032 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2033 return true;
2034 }
2035
2036 return false;
2037}
2038
Chris Lattnereeffaef2009-04-10 17:15:23 +00002039/// \brief Writes the block containing the serialized form of the
2040/// preprocessor.
2041///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002042void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002043 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2044 if (PPRec)
2045 WritePreprocessorDetail(*PPRec);
2046
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002047 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002048 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002049
Chris Lattner0af3ba12009-04-13 01:29:17 +00002050 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2051 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002052 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002053 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002054 }
2055
2056 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002057 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002058
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002059 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002060 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002061 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002062 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002063
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002064 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002065 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002066
Richard Smithee977932015-05-01 21:22:17 +00002067 // Construct the list of identifiers with macro directives that need to be
2068 // serialized.
2069 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2070 for (auto &Id : PP.getIdentifierTable())
2071 if (Id.second->hadMacroDefinition() &&
2072 (!Id.second->isFromAST() ||
2073 Id.second->hasChangedSinceDeserialization()))
2074 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002075 // Sort the set of macro definitions that need to be serialized by the
2076 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002077 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2078 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002079
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002080 // Emit the macro directives as a list and associate the offset with the
2081 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002082 for (const IdentifierInfo *Name : MacroIdentifiers) {
2083 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002084 auto StartOffset = Stream.GetCurrentBitNo();
2085
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002086 // Emit the macro directives in reverse source order.
2087 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002088 // Once we hit an ignored macro, we're done: the rest of the chain
2089 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002090 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002091 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002092
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002093 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002094 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002095 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002096 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002097 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002098 Record.push_back(VisMD->isPublic());
2099 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002100 }
Richard Smithd7329392015-04-21 21:46:32 +00002101
Richard Smithb8b2ed62015-04-23 18:18:26 +00002102 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002103 bool EmittedModuleMacros = false;
Richard Smithb8b2ed62015-04-23 18:18:26 +00002104 if (IsModule) {
2105 auto Leafs = PP.getLeafModuleMacros(Name);
2106 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2107 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2108 while (!Worklist.empty()) {
2109 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002110
Richard Smithb8b2ed62015-04-23 18:18:26 +00002111 // Emit a record indicating this submodule exports this macro.
2112 ModuleMacroRecord.push_back(
2113 getSubmoduleID(Macro->getOwningModule()));
Richard Smithee977932015-05-01 21:22:17 +00002114 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
Richard Smithb8b2ed62015-04-23 18:18:26 +00002115 for (auto *M : Macro->overrides())
2116 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002117
Richard Smithb8b2ed62015-04-23 18:18:26 +00002118 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2119 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002120
Richard Smithb8b2ed62015-04-23 18:18:26 +00002121 // Enqueue overridden macros once we've visited all their ancestors.
2122 for (auto *M : Macro->overrides())
2123 if (++Visits[M] == M->getNumOverridingMacros())
2124 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002125
2126 EmittedModuleMacros = true;
Richard Smithd7329392015-04-21 21:46:32 +00002127 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002128 }
Richard Smithd7329392015-04-21 21:46:32 +00002129
Richard Smithee977932015-05-01 21:22:17 +00002130 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002131 continue;
2132
Richard Smithd7329392015-04-21 21:46:32 +00002133 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002134 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2135 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002136 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002137
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002138 /// \brief Offsets of each of the macros into the bitstream, indexed by
2139 /// the local macro ID
2140 ///
2141 /// For each identifier that is associated with a macro, this map
2142 /// provides the offset into the bitstream where that macro is
2143 /// defined.
2144 std::vector<uint32_t> MacroOffsets;
2145
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002146 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2147 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2148 MacroInfo *MI = MacroInfosToEmit[I].MI;
2149 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002150
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002151 if (ID < FirstMacroID) {
2152 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2153 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002154 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002155
2156 // Record the local offset of this macro.
2157 unsigned Index = ID - FirstMacroID;
2158 if (Index == MacroOffsets.size())
2159 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2160 else {
2161 if (Index > MacroOffsets.size())
2162 MacroOffsets.resize(Index + 1);
2163
2164 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2165 }
2166
2167 AddIdentifierRef(Name, Record);
2168 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2169 AddSourceLocation(MI->getDefinitionLoc(), Record);
2170 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2171 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002172 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002173 unsigned Code;
2174 if (MI->isObjectLike()) {
2175 Code = PP_MACRO_OBJECT_LIKE;
2176 } else {
2177 Code = PP_MACRO_FUNCTION_LIKE;
2178
2179 Record.push_back(MI->isC99Varargs());
2180 Record.push_back(MI->isGNUVarargs());
2181 Record.push_back(MI->hasCommaPasting());
2182 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002183 for (const IdentifierInfo *Arg : MI->args())
2184 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002185 }
2186
2187 // If we have a detailed preprocessing record, record the macro definition
2188 // ID that corresponds to this macro.
2189 if (PPRec)
2190 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2191
2192 Stream.EmitRecord(Code, Record);
2193 Record.clear();
2194
2195 // Emit the tokens array.
2196 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2197 // Note that we know that the preprocessor does not have any annotation
2198 // tokens in it because they are created by the parser, and thus can't
2199 // be in a macro definition.
2200 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002201 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002202 Stream.EmitRecord(PP_TOKEN, Record);
2203 Record.clear();
2204 }
2205 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002206 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002207
Douglas Gregor92a96f52011-02-08 21:58:10 +00002208 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002209
2210 // Write the offsets table for macro IDs.
2211 using namespace llvm;
Richard Smith13090a22015-04-10 02:02:24 +00002212 auto *Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002213 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2217
2218 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002219 {
2220 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2221 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2222 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2223 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002224}
2225
2226void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002227 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002228 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002229
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002230 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002231
Douglas Gregor92a96f52011-02-08 21:58:10 +00002232 // Enter the preprocessor block.
2233 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002234
Douglas Gregoraae92242010-03-19 21:51:54 +00002235 // If the preprocessor has a preprocessing record, emit it.
2236 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002237 using namespace llvm;
2238
2239 // Set up the abbreviation for
2240 unsigned InclusionAbbrev = 0;
2241 {
2242 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2243 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2249 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2250 }
2251
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002252 unsigned FirstPreprocessorEntityID
2253 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2254 + NUM_PREDEF_PP_ENTITY_IDS;
2255 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002256 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002257 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2258 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002259 E != EEnd;
2260 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002261 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002262
Richard Smith66a81862015-05-04 02:25:31 +00002263 PreprocessedEntityOffsets.push_back(
2264 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002265
Richard Smith66a81862015-05-04 02:25:31 +00002266 if (MacroDefinitionRecord *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002267 // Record this macro definition's ID.
2268 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002269
Douglas Gregor92a96f52011-02-08 21:58:10 +00002270 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002271 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2272 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002273 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002274
Chandler Carrutha88a22182011-07-14 08:20:46 +00002275 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002276 Record.push_back(ME->isBuiltinMacro());
2277 if (ME->isBuiltinMacro())
2278 AddIdentifierRef(ME->getName(), Record);
2279 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002280 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002281 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002282 continue;
2283 }
2284
2285 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2286 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002287 Record.push_back(ID->getFileName().size());
2288 Record.push_back(ID->wasInQuotes());
2289 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002290 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002291 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002292 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002293 // Check that the FileEntry is not null because it was not resolved and
2294 // we create a PCH even with compiler errors.
2295 if (ID->getFile())
2296 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002297 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002298 continue;
2299 }
2300
2301 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2302 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002303 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002304
Douglas Gregoraae92242010-03-19 21:51:54 +00002305 // Write the offsets table for the preprocessing record.
2306 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002307 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2308
Douglas Gregoraae92242010-03-19 21:51:54 +00002309 // Write the offsets table for identifier IDs.
2310 using namespace llvm;
2311 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002312 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002315 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002316
Mehdi Amini57a41912015-09-10 01:46:39 +00002317 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2318 FirstPreprocessorEntityID -
2319 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002320 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002321 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002322 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002323}
2324
Richard Smith386bb072015-08-18 23:42:23 +00002325unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002326 if (!Mod)
2327 return 0;
2328
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002329 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2330 if (Known != SubmoduleIDs.end())
2331 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002332
2333 if (Mod->getTopLevelModule() != WritingModule)
2334 return 0;
2335
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002336 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2337}
2338
Richard Smith386bb072015-08-18 23:42:23 +00002339unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2340 // FIXME: This can easily happen, if we have a reference to a submodule that
2341 // did not result in us loading a module file for that submodule. For
2342 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2343 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2344 assert((ID || !Mod) &&
2345 "asked for module ID for non-local, non-imported module");
2346 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002347}
2348
Douglas Gregor253eefe2011-12-01 00:59:36 +00002349/// \brief Compute the number of modules within the given tree (including the
2350/// given module).
2351static unsigned getNumberOfModules(Module *Mod) {
2352 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002353 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2354 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002355 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002356 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002357
2358 return ChildModules + 1;
2359}
2360
Douglas Gregorde3ef502011-11-30 23:21:26 +00002361void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002362 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002363 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002364
2365 // Write the abbreviations needed for the submodules block.
2366 using namespace llvm;
2367 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2368 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2380 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2381
2382 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002383 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2385 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2386
2387 Abbrev = new BitCodeAbbrev();
2388 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2390 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002391
2392 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002393 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2395 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2396
2397 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002398 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2400 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2401
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002402 Abbrev = new BitCodeAbbrev();
2403 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002406 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2407
Douglas Gregor59527662012-10-15 06:28:11 +00002408 Abbrev = new BitCodeAbbrev();
2409 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2411 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2412
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002413 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002414 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2416 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2417
2418 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002419 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2421 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2422
2423 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002424 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2426 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2427
2428 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002429 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2432 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2433
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002434 Abbrev = new BitCodeAbbrev();
2435 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2437 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2438
Douglas Gregorfb912652013-03-20 21:10:35 +00002439 Abbrev = new BitCodeAbbrev();
2440 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2443 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2444
Douglas Gregor253eefe2011-12-01 00:59:36 +00002445 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002446 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2447 FirstSubmoduleID -
2448 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002449 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2450
Douglas Gregor69021972011-11-30 17:33:56 +00002451 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002452 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002453 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002454 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002455 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002456 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002457 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002458
2459 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002460 if (Mod->Parent) {
2461 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002462 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002463 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002464
2465 // Emit the definition of the block.
2466 {
2467 RecordData::value_type Record[] = {
2468 SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2469 Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2470 Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2471 Mod->ConfigMacrosExhaustive};
2472 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2473 }
2474
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002475 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002476 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002477 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002478 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002479 }
2480
Douglas Gregor69021972011-11-30 17:33:56 +00002481 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002482 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002483 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002484 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2485 UmbrellaHeader.NameAsWritten);
2486 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002487 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2488 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002489 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002490 }
Richard Smith306d8922014-10-22 23:50:56 +00002491
Douglas Gregor69021972011-11-30 17:33:56 +00002492 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002493 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002494 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002495 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002496 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002497 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002498 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2499 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2500 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002501 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002502 Module::HK_PrivateTextual},
2503 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002504 };
2505 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002506 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002507 for (auto &H : Mod->Headers[HL.HeaderKind])
2508 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2509 }
2510
2511 // Emit the top headers.
2512 {
2513 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002514 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002515 for (auto *H : TopHeaders)
2516 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002517 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002518
2519 // Emit the imports.
2520 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002521 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002522 for (auto *I : Mod->Imports)
2523 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002524 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2525 }
2526
Douglas Gregor24bb9232011-12-02 18:58:38 +00002527 // Emit the exports.
2528 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002529 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002530 for (const auto &E : Mod->Exports) {
2531 // FIXME: This may fail; we don't require that all exported modules
2532 // are local or imported.
2533 Record.push_back(getSubmoduleID(E.getPointer()));
2534 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002535 }
2536 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2537 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002538
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002539 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2540 // Might be unnecessary as use declarations are only used to build the
2541 // module itself.
2542
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002543 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002544 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002545 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2546 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002547 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002548 }
2549
Douglas Gregorfb912652013-03-20 21:10:35 +00002550 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002551 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002552 // FIXME: This may fail; we don't require that all conflicting modules
2553 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002554 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2555 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002556 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002557 }
2558
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002559 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002560 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002561 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002562 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002563 }
2564
Douglas Gregor69021972011-11-30 17:33:56 +00002565 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002566 for (auto *M : Mod->submodules())
2567 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002568 }
2569
2570 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002571
Richard Smith23d8d032015-05-14 00:45:20 +00002572 assert((NextSubmoduleID - FirstSubmoduleID ==
2573 getNumberOfModules(WritingModule)) &&
2574 "Wrong # of submodules; found a reference to a non-local, "
2575 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002576}
2577
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002578serialization::SubmoduleID
2579ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002580 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002581 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002582
2583 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002584 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002585 Module *OwningMod
2586 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002587 if (!OwningMod)
2588 return 0;
2589
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002590 // Check whether this submodule is part of our own module.
2591 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002592 return 0;
2593
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002594 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002595}
2596
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002597void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2598 bool isModule) {
2599 // Make sure set diagnostic pragmas don't affect the translation unit that
2600 // imports the module.
2601 // FIXME: Make diagnostic pragma sections work properly with modules.
2602 if (isModule)
2603 return;
2604
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002605 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2606 DiagStateIDMap;
2607 unsigned CurrID = 0;
2608 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002609 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002610 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002611 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2612 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002613 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002614 if (point.Loc.isInvalid())
2615 continue;
2616
2617 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002618 unsigned &DiagStateID = DiagStateIDMap[point.State];
2619 Record.push_back(DiagStateID);
2620
2621 if (DiagStateID == 0) {
2622 DiagStateID = ++CurrID;
2623 for (DiagnosticsEngine::DiagState::const_iterator
2624 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2625 if (I->second.isPragma()) {
2626 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002627 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002628 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002629 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002630 Record.push_back(-1); // mark the end of the diag/map pairs for this
2631 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002632 }
2633 }
2634
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002635 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002636 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002637}
2638
Richard Smithc2bb8182015-03-24 06:36:48 +00002639void ASTWriter::WriteCXXCtorInitializersOffsets() {
2640 if (CXXCtorInitializersOffsets.empty())
2641 return;
2642
Richard Smithc2bb8182015-03-24 06:36:48 +00002643 // Create a blob abbreviation for the C++ ctor initializer offsets.
2644 using namespace llvm;
2645
2646 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2647 Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS));
2648 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2649 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2650 unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2651
2652 // Write the base specifier offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00002653 RecordData::value_type Record[] = {CXX_CTOR_INITIALIZERS_OFFSETS,
2654 CXXCtorInitializersOffsets.size()};
Richard Smithc2bb8182015-03-24 06:36:48 +00002655 Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002656 bytes(CXXCtorInitializersOffsets));
Richard Smithc2bb8182015-03-24 06:36:48 +00002657}
2658
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002659void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2660 if (CXXBaseSpecifiersOffsets.empty())
2661 return;
2662
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002663 // Create a blob abbreviation for the C++ base specifiers offsets.
2664 using namespace llvm;
2665
2666 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2667 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2668 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2670 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2671
Douglas Gregorc27b2872011-08-04 00:01:48 +00002672 // Write the base specifier offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00002673 RecordData::value_type Record[] = {CXX_BASE_SPECIFIER_OFFSETS,
2674 CXXBaseSpecifiersOffsets.size()};
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002675 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002676 bytes(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002677}
2678
Douglas Gregorc5046832009-04-27 18:38:38 +00002679//===----------------------------------------------------------------------===//
2680// Type Serialization
2681//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002682
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002683/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002684void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002685 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002686 if (Idx.getIndex() == 0) // we haven't seen this type before.
2687 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002688
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002689 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002690
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002691 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002692 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002693 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002694 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002695 else if (TypeOffsets.size() < Index) {
2696 TypeOffsets.resize(Index + 1);
2697 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002698 }
2699
2700 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002701
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002702 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002703 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002704 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002705
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002706 if (T.hasLocalNonFastQualifiers()) {
2707 Qualifiers Qs = T.getLocalQualifiers();
2708 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002709 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002710 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002711 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002712 } else {
2713 switch (T->getTypeClass()) {
2714 // For all of the concrete, non-dependent types, call the
2715 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002716#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002717 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002718#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002719#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002720 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002721 }
2722
2723 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002724 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002725
2726 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002727 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002728}
2729
Douglas Gregorc5046832009-04-27 18:38:38 +00002730//===----------------------------------------------------------------------===//
2731// Declaration Serialization
2732//===----------------------------------------------------------------------===//
2733
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002734/// \brief Write the block containing all of the declaration IDs
2735/// lexically declared within the given DeclContext.
2736///
2737/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2738/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002739uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002740 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002741 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002742 return 0;
2743
Douglas Gregor8f45df52009-04-16 22:23:12 +00002744 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002745 SmallVector<uint32_t, 128> KindDeclPairs;
2746 for (const auto *D : DC->decls()) {
2747 KindDeclPairs.push_back(D->getKind());
2748 KindDeclPairs.push_back(GetDeclRef(D));
2749 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002750
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002751 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002752 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002753 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2754 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002755 return Offset;
2756}
2757
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002758void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002759 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002760
2761 // Write the type offsets array
2762 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002763 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002764 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2767 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002768 {
2769 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2770 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2771 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2772 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002773
2774 // Write the declaration offsets array
2775 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002776 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2780 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002781 {
2782 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2783 FirstDeclID - NUM_PREDEF_DECL_IDS};
2784 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2785 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002786}
2787
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002788void ASTWriter::WriteFileDeclIDsMap() {
2789 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002790
Chandler Carruthb306d732015-03-27 00:31:20 +00002791 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2792 FileDeclIDs.begin(), FileDeclIDs.end());
2793 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2794 llvm::less_first());
2795
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002796 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00002797 SmallVector<DeclID, 256> FileGroupedDeclIDs;
2798 for (auto &FileDeclEntry : SortedFileDeclIDs) {
2799 DeclIDInFileInfo &Info = *FileDeclEntry.second;
2800 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2801 for (auto &LocDeclEntry : Info.DeclIDs)
2802 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002803 }
2804
2805 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2806 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2809 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00002810 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2811 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00002812 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002813}
2814
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002815void ASTWriter::WriteComments() {
2816 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002817 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002818 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002819 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2820 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002821 I != E; ++I) {
2822 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002823 AddSourceRange((*I)->getSourceRange(), Record);
2824 Record.push_back((*I)->getKind());
2825 Record.push_back((*I)->isTrailingComment());
2826 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002827 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2828 }
2829 Stream.ExitBlock();
2830}
2831
Douglas Gregorc5046832009-04-27 18:38:38 +00002832//===----------------------------------------------------------------------===//
2833// Global Method Pool and Selector Serialization
2834//===----------------------------------------------------------------------===//
2835
Douglas Gregore84a9da2009-04-20 20:36:09 +00002836namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002837// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002838class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002839 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002840
2841public:
2842 typedef Selector key_type;
2843 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002844
Sebastian Redl834bb972010-08-04 17:20:04 +00002845 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002846 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002847 ObjCMethodList Instance, Factory;
2848 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002849 typedef const data_type& data_type_ref;
2850
Justin Bogner25463f12014-04-18 20:27:24 +00002851 typedef unsigned hash_value_type;
2852 typedef unsigned offset_type;
2853
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002854 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002855
Justin Bogner25463f12014-04-18 20:27:24 +00002856 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002857 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002858 }
Mike Stump11289f42009-09-09 15:08:12 +00002859
2860 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002861 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002862 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002863 using namespace llvm::support;
2864 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002865 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002866 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002867 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2868 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002869 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002870 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002871 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002872 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002873 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002874 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002875 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002876 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002877 return std::make_pair(KeyLen, DataLen);
2878 }
Mike Stump11289f42009-09-09 15:08:12 +00002879
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002880 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002881 using namespace llvm::support;
2882 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002883 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002884 assert((Start >> 32) == 0 && "Selector key offset too large");
2885 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002886 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002887 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002888 if (N == 0)
2889 N = 1;
2890 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002891 LE.write<uint32_t>(
2892 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002893 }
Mike Stump11289f42009-09-09 15:08:12 +00002894
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002895 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002896 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002897 using namespace llvm::support;
2898 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002899 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002900 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002901 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002902 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002903 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002904 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002905 ++NumInstanceMethods;
2906
2907 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002908 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002909 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002910 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002911 ++NumFactoryMethods;
2912
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002913 unsigned InstanceBits = Methods.Instance.getBits();
2914 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002915 unsigned InstanceHasMoreThanOneDeclBit =
2916 Methods.Instance.hasMoreThanOneDecl();
2917 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2918 (InstanceHasMoreThanOneDeclBit << 2) |
2919 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002920 unsigned FactoryBits = Methods.Factory.getBits();
2921 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002922 unsigned FactoryHasMoreThanOneDeclBit =
2923 Methods.Factory.hasMoreThanOneDecl();
2924 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2925 (FactoryHasMoreThanOneDeclBit << 2) |
2926 FactoryBits;
2927 LE.write<uint16_t>(FullInstanceBits);
2928 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002929 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002930 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002931 if (Method->getMethod())
2932 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00002933 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002934 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002935 if (Method->getMethod())
2936 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002937
2938 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002939 }
2940};
2941} // end anonymous namespace
2942
Sebastian Redla19a67f2010-08-03 21:58:15 +00002943/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002944///
2945/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002946/// in an on-disk hash table indexed by the selector. The hash table also
2947/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002948void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002949 using namespace llvm;
2950
Sebastian Redla19a67f2010-08-03 21:58:15 +00002951 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002952 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002953 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002954 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002955 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002956 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002957 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002958 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002959
Sebastian Redla19a67f2010-08-03 21:58:15 +00002960 // Create the on-disk hash table representation. We walk through every
2961 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002962 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002963 for (auto &SelectorAndID : SelectorIDs) {
2964 Selector S = SelectorAndID.first;
2965 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002966 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002967 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002968 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00002969 ObjCMethodList(),
2970 ObjCMethodList()
2971 };
2972 if (F != SemaRef.MethodPool.end()) {
2973 Data.Instance = F->second.first;
2974 Data.Factory = F->second.second;
2975 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002976 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002977 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00002978 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002979 // Selector already exists. Did it change?
2980 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00002981 for (ObjCMethodList *M = &Data.Instance;
2982 !changed && M && M->getMethod(); M = M->getNext()) {
2983 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002984 changed = true;
2985 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00002986 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002987 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00002988 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002989 changed = true;
2990 }
2991 if (!changed)
2992 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00002993 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002994 // A new method pool entry.
2995 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002996 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002997 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002998 }
2999
Douglas Gregorc78d3462009-04-24 21:10:55 +00003000 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003001 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003002 uint32_t BucketOffset;
3003 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003004 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003005 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003006 llvm::raw_svector_ostream Out(MethodPool);
3007 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003008 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003009 BucketOffset = Generator.Emit(Out, Trait);
3010 }
3011
3012 // Create a blob abbreviation
3013 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003014 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3018 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3019
Douglas Gregor95c13f52009-04-25 17:48:32 +00003020 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003021 {
3022 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3023 NumTableEntries};
3024 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3025 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003026
3027 // Create a blob abbreviation for the selector table offsets.
3028 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003029 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3033 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3034
3035 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003036 {
3037 RecordData::value_type Record[] = {
3038 SELECTOR_OFFSETS, SelectorOffsets.size(),
3039 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3040 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3041 bytes(SelectorOffsets));
3042 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003043 }
3044}
3045
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003046/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003047void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003048 using namespace llvm;
3049 if (SemaRef.ReferencedSelectors.empty())
3050 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003051
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003052 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003053
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003054 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003055 // very tricky to fix, and given that @selector shouldn't really appear in
3056 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003057 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3058 Selector Sel = SelectorAndLocation.first;
3059 SourceLocation Loc = SelectorAndLocation.second;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003060 AddSelectorRef(Sel, Record);
3061 AddSourceLocation(Loc, Record);
3062 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003063 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003064}
3065
Douglas Gregorc5046832009-04-27 18:38:38 +00003066//===----------------------------------------------------------------------===//
3067// Identifier Table Serialization
3068//===----------------------------------------------------------------------===//
3069
Richard Smithb3761912015-02-05 23:08:52 +00003070/// Determine the declaration that should be put into the name lookup table to
3071/// represent the given declaration in this module. This is usually D itself,
3072/// but if D was imported and merged into a local declaration, we want the most
3073/// recent local declaration instead. The chosen declaration will be the most
3074/// recent declaration in any module that imports this one.
3075static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3076 NamedDecl *D) {
3077 if (!LangOpts.Modules || !D->isFromASTFile())
3078 return D;
3079
3080 if (Decl *Redecl = D->getPreviousDecl()) {
3081 // For Redeclarable decls, a prior declaration might be local.
3082 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3083 if (!Redecl->isFromASTFile())
3084 return cast<NamedDecl>(Redecl);
Richard Smithfe620d22015-03-05 23:24:12 +00003085 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003086 // local one.
3087 if (D->getOwningModuleID() == 0)
3088 break;
3089 }
3090 } else if (Decl *First = D->getCanonicalDecl()) {
3091 // For Mergeable decls, the first decl might be local.
3092 if (!First->isFromASTFile())
3093 return cast<NamedDecl>(First);
3094 }
3095
3096 // All declarations are imported. Our most recent declaration will also be
3097 // the most recent one in anyone who imports us.
3098 return D;
3099}
3100
Douglas Gregorc78d3462009-04-24 21:10:55 +00003101namespace {
Richard Smithcf97cf62015-04-19 01:34:23 +00003102class ASTIdentifierTableTrait {
3103 ASTWriter &Writer;
3104 Preprocessor &PP;
3105 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003106 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003107 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003108 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003109
3110 /// \brief Determines whether this is an "interesting" identifier that needs a
3111 /// full IdentifierInfo structure written into the hash table. Notably, this
3112 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3113 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003114 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003115 if (MacroOffset ||
3116 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003117 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003118 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003119 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003120 return true;
3121
3122 return false;
3123 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003124
Douglas Gregore84a9da2009-04-20 20:36:09 +00003125public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003126 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003127 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003128
Sebastian Redl539c5062010-08-18 23:57:32 +00003129 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003130 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003131
Justin Bogner25463f12014-04-18 20:27:24 +00003132 typedef unsigned hash_value_type;
3133 typedef unsigned offset_type;
3134
Richard Smithd7329392015-04-21 21:46:32 +00003135 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003136 IdentifierResolver &IdResolver, bool IsModule,
3137 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003138 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003139 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3140 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003141
Justin Bogner25463f12014-04-18 20:27:24 +00003142 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003143 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003144 }
Mike Stump11289f42009-09-09 15:08:12 +00003145
Richard Smith33e0f7e2015-07-22 02:08:40 +00003146 bool isInterestingIdentifier(const IdentifierInfo *II) {
3147 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3148 return isInterestingIdentifier(II, MacroOffset);
3149 }
Richard Smith9c254182015-07-19 21:41:12 +00003150 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3151 return isInterestingIdentifier(II, 0);
3152 }
3153
Mike Stump11289f42009-09-09 15:08:12 +00003154 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003155 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003156 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003157 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003158 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3159 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003160 DataLen += 2; // 2 bytes for builtin ID
3161 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003162 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003163 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003164
Richard Smitha534a312015-07-21 23:54:07 +00003165 if (NeedDecls) {
3166 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3167 DEnd = IdResolver.end();
3168 D != DEnd; ++D)
3169 DataLen += 4;
3170 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003171 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003172 using namespace llvm::support;
3173 endian::Writer<little> LE(Out);
3174
Richard Smithdf8a8312015-03-13 04:05:01 +00003175 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003176 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003177 // We emit the key length after the data length so that every
3178 // string is preceded by a 16-bit length. This matches the PTH
3179 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003180 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003181 return std::make_pair(KeyLen, DataLen);
3182 }
Mike Stump11289f42009-09-09 15:08:12 +00003183
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003184 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003185 unsigned KeyLen) {
3186 // Record the location of the key data. This is used when generating
3187 // the mapping from persistent IDs to strings.
3188 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003189
3190 // Emit the offset of the key/data length information to the interesting
3191 // identifiers table if necessary.
3192 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3193 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3194
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003195 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003196 }
Mike Stump11289f42009-09-09 15:08:12 +00003197
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003198 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003199 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003200 using namespace llvm::support;
3201 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003202
Richard Smithd7329392015-04-21 21:46:32 +00003203 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3204 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003205 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003206 return;
3207 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003208
Justin Bognere1c147c2014-03-28 22:03:19 +00003209 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003210 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3211 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003212 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003213 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003214 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003215 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003216 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3217 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003218 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003219 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003220 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003221 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003222
Richard Smithd7329392015-04-21 21:46:32 +00003223 if (HadMacroDefinition)
3224 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003225
Richard Smitha534a312015-07-21 23:54:07 +00003226 if (NeedDecls) {
3227 // Emit the declaration IDs in reverse order, because the
3228 // IdentifierResolver provides the declarations as they would be
3229 // visible (e.g., the function "stat" would come before the struct
3230 // "stat"), but the ASTReader adds declarations to the end of the list
3231 // (so we need to see the struct "stat" before the function "stat").
3232 // Only emit declarations that aren't from a chained PCH, though.
3233 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3234 IdResolver.end());
3235 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3236 DEnd = Decls.rend();
3237 D != DEnd; ++D)
3238 LE.write<uint32_t>(
3239 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3240 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003241 }
3242};
3243} // end anonymous namespace
3244
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003245/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003246///
3247/// The identifier table consists of a blob containing string data
3248/// (the actual identifiers themselves) and a separate "offsets" index
3249/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003250void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3251 IdentifierResolver &IdResolver,
3252 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003253 using namespace llvm;
3254
Richard Smith33e0f7e2015-07-22 02:08:40 +00003255 RecordData InterestingIdents;
3256
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003257 // Create and write out the blob that contains the identifier
3258 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003259 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003260 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003261 ASTIdentifierTableTrait Trait(
3262 *this, PP, IdResolver, IsModule,
3263 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003264
Douglas Gregore6648fb2009-04-28 20:33:11 +00003265 // Look for any identifiers that were named while processing the
3266 // headers, but are otherwise not needed. We add these to the hash
3267 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003268 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003269 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003270 SmallVector<const IdentifierInfo *, 128> IIs;
Douglas Gregore6648fb2009-04-28 20:33:11 +00003271 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3272 IDEnd = PP.getIdentifierTable().end();
3273 ID != IDEnd; ++ID)
Chandler Carruth8440c982015-03-26 23:54:15 +00003274 IIs.push_back(ID->second);
3275 // Sort the identifiers lexicographically before getting them references so
3276 // that their order is stable.
3277 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3278 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003279 if (Trait.isInterestingNonMacroIdentifier(II))
3280 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003281
Sebastian Redlff4a2952010-07-23 23:49:55 +00003282 // Create the on-disk hash table representation. We only store offsets
3283 // for identifiers that appear here for the first time.
3284 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003285 for (auto IdentIDPair : IdentifierIDs) {
3286 IdentifierInfo *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3287 IdentID ID = IdentIDPair.second;
3288 assert(II && "NULL identifier in identifier table");
3289 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
3290 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003291 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003292
Douglas Gregore84a9da2009-04-20 20:36:09 +00003293 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003294 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003295 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003296 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003297 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003298 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003299 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003300 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003301 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003302 }
3303
3304 // Create a blob abbreviation
3305 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003306 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003309 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003310
3311 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003312 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003313 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003314 }
3315
3316 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003317 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003318 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003319 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003321 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3322 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3323
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003324#ifndef NDEBUG
3325 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3326 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3327#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003328
3329 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3330 IdentifierOffsets.size(),
3331 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003332 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003333 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003334
3335 // In C++, write the list of interesting identifiers (those that are
3336 // defined as macros, poisoned, or similar unusual things).
3337 if (!InterestingIdents.empty())
3338 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003339}
3340
Douglas Gregorc5046832009-04-27 18:38:38 +00003341//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003342// DeclContext's Name Lookup Table Serialization
3343//===----------------------------------------------------------------------===//
3344
3345namespace {
3346// Trait used for the on-disk hash table used in the method pool.
3347class ASTDeclContextNameLookupTrait {
3348 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003349 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003350
3351public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003352 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003353 typedef key_type key_type_ref;
3354
Richard Smithd88a7f12015-09-01 20:35:42 +00003355 /// A start and end index into DeclIDs, representing a sequence of decls.
3356 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003357 typedef const data_type& data_type_ref;
3358
Justin Bogner25463f12014-04-18 20:27:24 +00003359 typedef unsigned hash_value_type;
3360 typedef unsigned offset_type;
3361
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003362 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3363
Richard Smithd88a7f12015-09-01 20:35:42 +00003364 template<typename Coll>
3365 data_type getData(const Coll &Decls) {
3366 unsigned Start = DeclIDs.size();
3367 for (NamedDecl *D : Decls) {
3368 DeclIDs.push_back(
3369 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3370 }
3371 return std::make_pair(Start, DeclIDs.size());
3372 }
3373
3374 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3375 unsigned Start = DeclIDs.size();
3376 for (auto ID : FromReader)
3377 DeclIDs.push_back(ID);
3378 return std::make_pair(Start, DeclIDs.size());
3379 }
3380
3381 static bool EqualKey(key_type_ref a, key_type_ref b) {
3382 return a == b;
3383 }
3384
Richard Smitha06c7e62015-08-26 23:55:49 +00003385 hash_value_type ComputeHash(DeclarationNameKey Name) {
3386 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003387 }
3388
Richard Smithd88a7f12015-09-01 20:35:42 +00003389 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3390 assert(Writer.hasChain() &&
3391 "have reference to loaded module file but no chain?");
3392
3393 using namespace llvm::support;
3394 endian::Writer<little>(Out)
3395 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3396 }
3397
Richard Smitha06c7e62015-08-26 23:55:49 +00003398 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3399 DeclarationNameKey Name,
3400 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003401 using namespace llvm::support;
3402 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003403 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003404 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003405 case DeclarationName::Identifier:
3406 case DeclarationName::ObjCZeroArgSelector:
3407 case DeclarationName::ObjCOneArgSelector:
3408 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003409 case DeclarationName::CXXLiteralOperatorName:
3410 KeyLen += 4;
3411 break;
3412 case DeclarationName::CXXOperatorName:
3413 KeyLen += 1;
3414 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003415 case DeclarationName::CXXConstructorName:
3416 case DeclarationName::CXXDestructorName:
3417 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003418 case DeclarationName::CXXUsingDirective:
3419 break;
3420 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003421 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003422
Richard Smithf02662d2015-07-30 03:17:16 +00003423 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003424 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3425 assert(uint16_t(DataLen) == DataLen &&
3426 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003427 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003428
3429 return std::make_pair(KeyLen, DataLen);
3430 }
3431
Richard Smitha06c7e62015-08-26 23:55:49 +00003432 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003433 using namespace llvm::support;
3434 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003435 LE.write<uint8_t>(Name.getKind());
3436 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003437 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003438 case DeclarationName::CXXLiteralOperatorName:
3439 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003440 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003441 case DeclarationName::ObjCZeroArgSelector:
3442 case DeclarationName::ObjCOneArgSelector:
3443 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003444 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003445 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003446 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003447 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003448 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003449 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003450 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003451 case DeclarationName::CXXConstructorName:
3452 case DeclarationName::CXXDestructorName:
3453 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003454 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003455 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003456 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003457
3458 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003459 }
3460
Richard Smitha06c7e62015-08-26 23:55:49 +00003461 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3462 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003463 using namespace llvm::support;
3464 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003465 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003466 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3467 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003468 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3469 }
3470};
3471} // end anonymous namespace
3472
Chandler Carruthe972c362015-03-26 03:11:40 +00003473bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3474 DeclContext *DC) {
3475 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3476}
3477
3478bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3479 DeclContext *DC) {
3480 for (auto *D : Result.getLookupResult())
3481 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3482 return false;
3483
3484 return true;
3485}
3486
Richard Smithd88a7f12015-09-01 20:35:42 +00003487void
Chandler Carruthe972c362015-03-26 03:11:40 +00003488ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003489 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003490 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3491 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003492 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003493
Chandler Carruthe972c362015-03-26 03:11:40 +00003494 // FIXME: We need to build the lookups table, which is logically const.
3495 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
3496 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3497
3498 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003499 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3500 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003501 ASTDeclContextNameLookupTrait Trait(*this);
3502
Chandler Carruthe972c362015-03-26 03:11:40 +00003503 // The first step is to collect the declaration names which we need to
3504 // serialize into the name lookup table, and to collect them in a stable
3505 // order.
3506 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003507
Chandler Carruthe972c362015-03-26 03:11:40 +00003508 // We also build up small sets of the constructor and conversion function
3509 // names which are visible.
3510 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003511
Chandler Carruthe972c362015-03-26 03:11:40 +00003512 for (auto &Lookup : *DC->buildLookup()) {
3513 auto &Name = Lookup.first;
3514 auto &Result = Lookup.second;
3515
3516 // If there are no local declarations in our lookup result, we don't
3517 // need to write an entry for the name at all unless we're rewriting
3518 // the decl context. If we can't write out a lookup set without
3519 // performing more deserialization, just skip this entry.
3520 if (isLookupResultExternal(Result, DC) && !isRewritten(cast<Decl>(DC)) &&
3521 isLookupResultEntirelyExternal(Result, DC))
3522 continue;
3523
3524 // We also skip empty results. If any of the results could be external and
3525 // the currently available results are empty, then all of the results are
3526 // external and we skip it above. So the only way we get here with an empty
3527 // results is when no results could have been external *and* we have
3528 // external results.
3529 //
3530 // FIXME: While we might want to start emitting on-disk entries for negative
3531 // lookups into a decl context as an optimization, today we *have* to skip
3532 // them because there are names with empty lookup results in decl contexts
3533 // which we can't emit in any stable ordering: we lookup constructors and
3534 // conversion functions in the enclosing namespace scope creating empty
3535 // results for them. This in almost certainly a bug in Clang's name lookup,
3536 // but that is likely to be hard or impossible to fix and so we tolerate it
3537 // here by omitting lookups with empty results.
3538 if (Lookup.second.getLookupResult().empty())
3539 continue;
3540
3541 switch (Lookup.first.getNameKind()) {
3542 default:
3543 Names.push_back(Lookup.first);
3544 break;
3545
Richard Smithcd45dbc2014-04-19 03:48:30 +00003546 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003547 assert(isa<CXXRecordDecl>(DC) &&
3548 "Cannot have a constructor name outside of a class!");
3549 ConstructorNameSet.insert(Name);
3550 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003551
3552 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003553 assert(isa<CXXRecordDecl>(DC) &&
3554 "Cannot have a conversion function name outside of a class!");
3555 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003556 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003557 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003558 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003559
Chandler Carruthe972c362015-03-26 03:11:40 +00003560 // Sort the names into a stable order.
3561 std::sort(Names.begin(), Names.end());
3562
Chandler Carruthffbf7052015-03-26 22:27:09 +00003563 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003564 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003565 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003566
Chandler Carruthffbf7052015-03-26 22:27:09 +00003567 // First we try the easy case by forming the current context's constructor
3568 // name and adding that name first. This is a very useful optimization to
3569 // avoid walking the lexical declarations in many cases, and it also
3570 // handles the only case where a constructor name can come from some other
3571 // lexical context -- when that name is an implicit constructor merged from
3572 // another declaration in the redecl chain. Any non-implicit constructor or
3573 // conversion function which doesn't occur in all the lexical contexts
3574 // would be an ODR violation.
3575 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3576 Context->getCanonicalType(Context->getRecordType(D)));
3577 if (ConstructorNameSet.erase(ImplicitCtorName))
3578 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003579
Chandler Carruthffbf7052015-03-26 22:27:09 +00003580 // If we still have constructors or conversion functions, we walk all the
3581 // names in the decl and add the constructors and conversion functions
3582 // which are visible in the order they lexically occur within the context.
3583 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3584 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3585 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3586 auto Name = ChildND->getDeclName();
3587 switch (Name.getNameKind()) {
3588 default:
3589 continue;
3590
3591 case DeclarationName::CXXConstructorName:
3592 if (ConstructorNameSet.erase(Name))
3593 Names.push_back(Name);
3594 break;
3595
3596 case DeclarationName::CXXConversionFunctionName:
3597 if (ConversionNameSet.erase(Name))
3598 Names.push_back(Name);
3599 break;
3600 }
3601
3602 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3603 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003604 }
3605
Chandler Carruthe972c362015-03-26 03:11:40 +00003606 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3607 "constructors by walking all the "
3608 "lexical members of the context.");
3609 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3610 "conversion functions by walking all "
3611 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003612 }
3613
Chandler Carruthe972c362015-03-26 03:11:40 +00003614 // Next we need to do a lookup with each name into this decl context to fully
3615 // populate any results from external sources. We don't actually use the
3616 // results of these lookups because we only want to use the results after all
3617 // results have been loaded and the pointers into them will be stable.
3618 for (auto &Name : Names)
3619 DC->lookup(Name);
3620
3621 // Now we need to insert the results for each name into the hash table. For
3622 // constructor names and conversion function names, we actually need to merge
3623 // all of the results for them into one list of results each and insert
3624 // those.
3625 SmallVector<NamedDecl *, 8> ConstructorDecls;
3626 SmallVector<NamedDecl *, 8> ConversionDecls;
3627
3628 // Now loop over the names, either inserting them or appending for the two
3629 // special cases.
3630 for (auto &Name : Names) {
3631 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3632
3633 switch (Name.getNameKind()) {
3634 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003635 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003636 break;
3637
3638 case DeclarationName::CXXConstructorName:
3639 ConstructorDecls.append(Result.begin(), Result.end());
3640 break;
3641
3642 case DeclarationName::CXXConversionFunctionName:
3643 ConversionDecls.append(Result.begin(), Result.end());
3644 break;
3645 }
3646 }
3647
3648 // Handle our two special cases if we ended up having any. We arbitrarily use
3649 // the first declaration's name here because the name itself isn't part of
3650 // the key, only the kind of name is used.
3651 if (!ConstructorDecls.empty())
3652 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003653 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003654 if (!ConversionDecls.empty())
3655 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003656 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003657
Richard Smithd88a7f12015-09-01 20:35:42 +00003658 // Create the on-disk hash table. Also emit the existing imported and
3659 // merged table if there is one.
3660 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3661 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003662}
3663
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003664/// \brief Write the block containing all of the declaration IDs
3665/// visible from the given DeclContext.
3666///
3667/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003668/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003669uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3670 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003671 // If we imported a key declaration of this namespace, write the visible
3672 // lookup results as an update record for it rather than including them
3673 // on this declaration. We will only look at key declarations on reload.
3674 if (isa<NamespaceDecl>(DC) && Chain &&
3675 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3676 // Only do this once, for the first local declaration of the namespace.
3677 for (NamespaceDecl *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
3678 Prev = Prev->getPreviousDecl())
3679 if (!Prev->isFromASTFile())
3680 return 0;
3681
3682 // Note that we need to emit an update record for the primary context.
3683 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3684
3685 // Make sure all visible decls are written. They will be recorded later. We
3686 // do this using a side data structure so we can sort the names into
3687 // a deterministic order.
3688 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3689 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3690 LookupResults;
3691 if (Map) {
3692 LookupResults.reserve(Map->size());
3693 for (auto &Entry : *Map)
3694 LookupResults.push_back(
3695 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3696 }
3697
3698 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3699 for (auto &NameAndResult : LookupResults) {
3700 DeclarationName Name = NameAndResult.first;
3701 DeclContext::lookup_result Result = NameAndResult.second;
3702 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3703 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3704 // We have to work around a name lookup bug here where negative lookup
3705 // results for these names get cached in namespace lookup tables (these
3706 // names should never be looked up in a namespace).
3707 assert(Result.empty() && "Cannot have a constructor or conversion "
3708 "function name in a namespace!");
3709 continue;
3710 }
3711
3712 for (NamedDecl *ND : Result)
3713 if (!ND->isFromASTFile())
3714 GetDeclRef(ND);
3715 }
3716
3717 return 0;
3718 }
3719
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003720 if (DC->getPrimaryContext() != DC)
3721 return 0;
3722
Chandler Carruthe972c362015-03-26 03:11:40 +00003723 // Skip contexts which don't support name lookup.
3724 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003725 return 0;
3726
3727 // If not in C++, we perform name lookup for the translation unit via the
3728 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003729 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003730 return 0;
3731
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003732 // Serialize the contents of the mapping used for lookup. Note that,
3733 // although we have two very different code paths, the serialized
3734 // representation is the same for both cases: a declaration name,
3735 // followed by a size, followed by references to the visible
3736 // declarations that have that name.
3737 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003738 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003739 if (!Map || Map->empty())
3740 return 0;
3741
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003742 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003743 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003744 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003745
3746 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003747 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003748 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003749 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003750 ++NumVisibleDeclContexts;
3751 return Offset;
3752}
3753
Sebastian Redla4071b42010-08-24 00:50:09 +00003754/// \brief Write an UPDATE_VISIBLE block for the given context.
3755///
3756/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3757/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003758/// (in C++), for namespaces, and for classes with forward-declared unscoped
3759/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003760void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith0f4e2c42015-08-06 04:23:48 +00003761 if (isRewritten(cast<Decl>(DC)))
3762 return;
3763
Richard Smith961eae52014-03-25 01:14:22 +00003764 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003765 if (!Map || Map->empty())
3766 return;
3767
Sebastian Redla4071b42010-08-24 00:50:09 +00003768 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003769 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003770 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003771
Richard Smith5fc18a92015-07-12 23:43:21 +00003772 // If we're updating a namespace, select a key declaration as the key for the
3773 // update record; those are the only ones that will be checked on reload.
3774 if (isa<NamespaceDecl>(DC))
3775 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3776
Sebastian Redla4071b42010-08-24 00:50:09 +00003777 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003778 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00003779 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003780}
3781
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003782/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3783void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00003784 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003785 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3786}
3787
3788/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3789void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003790 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003791 return;
3792
3793 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3794 RecordData Record;
3795#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3796#include "clang/Basic/OpenCLExtensions.def"
3797 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3798}
3799
Douglas Gregor404cdde2012-01-27 01:47:08 +00003800void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003801 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003802 RecordData Categories;
3803
3804 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3805 unsigned Size = 0;
3806 unsigned StartIndex = Categories.size();
3807
3808 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3809
3810 // Allocate space for the size.
3811 Categories.push_back(0);
3812
3813 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003814 for (ObjCInterfaceDecl::known_categories_iterator
3815 Cat = Class->known_categories_begin(),
3816 CatEnd = Class->known_categories_end();
3817 Cat != CatEnd; ++Cat, ++Size) {
3818 assert(getDeclID(*Cat) != 0 && "Bogus category");
3819 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003820 }
3821
3822 // Update the size.
3823 Categories[StartIndex] = Size;
3824
3825 // Record this interface -> category map.
3826 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3827 CategoriesMap.push_back(CatInfo);
3828 }
3829
3830 // Sort the categories map by the definition ID, since the reader will be
3831 // performing binary searches on this information.
3832 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3833
3834 // Emit the categories map.
3835 using namespace llvm;
3836 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3837 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3840 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
Mehdi Amini57a41912015-09-10 01:46:39 +00003841
3842 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
3843 Stream.EmitRecordWithBlob(AbbrevID, Record,
3844 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00003845 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00003846
Douglas Gregor404cdde2012-01-27 01:47:08 +00003847 // Emit the category lists.
3848 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3849}
3850
Richard Smithe40f2ba2013-08-07 21:41:30 +00003851void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3852 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3853
3854 if (LPTMap.empty())
3855 return;
3856
3857 RecordData Record;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00003858 for (auto LPTMapEntry : LPTMap) {
3859 const FunctionDecl *FD = LPTMapEntry.first;
3860 LateParsedTemplate *LPT = LPTMapEntry.second;
3861 AddDeclRef(FD, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00003862 AddDeclRef(LPT->D, Record);
3863 Record.push_back(LPT->Toks.size());
3864
3865 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3866 TokEnd = LPT->Toks.end();
3867 TokIt != TokEnd; ++TokIt) {
3868 AddToken(*TokIt, Record);
3869 }
3870 }
3871 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3872}
3873
Dario Domizioli13a0a382014-05-23 12:13:25 +00003874/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3875void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3876 RecordData Record;
3877 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3878 AddSourceLocation(PragmaLoc, Record);
3879 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3880}
3881
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003882//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003883// General Serialization Routines
3884//===----------------------------------------------------------------------===//
3885
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003886/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003887void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3888 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003889 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003890 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3891 e = Attrs.end(); i != e; ++i){
3892 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003893 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003894 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003895
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003896#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003897
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003898 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003899}
3900
John McCallf413f5e2013-05-03 00:10:13 +00003901void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3902 AddSourceLocation(Tok.getLocation(), Record);
3903 Record.push_back(Tok.getLength());
3904
3905 // FIXME: When reading literal tokens, reconstruct the literal pointer
3906 // if it is needed.
3907 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3908 // FIXME: Should translate token kind to a stable encoding.
3909 Record.push_back(Tok.getKind());
3910 // FIXME: Should translate token flags to a stable encoding.
3911 Record.push_back(Tok.getFlags());
3912}
3913
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003914void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003915 Record.push_back(Str.size());
3916 Record.insert(Record.end(), Str.begin(), Str.end());
3917}
3918
Richard Smith7ed1bc92014-12-05 22:42:13 +00003919bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00003920 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00003921
Richard Smith54cc3c22014-12-11 20:50:24 +00003922 bool Changed =
3923 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00003924
3925 // Remove a prefix to make the path relative, if relevant.
3926 const char *PathBegin = Path.data();
3927 const char *PathPtr =
3928 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
3929 if (PathPtr != PathBegin) {
3930 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
3931 Changed = true;
3932 }
3933
3934 return Changed;
3935}
3936
3937void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
3938 SmallString<128> FilePath(Path);
3939 PreparePathForOutput(FilePath);
3940 AddString(FilePath, Record);
3941}
3942
Mehdi Amini57a41912015-09-10 01:46:39 +00003943void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00003944 StringRef Path) {
3945 SmallString<128> FilePath(Path);
3946 PreparePathForOutput(FilePath);
3947 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
3948}
3949
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003950void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3951 RecordDataImpl &Record) {
3952 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003953 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003954 Record.push_back(*Minor + 1);
3955 else
3956 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003957 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003958 Record.push_back(*Subminor + 1);
3959 else
3960 Record.push_back(0);
3961}
3962
Douglas Gregore84a9da2009-04-20 20:36:09 +00003963/// \brief Note that the identifier II occurs at the given offset
3964/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003965void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003966 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003967 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003968 // up earlier in the chain and thus don't need an offset.
3969 if (ID >= FirstIdentID)
3970 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003971}
3972
Douglas Gregor95c13f52009-04-25 17:48:32 +00003973/// \brief Note that the selector Sel occurs at the given offset
3974/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003975void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003976 unsigned ID = SelectorIDs[Sel];
3977 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003978 // Don't record offsets for selectors that are also available in a different
3979 // file.
3980 if (ID < FirstSelectorID)
3981 return;
3982 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003983}
3984
Richard Smithe75ee0f2015-08-17 07:13:32 +00003985ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream, bool IncludeTimestamps)
Richard Smith01b2cb42014-07-26 06:37:51 +00003986 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
Richard Smithe75ee0f2015-08-17 07:13:32 +00003987 WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps),
3988 WritingAST(false), DoneWritingDeclsAndTypes(false),
3989 ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS),
3990 NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS),
3991 NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS),
3992 NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS),
3993 NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
Richard Smith01b2cb42014-07-26 06:37:51 +00003994 NextSubmoduleID(FirstSubmoduleID),
3995 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
3996 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
3997 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00003998 NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
Richard Smithe75ee0f2015-08-17 07:13:32 +00003999 TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004000 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004001 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004002 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004003 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4004 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4005 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004006
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004007ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004008 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004009}
4010
Richard Smithb3761912015-02-05 23:08:52 +00004011const LangOptions &ASTWriter::getLangOpts() const {
4012 assert(WritingAST && "can't determine lang opts when not writing AST");
4013 return Context->getLangOpts();
4014}
4015
Richard Smithe75ee0f2015-08-17 07:13:32 +00004016time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4017 return IncludeTimestamps ? E->getModificationTime() : 0;
4018}
4019
Adrian Prantladbd2b12015-09-22 23:26:31 +00004020uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4021 Module *WritingModule, StringRef isysroot,
4022 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004023 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004024
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004025 ASTHasCompilerErrors = hasErrors;
4026
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004027 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004028 Stream.Emit((unsigned)'C', 8);
4029 Stream.Emit((unsigned)'P', 8);
4030 Stream.Emit((unsigned)'C', 8);
4031 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004032
Chris Lattner28fa4e62009-04-26 22:26:21 +00004033 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004034
Douglas Gregoreda8e122011-08-09 15:13:55 +00004035 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004036 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004037 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004038 ASTFileSignature Signature =
4039 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004040 Context = nullptr;
4041 PP = nullptr;
4042 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004043 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004044
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004045 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004046 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004047}
4048
Douglas Gregora94a1542011-07-27 21:45:57 +00004049template<typename Vector>
4050static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4051 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004052 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4053 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004054 Writer.AddDeclRef(*I, Record);
4055 }
4056}
4057
Adrian Prantladbd2b12015-09-22 23:26:31 +00004058uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4059 const std::string &OutputFile,
4060 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004061 using namespace llvm;
4062
Craig Toppera13603a2014-05-22 05:54:18 +00004063 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004064
Douglas Gregorcf68c582011-12-01 22:20:10 +00004065 // Make sure that the AST reader knows to finalize itself.
4066 if (Chain)
4067 Chain->finalizeForWriting();
4068
Sebastian Redl143413f2010-07-12 22:02:52 +00004069 ASTContext &Context = SemaRef.Context;
4070 Preprocessor &PP = SemaRef.PP;
4071
Douglas Gregordab42432011-08-12 00:15:20 +00004072 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004073 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4074 if (D) {
4075 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4076 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004077 }
4078 };
4079 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4080 PREDEF_DECL_TRANSLATION_UNIT_ID);
4081 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4082 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4083 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4084 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4085 PREDEF_DECL_OBJC_PROTOCOL_ID);
4086 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4087 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4088 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4089 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4090 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004091 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004092 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4093 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004094 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004095
Chris Lattner0c797362009-09-08 18:19:27 +00004096 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004097 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004098 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004099 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004100 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004101
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004102 // Build a record containing all of the file scoped decls in this file.
4103 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004104 if (!isModule)
4105 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4106 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004107
Douglas Gregor851443c2011-08-12 01:39:19 +00004108 // Build a record containing all of the delegating constructors we still need
4109 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004110 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004111 if (!isModule)
4112 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004113
Douglas Gregor851443c2011-08-12 01:39:19 +00004114 // Write the set of weak, undeclared identifiers. We always write the
4115 // entire table, since later PCH files in a PCH chain are only interested in
4116 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004117 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004118 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4119 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4120 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4121 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4122 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4123 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4124 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004125 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004126
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004127 // Build a record containing all of the ext_vector declarations.
4128 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004129 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004130
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004131 // Build a record containing all of the VTable uses information.
4132 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004133 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004134 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4135 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4136 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4137 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4138 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004139 }
4140
Nico Weber72889432014-09-06 01:25:55 +00004141 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4142 RecordData UnusedLocalTypedefNameCandidates;
4143 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4144 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4145
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004146 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004147 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004148 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004149 I = SemaRef.PendingInstantiations.begin(),
4150 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4151 AddDeclRef(I->first, PendingInstantiations);
4152 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004153 }
4154 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4155 "There are local ones at end of translation unit!");
4156
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004157 // Build a record containing some declaration references.
4158 RecordData SemaDeclRefs;
4159 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4160 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4161 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4162 }
4163
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004164 RecordData CUDASpecialDeclRefs;
4165 if (Context.getcudaConfigureCallDecl()) {
4166 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4167 }
4168
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004169 // Build a record containing all of the known namespaces.
4170 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004171 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004172 I = SemaRef.KnownNamespaces.begin(),
4173 IEnd = SemaRef.KnownNamespaces.end();
4174 I != IEnd; ++I) {
4175 if (!I->second)
4176 AddDeclRef(I->first, KnownNamespaces);
4177 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004178
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004179 // Build a record of all used, undefined objects that require definitions.
4180 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004181
4182 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004183 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004184 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4185 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004186 AddDeclRef(I->first, UndefinedButUsed);
4187 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004188 }
4189
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004190 // Build a record containing all delete-expressions that we would like to
4191 // analyze later in AST.
4192 RecordData DeleteExprsToAnalyze;
4193
4194 for (const auto &DeleteExprsInfo :
4195 SemaRef.getMismatchingDeleteExpressions()) {
4196 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4197 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4198 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4199 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4200 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4201 }
4202 }
4203
Douglas Gregor112b9072012-10-18 05:31:06 +00004204 // Write the control block
Adrian Prantladbd2b12015-09-22 23:26:31 +00004205 uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004206
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004207 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004208 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004209
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004210 // This is so that older clang versions, before the introduction
4211 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004212 {
4213 RecordData Record = {VERSION_MAJOR};
4214 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4215 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004216
Douglas Gregor851443c2011-08-12 01:39:19 +00004217 // Create a lexical update block containing all of the declarations in the
4218 // translation unit that do not come from other AST files.
4219 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004220 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4221 for (const auto *D : TU->noload_decls()) {
4222 if (!D->isFromASTFile()) {
4223 NewGlobalKindDeclPairs.push_back(D->getKind());
4224 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4225 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004226 }
4227
4228 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4229 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4230 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4231 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
Mehdi Amini57a41912015-09-10 01:46:39 +00004232 {
4233 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4234 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4235 bytes(NewGlobalKindDeclPairs));
4236 }
4237
Douglas Gregor851443c2011-08-12 01:39:19 +00004238 // And a visible updates block for the translation unit.
4239 Abv = new llvm::BitCodeAbbrev();
4240 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4241 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004242 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4243 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4244 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004245
4246 // If we have any extern "C" names, write out a visible update for them.
4247 if (Context.ExternCContext)
4248 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004249
4250 // If the translation unit has an anonymous namespace, and we don't already
4251 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004252 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004253 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4254 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004255 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004256 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004257 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004258
Richard Smith5652c0f2014-03-21 01:48:23 +00004259 // Add update records for all mangling numbers and static local numbers.
4260 // These aren't really update records, but this is a convenient way of
4261 // tagging this rare extra data onto the declarations.
4262 for (const auto &Number : Context.MangleNumbers)
4263 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004264 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4265 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004266 for (const auto &Number : Context.StaticLocalNumbers)
4267 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004268 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4269 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004270
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004271 // Make sure visible decls, added to DeclContexts previously loaded from
4272 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004273 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004274 I = UpdatingVisibleDecls.begin(),
4275 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4276 GetDeclRef(*I);
4277 }
4278
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004279 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004280 // serialization, if we're storing decls with identifiers.
4281 if (!WritingModule || !getLangOpts().CPlusPlus) {
4282 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4283 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4284 IDEnd = PP.getIdentifierTable().end();
4285 ID != IDEnd; ++ID) {
4286 const IdentifierInfo *II = ID->second;
4287 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4288 IIs.push_back(II);
4289 }
4290 // Sort the identifiers to visit based on their name.
4291 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4292 for (const IdentifierInfo *II : IIs) {
4293 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4294 DEnd = SemaRef.IdResolver.end();
4295 D != DEnd; ++D) {
4296 GetDeclRef(*D);
4297 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004298 }
4299 }
4300
Douglas Gregor5204bde2011-08-02 16:26:37 +00004301 // Form the record of special types.
4302 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004303 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004304 AddTypeRef(Context.getFILEType(), SpecialTypes);
4305 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4306 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4307 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4308 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004309 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004310 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004311
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004312 if (Chain) {
4313 // Write the mapping information describing our module dependencies and how
4314 // each of those modules were mapped into our own offset/ID space, so that
4315 // the reader can build the appropriate mapping to its own offset/ID space.
4316 // The map consists solely of a blob with the following format:
4317 // *(module-name-len:i16 module-name:len*i8
4318 // source-location-offset:i32
4319 // identifier-id:i32
4320 // preprocessed-entity-id:i32
4321 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004322 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004323 // selector-id:i32
4324 // declaration-id:i32
4325 // c++-base-specifiers-id:i32
4326 // type-id:i32)
4327 //
4328 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4329 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4331 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004332 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004333 {
4334 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004335 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004336 using namespace llvm::support;
4337 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004338 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004339 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004340 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004341
Ben Langmuir785180e2014-10-20 16:27:30 +00004342 // Note: if a base ID was uint max, it would not be possible to load
4343 // another module after it or have more than one entity inside it.
4344 uint32_t None = std::numeric_limits<uint32_t>::max();
4345
4346 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4347 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4348 if (ShouldWrite)
4349 LE.write<uint32_t>(BaseID);
4350 else
4351 LE.write<uint32_t>(None);
4352 };
4353
Ben Langmuirfe971d92014-08-16 04:54:18 +00004354 // These values should be unique within a chain, since they will be read
4355 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004356 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4357 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4358 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4359 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4360 M->NumPreprocessedEntities);
4361 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4362 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4363 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4364 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004365 }
4366 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004367 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004368 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4369 Buffer.data(), Buffer.size());
4370 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004371
Richard Smithb9eab6d2014-03-20 19:44:17 +00004372 RecordData DeclUpdatesOffsetsRecord;
4373
Richard Smith59442b42014-03-20 20:07:19 +00004374 // Keep writing types, declarations, and declaration update records
4375 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004376 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4377 WriteTypeAbbrevs();
4378 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004379 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4380 E = DeclsToRewrite.end();
4381 I != E; ++I)
4382 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004383 do {
4384 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4385 while (!DeclTypesToEmit.empty()) {
4386 DeclOrType DOT = DeclTypesToEmit.front();
4387 DeclTypesToEmit.pop();
4388 if (DOT.isType())
4389 WriteType(DOT.getType());
4390 else
4391 WriteDecl(Context, DOT.getDecl());
4392 }
4393 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004394 Stream.ExitBlock();
4395
Richard Smithb9eab6d2014-03-20 19:44:17 +00004396 DoneWritingDeclsAndTypes = true;
4397
4398 // These things can only be done once we've written out decls and types.
4399 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004400 if (!DeclUpdatesOffsetsRecord.empty())
4401 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004402 WriteCXXBaseSpecifiersOffsets();
Richard Smithc2bb8182015-03-24 06:36:48 +00004403 WriteCXXCtorInitializersOffsets();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004404 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004405 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004406
4407 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004408 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004409 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004410 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004411 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004412 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004413 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004414 WriteFPPragmaOptions(SemaRef.getFPOptions());
4415 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004416 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004417
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004418 // If we're emitting a module, write out the submodule information.
4419 if (WritingModule)
4420 WriteSubmodules(WritingModule);
4421
Douglas Gregor5204bde2011-08-02 16:26:37 +00004422 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4423
Douglas Gregord4df8652009-04-22 22:02:47 +00004424 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004425 if (!EagerlyDeserializedDecls.empty())
4426 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004427
4428 // Write the record containing tentative definitions.
4429 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004430 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004431
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004432 // Write the record containing unused file scoped decls.
4433 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004434 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004435
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004436 // Write the record containing weak undeclared identifiers.
4437 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004438 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004439 WeakUndeclaredIdentifiers);
4440
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004441 // Write the record containing ext_vector type names.
4442 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004443 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004444
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004445 // Write the record containing VTable uses information.
4446 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004447 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004448
Nico Weber72889432014-09-06 01:25:55 +00004449 // Write the record containing potentially unused local typedefs.
4450 if (!UnusedLocalTypedefNameCandidates.empty())
4451 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4452 UnusedLocalTypedefNameCandidates);
4453
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004454 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004455 if (!PendingInstantiations.empty())
4456 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004457
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004458 // Write the record containing declaration references of Sema.
4459 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004460 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004461
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004462 // Write the record containing CUDA-specific declaration references.
4463 if (!CUDASpecialDeclRefs.empty())
4464 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004465
4466 // Write the delegating constructors.
4467 if (!DelegatingCtorDecls.empty())
4468 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004469
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004470 // Write the known namespaces.
4471 if (!KnownNamespaces.empty())
4472 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004473
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004474 // Write the undefined internal functions and variables, and inline functions.
4475 if (!UndefinedButUsed.empty())
4476 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004477
4478 if (!DeleteExprsToAnalyze.empty())
4479 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4480
Douglas Gregor851443c2011-08-12 01:39:19 +00004481 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004482 for (auto *DC : UpdatedDeclContexts)
4483 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004484
Douglas Gregor959bb062011-12-03 01:15:29 +00004485 if (!WritingModule) {
4486 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004487 struct ModuleInfo {
4488 uint64_t ID;
4489 Module *M;
4490 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4491 };
Richard Smith56be7542014-03-21 00:33:59 +00004492 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004493 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004494 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004495 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4496 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004497 }
Richard Smith56be7542014-03-21 00:33:59 +00004498
4499 if (!Imports.empty()) {
4500 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4501 return A.ID < B.ID;
4502 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004503 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4504 return A.ID == B.ID;
4505 };
Richard Smith56be7542014-03-21 00:33:59 +00004506
4507 // Sort and deduplicate module IDs.
4508 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004509 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004510 Imports.end());
4511
4512 RecordData ImportedModules;
4513 for (const auto &Import : Imports) {
4514 ImportedModules.push_back(Import.ID);
4515 // FIXME: If the module has macros imported then later has declarations
4516 // imported, this location won't be the right one as a location for the
4517 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004518 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004519 }
4520
Douglas Gregor959bb062011-12-03 01:15:29 +00004521 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4522 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004523 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004524
Douglas Gregor851443c2011-08-12 01:39:19 +00004525 WriteDeclReplacementsBlock();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004526 WriteObjCCategories();
Dario Domizioli13a0a382014-05-23 12:13:25 +00004527 if(!WritingModule)
4528 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004529
Douglas Gregor08f01292009-04-17 22:13:46 +00004530 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004531 RecordData::value_type Record[] = {
4532 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004533 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004534 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004535
4536 return Signature;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004537}
4538
Richard Smithb9eab6d2014-03-20 19:44:17 +00004539void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004540 if (DeclUpdates.empty())
4541 return;
4542
Richard Smith59442b42014-03-20 20:07:19 +00004543 DeclUpdateMap LocalUpdates;
4544 LocalUpdates.swap(DeclUpdates);
4545
Richard Smith6ef42932014-03-20 21:02:00 +00004546 for (auto &DeclUpdate : LocalUpdates) {
4547 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004548 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004549 continue; // The decl will be written completely,no need to store updates.
4550
Richard Smithd28ac5b2014-03-22 23:33:22 +00004551 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004552 RecordData Record;
4553 for (auto &Update : DeclUpdate.second) {
4554 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4555
4556 Record.push_back(Kind);
4557 switch (Kind) {
4558 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4559 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4560 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004561 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004562 Record.push_back(GetDeclRef(Update.getDecl()));
4563 break;
4564
Richard Smith4d235792014-08-07 18:53:08 +00004565 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004566 // An updated body is emitted last, so that the reader doesn't need
4567 // to skip over the lazy body to reach statements for other records.
4568 Record.pop_back();
4569 HasUpdatedBody = true;
4570 break;
4571
Richard Smith4d235792014-08-07 18:53:08 +00004572 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4573 AddSourceLocation(Update.getLoc(), Record);
4574 break;
4575
Richard Smithcd45dbc2014-04-19 03:48:30 +00004576 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4577 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004578 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004579 AddCXXDefinitionData(RD, Record);
4580 Record.push_back(WriteDeclContextLexicalBlock(
4581 *Context, const_cast<CXXRecordDecl *>(RD)));
4582
4583 // This state is sometimes updated by template instantiation, when we
4584 // switch from the specialization referring to the template declaration
4585 // to it referring to the template definition.
4586 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4587 Record.push_back(MSInfo->getTemplateSpecializationKind());
4588 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4589 } else {
4590 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4591 Record.push_back(Spec->getTemplateSpecializationKind());
4592 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004593
4594 // The instantiation might have been resolved to a partial
4595 // specialization. If so, record which one.
4596 auto From = Spec->getInstantiatedFrom();
4597 if (auto PartialSpec =
4598 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4599 Record.push_back(true);
4600 AddDeclRef(PartialSpec, Record);
4601 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4602 Record);
4603 } else {
4604 Record.push_back(false);
4605 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004606 }
4607 Record.push_back(RD->getTagKind());
4608 AddSourceLocation(RD->getLocation(), Record);
4609 AddSourceLocation(RD->getLocStart(), Record);
4610 AddSourceLocation(RD->getRBraceLoc(), Record);
4611
4612 // Instantiation may change attributes; write them all out afresh.
4613 Record.push_back(D->hasAttrs());
4614 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004615 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4616 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004617
4618 // FIXME: Ensure we don't get here for explicit instantiations.
4619 break;
4620 }
4621
Richard Smithf8134002015-03-10 01:41:22 +00004622 case UPD_CXX_RESOLVED_DTOR_DELETE:
4623 AddDeclRef(Update.getDecl(), Record);
4624 break;
4625
Richard Smith564417a2014-03-20 21:47:22 +00004626 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4627 addExceptionSpec(
4628 *this,
4629 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4630 Record);
4631 break;
4632
Richard Smith6ef42932014-03-20 21:02:00 +00004633 case UPD_CXX_DEDUCED_RETURN_TYPE:
4634 Record.push_back(GetOrCreateTypeID(Update.getType()));
4635 break;
4636
4637 case UPD_DECL_MARKED_USED:
4638 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004639
4640 case UPD_MANGLING_NUMBER:
4641 case UPD_STATIC_LOCAL_NUMBER:
4642 Record.push_back(Update.getNumber());
4643 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004644
Alexey Bataev97720002014-11-11 04:05:39 +00004645 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4646 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4647 Record);
4648 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004649
4650 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004651 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004652 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004653
4654 case UPD_ADDED_ATTR_TO_RECORD:
4655 WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record);
4656 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004657 }
4658 }
4659
Richard Smithd28ac5b2014-03-22 23:33:22 +00004660 if (HasUpdatedBody) {
4661 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004662 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004663 Record.push_back(Def->isInlined());
4664 AddSourceLocation(Def->getInnerLocStart(), Record);
4665 AddFunctionDefinition(Def, Record);
4666 }
4667
Richard Smithcd45dbc2014-04-19 03:48:30 +00004668 OffsetsRecord.push_back(GetDeclRef(D));
4669 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4670
Richard Smith6ef42932014-03-20 21:02:00 +00004671 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004672
Richard Smithc2bb8182015-03-24 06:36:48 +00004673 FlushPendingAfterDecl();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004674 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004675}
4676
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004677void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004678 if (ReplacedDecls.empty())
4679 return;
4680
4681 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004682 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4683 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004684 Record.push_back(I->ID);
4685 Record.push_back(I->Offset);
4686 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004687 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004688 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004689}
4690
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004691void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004692 Record.push_back(Loc.getRawEncoding());
4693}
4694
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004695void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004696 AddSourceLocation(Range.getBegin(), Record);
4697 AddSourceLocation(Range.getEnd(), Record);
4698}
4699
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004700void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004701 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004702 const uint64_t *Words = Value.getRawData();
4703 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004704}
4705
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004706void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004707 Record.push_back(Value.isUnsigned());
4708 AddAPInt(Value, Record);
4709}
4710
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004711void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004712 AddAPInt(Value.bitcastToAPInt(), Record);
4713}
4714
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004715void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004716 Record.push_back(getIdentifierRef(II));
4717}
4718
Sebastian Redl539c5062010-08-18 23:57:32 +00004719IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004720 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004721 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004722
Sebastian Redl539c5062010-08-18 23:57:32 +00004723 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004724 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004725 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004726 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004727}
4728
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004729MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004730 // Don't emit builtin macros like __LINE__ to the AST file unless they
4731 // have been redefined by the header (in which case they are not
4732 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004733 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004734 return 0;
4735
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004736 MacroID &ID = MacroIDs[MI];
4737 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004738 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004739 MacroInfoToEmitData Info = { Name, MI, ID };
4740 MacroInfosToEmit.push_back(Info);
4741 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004742 return ID;
4743}
4744
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004745MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004746 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004747 return 0;
4748
4749 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4750 return MacroIDs[MI];
4751}
4752
4753uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00004754 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004755}
4756
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004757void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004758 Record.push_back(getSelectorRef(SelRef));
4759}
4760
Sebastian Redl539c5062010-08-18 23:57:32 +00004761SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004762 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004763 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004764 }
4765
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004766 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004767 if (SID == 0 && Chain) {
4768 // This might trigger a ReadSelector callback, which will set the ID for
4769 // this selector.
4770 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004771 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004772 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004773 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004774 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004775 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004776 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004777 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004778}
4779
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004780void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004781 AddDeclRef(Temp->getDestructor(), Record);
4782}
4783
Richard Smithc2bb8182015-03-24 06:36:48 +00004784void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
4785 RecordDataImpl &Record) {
4786 assert(!Inits.empty() && "Empty ctor initializer sets are not recorded");
4787 CXXCtorInitializersToWrite.push_back(
4788 QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits));
4789 Record.push_back(NextCXXCtorInitializersID++);
4790}
4791
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004792void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
Richard Smithc2bb8182015-03-24 06:36:48 +00004793 CXXBaseSpecifier const *BasesEnd,
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004794 RecordDataImpl &Record) {
4795 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4796 CXXBaseSpecifiersToWrite.push_back(
4797 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4798 Bases, BasesEnd));
4799 Record.push_back(NextCXXBaseSpecifiersID++);
4800}
4801
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004802void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004803 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004804 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004805 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004806 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004807 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004808 break;
4809 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004810 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004811 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004812 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004813 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004814 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004815 break;
4816 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004817 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004818 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004819 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004820 break;
John McCall0ad16662009-10-29 08:12:44 +00004821 case TemplateArgument::Null:
4822 case TemplateArgument::Integral:
4823 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004824 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004825 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004826 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004827 break;
4828 }
4829}
4830
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004831void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004832 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004833 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004834
4835 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4836 bool InfoHasSameExpr
4837 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4838 Record.push_back(InfoHasSameExpr);
4839 if (InfoHasSameExpr)
4840 return; // Avoid storing the same expr twice.
4841 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004842 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4843 Record);
4844}
4845
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004846void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4847 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004848 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004849 AddTypeRef(QualType(), Record);
4850 return;
4851 }
4852
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004853 AddTypeLoc(TInfo->getTypeLoc(), Record);
4854}
4855
4856void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4857 AddTypeRef(TL.getType(), Record);
4858
John McCall8f115c62009-10-16 21:56:05 +00004859 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004860 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004861 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004862}
4863
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004864void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004865 Record.push_back(GetOrCreateTypeID(T));
4866}
4867
Richard Smith2095ffe2015-03-16 20:11:03 +00004868TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004869 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004870 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4871 if (T.isNull())
4872 return TypeIdx();
4873 assert(!T.getLocalFastQualifiers());
4874
4875 TypeIdx &Idx = TypeIdxs[T];
4876 if (Idx.getIndex() == 0) {
4877 if (DoneWritingDeclsAndTypes) {
4878 assert(0 && "New type seen after serializing all the types to emit!");
4879 return TypeIdx();
4880 }
4881
4882 // We haven't seen this type before. Assign it a new ID and put it
4883 // into the queue of types to emit.
4884 Idx = TypeIdx(NextTypeID++);
4885 DeclTypesToEmit.push(T);
4886 }
4887 return Idx;
4888 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004889}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004890
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004891TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004892 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00004893 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4894 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004895 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00004896 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004897
Richard Smith2095ffe2015-03-16 20:11:03 +00004898 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4899 assert(I != TypeIdxs.end() && "Type not emitted!");
4900 return I->second;
4901 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004902}
4903
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004904void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004905 Record.push_back(GetDeclRef(D));
4906}
4907
Sebastian Redl539c5062010-08-18 23:57:32 +00004908DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004909 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00004910
4911 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004912 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004913 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004914
4915 // If D comes from an AST file, its declaration ID is already known and
4916 // fixed.
4917 if (D->isFromASTFile())
4918 return D->getGlobalID();
4919
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004920 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004921 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004922 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004923 if (DoneWritingDeclsAndTypes) {
4924 assert(0 && "New decl seen after serializing all the decls to emit!");
4925 return 0;
4926 }
4927
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004928 // We haven't seen this declaration before. Give it a new ID and
4929 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004930 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004931 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004932 }
4933
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004934 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004935}
4936
Sebastian Redl539c5062010-08-18 23:57:32 +00004937DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00004938 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00004939 return 0;
4940
Douglas Gregorb3163e52012-01-05 22:33:30 +00004941 // If D comes from an AST file, its declaration ID is already known and
4942 // fixed.
4943 if (D->isFromASTFile())
4944 return D->getGlobalID();
4945
Douglas Gregore84a9da2009-04-20 20:36:09 +00004946 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4947 return DeclIDs[D];
4948}
4949
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004950void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004951 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004952 assert(D);
4953
4954 SourceLocation Loc = D->getLocation();
4955 if (Loc.isInvalid())
4956 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004957
4958 // We only keep track of the file-level declarations of each file.
4959 if (!D->getLexicalDeclContext()->isFileContext())
4960 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004961 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4962 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004963 if (isa<ParmVarDecl>(D))
4964 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004965
4966 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004967 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004968 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004969 FileID FID;
4970 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004971 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004972 if (FID.isInvalid())
4973 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004974 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004975
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004976 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004977 if (!Info)
4978 Info = new DeclIDInFileInfo();
4979
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004980 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004981 LocDeclIDsTy &Decls = Info->DeclIDs;
4982
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004983 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004984 Decls.push_back(LocDecl);
4985 return;
4986 }
4987
Benjamin Kramer45025c02013-08-24 13:22:59 +00004988 LocDeclIDsTy::iterator I =
4989 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004990
4991 Decls.insert(I, LocDecl);
4992}
4993
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004994void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004995 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004996 Record.push_back(Name.getNameKind());
4997 switch (Name.getNameKind()) {
4998 case DeclarationName::Identifier:
4999 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5000 break;
5001
5002 case DeclarationName::ObjCZeroArgSelector:
5003 case DeclarationName::ObjCOneArgSelector:
5004 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005005 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005006 break;
5007
5008 case DeclarationName::CXXConstructorName:
5009 case DeclarationName::CXXDestructorName:
5010 case DeclarationName::CXXConversionFunctionName:
5011 AddTypeRef(Name.getCXXNameType(), Record);
5012 break;
5013
5014 case DeclarationName::CXXOperatorName:
5015 Record.push_back(Name.getCXXOverloadedOperator());
5016 break;
5017
Alexis Hunt3d221f22009-11-29 07:34:05 +00005018 case DeclarationName::CXXLiteralOperatorName:
5019 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5020 break;
5021
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005022 case DeclarationName::CXXUsingDirective:
5023 // No extra data to emit
5024 break;
5025 }
5026}
Chris Lattnerca025db2010-05-07 21:43:38 +00005027
Richard Smithd08aeb62014-08-28 01:33:39 +00005028unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5029 assert(needsAnonymousDeclarationNumber(D) &&
5030 "expected an anonymous declaration");
5031
5032 // Number the anonymous declarations within this context, if we've not
5033 // already done so.
5034 auto It = AnonymousDeclarationNumbers.find(D);
5035 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005036 auto *DC = D->getLexicalDeclContext();
5037 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5038 AnonymousDeclarationNumbers[ND] = Number;
5039 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005040
5041 It = AnonymousDeclarationNumbers.find(D);
5042 assert(It != AnonymousDeclarationNumbers.end() &&
5043 "declaration not found within its lexical context");
5044 }
5045
5046 return It->second;
5047}
5048
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005049void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005050 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005051 switch (Name.getNameKind()) {
5052 case DeclarationName::CXXConstructorName:
5053 case DeclarationName::CXXDestructorName:
5054 case DeclarationName::CXXConversionFunctionName:
5055 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5056 break;
5057
5058 case DeclarationName::CXXOperatorName:
5059 AddSourceLocation(
5060 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5061 Record);
5062 AddSourceLocation(
5063 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5064 Record);
5065 break;
5066
5067 case DeclarationName::CXXLiteralOperatorName:
5068 AddSourceLocation(
5069 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5070 Record);
5071 break;
5072
5073 case DeclarationName::Identifier:
5074 case DeclarationName::ObjCZeroArgSelector:
5075 case DeclarationName::ObjCOneArgSelector:
5076 case DeclarationName::ObjCMultiArgSelector:
5077 case DeclarationName::CXXUsingDirective:
5078 break;
5079 }
5080}
5081
5082void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005083 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005084 AddDeclarationName(NameInfo.getName(), Record);
5085 AddSourceLocation(NameInfo.getLoc(), Record);
5086 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5087}
5088
5089void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005090 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005091 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005092 Record.push_back(Info.NumTemplParamLists);
5093 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5094 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5095}
5096
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005097void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005098 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005099 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005100 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005101 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005102
5103 // Push each of the NNS's onto a stack for serialization in reverse order.
5104 while (NNS) {
5105 NestedNames.push_back(NNS);
5106 NNS = NNS->getPrefix();
5107 }
5108
5109 Record.push_back(NestedNames.size());
5110 while(!NestedNames.empty()) {
5111 NNS = NestedNames.pop_back_val();
5112 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5113 Record.push_back(Kind);
5114 switch (Kind) {
5115 case NestedNameSpecifier::Identifier:
5116 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5117 break;
5118
5119 case NestedNameSpecifier::Namespace:
5120 AddDeclRef(NNS->getAsNamespace(), Record);
5121 break;
5122
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005123 case NestedNameSpecifier::NamespaceAlias:
5124 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5125 break;
5126
Chris Lattnerca025db2010-05-07 21:43:38 +00005127 case NestedNameSpecifier::TypeSpec:
5128 case NestedNameSpecifier::TypeSpecWithTemplate:
5129 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5130 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5131 break;
5132
5133 case NestedNameSpecifier::Global:
5134 // Don't need to write an associated value.
5135 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005136
5137 case NestedNameSpecifier::Super:
5138 AddDeclRef(NNS->getAsRecordDecl(), Record);
5139 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005140 }
5141 }
5142}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005143
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005144void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5145 RecordDataImpl &Record) {
5146 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005147 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005148 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005149
5150 // Push each of the nested-name-specifiers's onto a stack for
5151 // serialization in reverse order.
5152 while (NNS) {
5153 NestedNames.push_back(NNS);
5154 NNS = NNS.getPrefix();
5155 }
5156
5157 Record.push_back(NestedNames.size());
5158 while(!NestedNames.empty()) {
5159 NNS = NestedNames.pop_back_val();
5160 NestedNameSpecifier::SpecifierKind Kind
5161 = NNS.getNestedNameSpecifier()->getKind();
5162 Record.push_back(Kind);
5163 switch (Kind) {
5164 case NestedNameSpecifier::Identifier:
5165 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5166 AddSourceRange(NNS.getLocalSourceRange(), Record);
5167 break;
5168
5169 case NestedNameSpecifier::Namespace:
5170 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5171 AddSourceRange(NNS.getLocalSourceRange(), Record);
5172 break;
5173
5174 case NestedNameSpecifier::NamespaceAlias:
5175 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5176 AddSourceRange(NNS.getLocalSourceRange(), Record);
5177 break;
5178
5179 case NestedNameSpecifier::TypeSpec:
5180 case NestedNameSpecifier::TypeSpecWithTemplate:
5181 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5182 AddTypeLoc(NNS.getTypeLoc(), Record);
5183 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5184 break;
5185
5186 case NestedNameSpecifier::Global:
5187 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5188 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005189
5190 case NestedNameSpecifier::Super:
5191 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5192 AddSourceRange(NNS.getLocalSourceRange(), Record);
5193 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005194 }
5195 }
5196}
5197
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005198void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005199 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005200 Record.push_back(Kind);
5201 switch (Kind) {
5202 case TemplateName::Template:
5203 AddDeclRef(Name.getAsTemplateDecl(), Record);
5204 break;
5205
5206 case TemplateName::OverloadedTemplate: {
5207 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5208 Record.push_back(OvT->size());
5209 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5210 I != E; ++I)
5211 AddDeclRef(*I, Record);
5212 break;
5213 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005214
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005215 case TemplateName::QualifiedTemplate: {
5216 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5217 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5218 Record.push_back(QualT->hasTemplateKeyword());
5219 AddDeclRef(QualT->getTemplateDecl(), Record);
5220 break;
5221 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005222
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005223 case TemplateName::DependentTemplate: {
5224 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5225 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5226 Record.push_back(DepT->isIdentifier());
5227 if (DepT->isIdentifier())
5228 AddIdentifierRef(DepT->getIdentifier(), Record);
5229 else
5230 Record.push_back(DepT->getOperator());
5231 break;
5232 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005233
5234 case TemplateName::SubstTemplateTemplateParm: {
5235 SubstTemplateTemplateParmStorage *subst
5236 = Name.getAsSubstTemplateTemplateParm();
5237 AddDeclRef(subst->getParameter(), Record);
5238 AddTemplateName(subst->getReplacement(), Record);
5239 break;
5240 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005241
5242 case TemplateName::SubstTemplateTemplateParmPack: {
5243 SubstTemplateTemplateParmPackStorage *SubstPack
5244 = Name.getAsSubstTemplateTemplateParmPack();
5245 AddDeclRef(SubstPack->getParameterPack(), Record);
5246 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5247 break;
5248 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005249 }
5250}
5251
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005252void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005253 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005254 Record.push_back(Arg.getKind());
5255 switch (Arg.getKind()) {
5256 case TemplateArgument::Null:
5257 break;
5258 case TemplateArgument::Type:
5259 AddTypeRef(Arg.getAsType(), Record);
5260 break;
5261 case TemplateArgument::Declaration:
5262 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005263 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005264 break;
5265 case TemplateArgument::NullPtr:
5266 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005267 break;
5268 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005269 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005270 AddTypeRef(Arg.getIntegralType(), Record);
5271 break;
5272 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005273 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5274 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005275 case TemplateArgument::TemplateExpansion:
5276 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005277 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005278 Record.push_back(*NumExpansions + 1);
5279 else
5280 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005281 break;
5282 case TemplateArgument::Expression:
5283 AddStmt(Arg.getAsExpr());
5284 break;
5285 case TemplateArgument::Pack:
5286 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005287 for (const auto &P : Arg.pack_elements())
5288 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005289 break;
5290 }
5291}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005292
5293void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005294ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005295 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005296 assert(TemplateParams && "No TemplateParams!");
5297 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5298 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5299 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5300 Record.push_back(TemplateParams->size());
5301 for (TemplateParameterList::const_iterator
5302 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5303 P != PEnd; ++P)
5304 AddDeclRef(*P, Record);
5305}
5306
5307/// \brief Emit a template argument list.
5308void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005309ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005310 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005311 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005312 Record.push_back(TemplateArgs->size());
5313 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005314 AddTemplateArgument(TemplateArgs->get(i), Record);
5315}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005316
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005317void
5318ASTWriter::AddASTTemplateArgumentListInfo
5319(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5320 assert(ASTTemplArgList && "No ASTTemplArgList!");
5321 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5322 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5323 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5324 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5325 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5326 AddTemplateArgumentLoc(TemplArgs[i], Record);
5327}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005328
5329void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005330ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005331 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005332 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005333 I = Set.begin(), E = Set.end(); I != E; ++I) {
5334 AddDeclRef(I.getDecl(), Record);
5335 Record.push_back(I.getAccess());
5336 }
5337}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005338
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005339void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005340 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005341 Record.push_back(Base.isVirtual());
5342 Record.push_back(Base.isBaseOfClass());
5343 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005344 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005345 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005346 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005347 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5348 : SourceLocation(),
5349 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005350}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005351
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005352void ASTWriter::FlushCXXBaseSpecifiers() {
5353 RecordData Record;
Richard Smithc2bb8182015-03-24 06:36:48 +00005354 unsigned N = CXXBaseSpecifiersToWrite.size();
5355 for (unsigned I = 0; I != N; ++I) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005356 Record.clear();
5357
5358 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005359 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005360 if (Index == CXXBaseSpecifiersOffsets.size())
5361 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5362 else {
5363 if (Index > CXXBaseSpecifiersOffsets.size())
5364 CXXBaseSpecifiersOffsets.resize(Index + 1);
5365 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5366 }
5367
5368 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5369 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5370 Record.push_back(BEnd - B);
5371 for (; B != BEnd; ++B)
5372 AddCXXBaseSpecifier(*B, Record);
5373 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005374
5375 // Flush any expressions that were written as part of the base specifiers.
5376 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005377 }
5378
Richard Smithc2bb8182015-03-24 06:36:48 +00005379 assert(N == CXXBaseSpecifiersToWrite.size() &&
5380 "added more base specifiers while writing base specifiers");
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005381 CXXBaseSpecifiersToWrite.clear();
5382}
5383
Alexis Hunt1d792652011-01-08 20:30:50 +00005384void ASTWriter::AddCXXCtorInitializers(
5385 const CXXCtorInitializer * const *CtorInitializers,
5386 unsigned NumCtorInitializers,
5387 RecordDataImpl &Record) {
5388 Record.push_back(NumCtorInitializers);
5389 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5390 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005391
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005392 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005393 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005394 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005395 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005396 } else if (Init->isDelegatingInitializer()) {
5397 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005398 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005399 } else if (Init->isMemberInitializer()){
5400 Record.push_back(CTOR_INITIALIZER_MEMBER);
5401 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005402 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005403 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5404 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005405 }
Francois Pichetd583da02010-12-04 09:14:42 +00005406
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005407 AddSourceLocation(Init->getMemberLocation(), Record);
5408 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005409 AddSourceLocation(Init->getLParenLoc(), Record);
5410 AddSourceLocation(Init->getRParenLoc(), Record);
5411 Record.push_back(Init->isWritten());
5412 if (Init->isWritten()) {
5413 Record.push_back(Init->getSourceOrder());
5414 } else {
5415 Record.push_back(Init->getNumArrayIndices());
5416 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5417 AddDeclRef(Init->getArrayIndex(i), Record);
5418 }
5419 }
5420}
5421
Richard Smithc2bb8182015-03-24 06:36:48 +00005422void ASTWriter::FlushCXXCtorInitializers() {
5423 RecordData Record;
5424
5425 unsigned N = CXXCtorInitializersToWrite.size();
Daniel Jasperc77b0a12015-03-24 08:06:38 +00005426 (void)N; // Silence unused warning in non-assert builds.
Richard Smithc2bb8182015-03-24 06:36:48 +00005427 for (auto &Init : CXXCtorInitializersToWrite) {
5428 Record.clear();
5429
5430 // Record the offset of this mem-initializer list.
5431 unsigned Index = Init.ID - 1;
5432 if (Index == CXXCtorInitializersOffsets.size())
5433 CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
5434 else {
5435 if (Index > CXXCtorInitializersOffsets.size())
5436 CXXCtorInitializersOffsets.resize(Index + 1);
5437 CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
5438 }
5439
5440 AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
5441 Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
5442
5443 // Flush any expressions that were written as part of the initializers.
5444 FlushStmts();
5445 }
5446
5447 assert(N == CXXCtorInitializersToWrite.size() &&
5448 "added more ctor initializers while writing ctor initializers");
5449 CXXCtorInitializersToWrite.clear();
5450}
5451
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005452void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005453 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005454 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005455 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005456 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005457 Record.push_back(Data.Aggregate);
5458 Record.push_back(Data.PlainOldData);
5459 Record.push_back(Data.Empty);
5460 Record.push_back(Data.Polymorphic);
5461 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005462 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005463 Record.push_back(Data.HasNoNonEmptyBases);
5464 Record.push_back(Data.HasPrivateFields);
5465 Record.push_back(Data.HasProtectedFields);
5466 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005467 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005468 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005469 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005470 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005471 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005472 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5473 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5474 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5475 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5476 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5477 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005478 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005479 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005480 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005481 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005482 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005483 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005484 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005485 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005486 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005487 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005488 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5489 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5490 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5491 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005492 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005493
5494 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005495 if (Data.NumBases > 0)
5496 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5497 Record);
5498
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005499 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5500 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005501 if (Data.NumVBases > 0)
5502 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5503 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005504
Richard Smitha4ba74c2013-08-30 04:46:40 +00005505 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5506 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005507 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005508 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005509
5510 // Add lambda-specific data.
5511 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005512 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005513 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005514 Record.push_back(Lambda.IsGenericLambda);
5515 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005516 Record.push_back(Lambda.NumCaptures);
5517 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005518 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005519 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005520 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005521 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005522 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005523 AddSourceLocation(Capture.getLocation(), Record);
5524 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005525 Record.push_back(Capture.getCaptureKind());
5526 switch (Capture.getCaptureKind()) {
5527 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005528 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005529 break;
5530 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005531 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005532 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005533 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005534 AddDeclRef(Var, Record);
5535 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5536 : SourceLocation(),
5537 Record);
5538 break;
5539 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005540 }
5541 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005542}
5543
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005544void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005545 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005546 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005547 assert(FirstDeclID == NextDeclID &&
5548 FirstTypeID == NextTypeID &&
5549 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005550 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005551 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005552 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005553 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005554
Sebastian Redl07a89a82010-07-30 00:29:29 +00005555 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005556
Richard Smith7f330cd2015-03-18 01:42:29 +00005557 // Note, this will get called multiple times, once one the reader starts up
5558 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005559 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5560 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5561 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005562 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005563 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005564 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005565 NextDeclID = FirstDeclID;
5566 NextTypeID = FirstTypeID;
5567 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005568 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005569 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005570 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005571}
5572
Sebastian Redl539c5062010-08-18 23:57:32 +00005573void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005574 // Always keep the highest ID. See \p TypeRead() for more information.
5575 IdentID &StoredID = IdentifierIDs[II];
5576 if (ID > StoredID)
5577 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005578}
5579
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005580void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005581 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005582 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005583 if (ID > StoredID)
5584 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005585}
5586
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005587void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005588 // Always take the highest-numbered type index. This copes with an interesting
5589 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005590 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005591 // keep the higher-numbered entry so that we can properly write it out to
5592 // the AST file.
5593 TypeIdx &StoredIdx = TypeIdxs[T];
5594 if (Idx.getIndex() >= StoredIdx.getIndex())
5595 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005596}
5597
Sebastian Redl539c5062010-08-18 23:57:32 +00005598void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005599 // Always keep the highest ID. See \p TypeRead() for more information.
5600 SelectorID &StoredID = SelectorIDs[S];
5601 if (ID > StoredID)
5602 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005603}
Douglas Gregor91096292010-10-02 19:29:26 +00005604
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005605void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005606 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005607 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005608 MacroDefinitions[MD] = ID;
5609}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005610
Douglas Gregore37a85a2011-12-02 17:30:13 +00005611void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5612 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5613 SubmoduleIDs[Mod] = ID;
5614}
5615
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005616void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005617 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005618 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005619 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5620 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005621 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005622 // A forward reference was mutated into a definition. Rewrite it.
5623 // FIXME: This happens during template instantiation, should we
5624 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005625 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5626 "completed a tag from another module but not by instantiation?");
5627 DeclUpdates[RD].push_back(
5628 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005629 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005630 }
5631}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005632
Richard Smithf983f7f2015-10-13 00:23:25 +00005633static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5634 if (D->isFromASTFile())
5635 return true;
5636
5637 // If we've not loaded any modules, this can't be imported.
5638 if (!Chain || !Chain->getModuleManager().size())
5639 return false;
5640
5641 // The predefined __va_list_tag struct is imported if we imported any decls.
5642 // FIXME: This is a gross hack.
5643 return D == D->getASTContext().getVaListTagDecl();
5644}
5645
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005646void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5647 // TU and namespaces are handled elsewhere.
5648 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5649 return;
5650
Richard Smithf983f7f2015-10-13 00:23:25 +00005651 // We're only interested in cases where a local declaration is added to an
5652 // imported context.
5653 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5654 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005655
Richard Smith0f4e2c42015-08-06 04:23:48 +00005656 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005657 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005658 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005659 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5660 // We're adding a visible declaration to a predefined decl context. Ensure
5661 // that we write out all of its lookup results so we don't get a nasty
5662 // surprise when we try to emit its lookup table.
5663 for (auto *Child : DC->decls())
5664 UpdatingVisibleDecls.push_back(Child);
5665 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005666 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005667}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005668
5669void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5670 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005671
5672 // We're only interested in cases where a local declaration is added to an
5673 // imported context.
5674 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5675 return;
5676
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005677 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005678 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005679
5680 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005681 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005682 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005683 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005684}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005685
Richard Smith564417a2014-03-20 21:47:22 +00005686void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005687 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5688 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005689 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005690 // If we don't already know the exception specification for this redecl
5691 // chain, add an update record for it.
5692 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5693 ->getType()
5694 ->castAs<FunctionProtoType>()
5695 ->getExceptionSpecType()))
5696 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5697 });
Richard Smith564417a2014-03-20 21:47:22 +00005698}
5699
Richard Smith1fa5d642013-05-11 05:45:24 +00005700void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5701 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005702 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005703 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005704 DeclUpdates[D].push_back(
5705 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5706 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005707}
5708
Richard Smithf8134002015-03-10 01:41:22 +00005709void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5710 const FunctionDecl *Delete) {
5711 assert(!WritingAST && "Already writing the AST!");
5712 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005713 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005714 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005715 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5716 });
Richard Smithf8134002015-03-10 01:41:22 +00005717}
5718
Sebastian Redlab238a72011-04-24 16:28:06 +00005719void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005720 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005721 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005722 return; // Declaration not imported from PCH.
5723
Richard Smith4d235792014-08-07 18:53:08 +00005724 // Implicit function decl from a PCH was defined.
5725 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005726}
5727
Richard Smithd28ac5b2014-03-22 23:33:22 +00005728void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5729 assert(!WritingAST && "Already writing the AST!");
5730 if (!D->isFromASTFile())
5731 return;
5732
Richard Smith9e2341d2015-03-23 03:25:59 +00005733 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005734}
5735
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005736void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005737 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005738 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005739 return;
5740
5741 // Since the actual instantiation is delayed, this really means that we need
5742 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005743 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005744 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5745 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005746}
5747
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005748void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5749 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005750 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005751 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005752 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005753
5754 assert(IFD->getDefinition() && "Category on a class without a definition?");
5755 ObjCClassesWithCategories.insert(
5756 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005757}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005758
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005759
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005760void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5761 const ObjCPropertyDecl *OrigProp,
5762 const ObjCCategoryDecl *ClassExt) {
5763 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5764 if (!D)
5765 return;
5766
5767 assert(!WritingAST && "Already writing the AST!");
5768 if (!D->isFromASTFile())
5769 return; // Declaration not imported from PCH.
5770
5771 RewriteDecl(D);
5772}
Eli Friedman276dd182013-09-05 00:02:25 +00005773
5774void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5775 assert(!WritingAST && "Already writing the AST!");
5776 if (!D->isFromASTFile())
5777 return;
5778
Aaron Ballman4f45b712014-03-21 15:22:56 +00005779 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005780}
Alexey Bataev97720002014-11-11 04:05:39 +00005781
5782void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5783 assert(!WritingAST && "Already writing the AST!");
5784 if (!D->isFromASTFile())
5785 return;
5786
5787 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5788}
Richard Smith65ebb4a2015-03-26 04:09:53 +00005789
Richard Smith4caa4492015-05-15 02:34:32 +00005790void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Richard Smith65ebb4a2015-03-26 04:09:53 +00005791 assert(!WritingAST && "Already writing the AST!");
5792 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00005793 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005794}
Alex Denisovfde64952015-06-26 05:28:36 +00005795
5796void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5797 const RecordDecl *Record) {
5798 assert(!WritingAST && "Already writing the AST!");
5799 if (!Record->isFromASTFile())
5800 return;
5801 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5802}