blob: 000a7a9001f407b1b7f5ccbbf4fe6eacfceaa398 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Richard Smith961eae52014-03-25 01:14:22 +000020#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000026#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000028#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000032#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000034#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000035#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/HeaderSearchOptions.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Lex/PreprocessingRecord.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Lex/PreprocessorOptions.h"
41#include "clang/Sema/IdentifierResolver.h"
42#include "clang/Sema/Sema.h"
43#include "clang/Serialization/ASTReader.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000044#include "llvm/ADT/APFloat.h"
45#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000047#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include "llvm/Bitcode/BitstreamWriter.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000049#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000050#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000051#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000052#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000053#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000054#include "llvm/Support/Process.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000055#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000056#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000057#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000058#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000059using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000060using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061
Sebastian Redl3df5a082010-07-30 17:03:48 +000062template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000063static StringRef data(const std::vector<T, Allocator> &v) {
64 if (v.empty()) return StringRef();
65 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000066 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000067}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000068
69template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000070static StringRef data(const SmallVectorImpl<T> &v) {
71 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000072 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000073}
74
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075//===----------------------------------------------------------------------===//
76// Type serialization
77//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000078
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000080 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000081 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000082 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
84 public:
85 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000086 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +000087 /// \brief Abbreviation to use for the record, if any.
88 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000090 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000091 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092
93 void VisitArrayType(const ArrayType *T);
94 void VisitFunctionType(const FunctionType *T);
95 void VisitTagType(const TagType *T);
96
97#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
98#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099#include "clang/AST/TypeNodes.def"
100 };
101}
102
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000104 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000114 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115}
116
Reid Kleckner8a365022013-06-24 17:51:48 +0000117void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
118 Writer.AddTypeRef(T->getOriginalType(), Record);
119 Code = TYPE_DECAYED;
120}
121
Reid Kleckner0503a872013-12-05 01:23:43 +0000122void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
123 Writer.AddTypeRef(T->getOriginalType(), Record);
124 Writer.AddTypeRef(T->getAdjustedType(), Record);
125 Code = TYPE_ADJUSTED;
126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000134 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
135 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000136 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137}
138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000140 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000145 Writer.AddTypeRef(T->getPointeeType(), Record);
146 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000147 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148}
149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151 Writer.AddTypeRef(T->getElementType(), Record);
152 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000153 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154}
155
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157 VisitArrayType(T);
158 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000159 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160}
161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000164 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000169 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
170 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000171 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000172 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173}
174
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176 Writer.AddTypeRef(T->getElementType(), Record);
177 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000178 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000179 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180}
181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000184 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000188 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000189 FunctionType::ExtInfo C = T->getExtInfo();
190 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000191 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000192 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000193 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000194 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000195 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000196
197 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
198 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Richard Smith564417a2014-03-20 21:47:22 +0000206static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
207 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000208 Record.push_back(T->getExceptionSpecType());
209 if (T->getExceptionSpecType() == EST_Dynamic) {
210 Record.push_back(T->getNumExceptions());
211 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
212 Writer.AddTypeRef(T->getExceptionType(I), Record);
213 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
214 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000215 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
216 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
217 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000218 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
219 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000220 }
Richard Smith564417a2014-03-20 21:47:22 +0000221}
222
223void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
224 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000225
Richard Smith564417a2014-03-20 21:47:22 +0000226 Record.push_back(T->isVariadic());
227 Record.push_back(T->hasTrailingReturn());
228 Record.push_back(T->getTypeQuals());
229 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
230 addExceptionSpec(Writer, T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000231
232 Record.push_back(T->getNumParams());
233 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
234 Writer.AddTypeRef(T->getParamType(I), Record);
235
236 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
237 T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
238 AbbrevToUse = 0;
239
Sebastian Redl539c5062010-08-18 23:57:32 +0000240 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241}
242
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000243void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000244 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000246}
John McCallb96ec562009-12-04 22:46:56 +0000247
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000250 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
251 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000252 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000253}
254
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000255void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000256 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000257 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258}
259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000266 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000267 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000268 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000269}
270
Alexis Hunte852b102011-05-24 22:41:36 +0000271void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
272 Writer.AddTypeRef(T->getBaseType(), Record);
273 Writer.AddTypeRef(T->getUnderlyingType(), Record);
274 Record.push_back(T->getUTTKind());
275 Code = TYPE_UNARY_TRANSFORM;
276}
277
Richard Smith30482bc2011-02-20 03:19:35 +0000278void ASTTypeWriter::VisitAutoType(const AutoType *T) {
279 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000280 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000281 if (T->getDeducedType().isNull())
282 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000283 Code = TYPE_AUTO;
284}
285
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000287 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000288 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000289 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000290 "Cannot serialize in the middle of a type definition");
291}
292
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000293void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000294 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000295 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000296}
297
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000298void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000299 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000300 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000301}
302
John McCall81904512011-01-06 01:58:22 +0000303void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
304 Writer.AddTypeRef(T->getModifiedType(), Record);
305 Writer.AddTypeRef(T->getEquivalentType(), Record);
306 Record.push_back(T->getAttrKind());
307 Code = TYPE_ATTRIBUTED;
308}
309
Mike Stump11289f42009-09-09 15:08:12 +0000310void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000311ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000312 const SubstTemplateTypeParmType *T) {
313 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
314 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000315 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000316}
317
318void
Douglas Gregorada4b792011-01-14 02:55:32 +0000319ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
320 const SubstTemplateTypeParmPackType *T) {
321 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
322 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
323 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
324}
325
326void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000327ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000328 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000329 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000330 Writer.AddTemplateName(T->getTemplateName(), Record);
331 Record.push_back(T->getNumArgs());
332 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
333 ArgI != ArgE; ++ArgI)
334 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000335 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
336 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000337 : T->getCanonicalTypeInternal(),
338 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000339 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000343ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000344 VisitArrayType(T);
345 Writer.AddStmt(T->getSizeExpr());
346 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000347 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000348}
349
350void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000352 const DependentSizedExtVectorType *T) {
353 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000354 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000355}
356
357void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000358ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000359 Record.push_back(T->getDepth());
360 Record.push_back(T->getIndex());
361 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000362 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000363 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000364}
365
366void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000367ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000368 Record.push_back(T->getKeyword());
369 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
370 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000371 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
372 : T->getCanonicalTypeInternal(),
373 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000374 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000375}
376
377void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000378ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000379 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000380 Record.push_back(T->getKeyword());
381 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
382 Writer.AddIdentifierRef(T->getIdentifier(), Record);
383 Record.push_back(T->getNumArgs());
384 for (DependentTemplateSpecializationType::iterator
385 I = T->begin(), E = T->end(); I != E; ++I)
386 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000387 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388}
389
Douglas Gregord2fa7662010-12-20 02:24:11 +0000390void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
391 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000392 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000393 Record.push_back(*NumExpansions + 1);
394 else
395 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000396 Code = TYPE_PACK_EXPANSION;
397}
398
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000399void ASTTypeWriter::VisitParenType(const ParenType *T) {
400 Writer.AddTypeRef(T->getInnerType(), Record);
401 Code = TYPE_PAREN;
402}
403
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000404void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000405 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000406 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
407 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000408 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000409}
410
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000411void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000412 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000413 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000414 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000415}
416
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000417void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000418 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000419 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000420}
421
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000422void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000423 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000424 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000425 for (const auto *I : T->quals())
426 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000427 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000428}
429
Steve Narofffb4330f2009-06-17 22:40:22 +0000430void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000431ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000432 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000433 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000434}
435
Eli Friedman0dfb8892011-10-06 23:00:33 +0000436void
437ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
438 Writer.AddTypeRef(T->getValueType(), Record);
439 Code = TYPE_ATOMIC;
440}
441
John McCall8f115c62009-10-16 21:56:05 +0000442namespace {
443
444class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000445 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000446 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000447
448public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000449 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000450 : Writer(Writer), Record(Record) { }
451
John McCall17001972009-10-18 01:05:36 +0000452#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000453#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000454 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000455#include "clang/AST/TypeLocNodes.def"
456
John McCall17001972009-10-18 01:05:36 +0000457 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
458 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000459};
460
461}
462
John McCall17001972009-10-18 01:05:36 +0000463void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
464 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000465}
John McCall17001972009-10-18 01:05:36 +0000466void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000467 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
468 if (TL.needsExtraLocalData()) {
469 Record.push_back(TL.getWrittenTypeSpec());
470 Record.push_back(TL.getWrittenSignSpec());
471 Record.push_back(TL.getWrittenWidthSpec());
472 Record.push_back(TL.hasModeAttr());
473 }
John McCall8f115c62009-10-16 21:56:05 +0000474}
John McCall17001972009-10-18 01:05:36 +0000475void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000477}
John McCall17001972009-10-18 01:05:36 +0000478void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000480}
Reid Kleckner8a365022013-06-24 17:51:48 +0000481void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
482 // nothing to do
483}
Reid Kleckner0503a872013-12-05 01:23:43 +0000484void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
485 // nothing to do
486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000489}
John McCall17001972009-10-18 01:05:36 +0000490void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000492}
John McCall17001972009-10-18 01:05:36 +0000493void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000495}
John McCall17001972009-10-18 01:05:36 +0000496void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000498 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
502 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
503 Record.push_back(TL.getSizeExpr() ? 1 : 0);
504 if (TL.getSizeExpr())
505 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000506}
John McCall17001972009-10-18 01:05:36 +0000507void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
508 VisitArrayTypeLoc(TL);
509}
510void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
511 VisitArrayTypeLoc(TL);
512}
513void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
514 VisitArrayTypeLoc(TL);
515}
516void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
517 DependentSizedArrayTypeLoc TL) {
518 VisitArrayTypeLoc(TL);
519}
520void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
521 DependentSizedExtVectorTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
527void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000531 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000532 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
533 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000534 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000535 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
536 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000537}
538void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
539 VisitFunctionTypeLoc(TL);
540}
541void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
542 VisitFunctionTypeLoc(TL);
543}
John McCallb96ec562009-12-04 22:46:56 +0000544void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
John McCall17001972009-10-18 01:05:36 +0000547void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
550void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000551 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
552 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
553 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000554}
555void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000556 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
557 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
558 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
559 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000560}
561void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
Alexis Hunte852b102011-05-24 22:41:36 +0000564void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
565 Writer.AddSourceLocation(TL.getKWLoc(), Record);
566 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
567 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
568 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
569}
Richard Smith30482bc2011-02-20 03:19:35 +0000570void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCall17001972009-10-18 01:05:36 +0000573void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
576void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578}
John McCall81904512011-01-06 01:58:22 +0000579void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
581 if (TL.hasAttrOperand()) {
582 SourceRange range = TL.getAttrOperandParensRange();
583 Writer.AddSourceLocation(range.getBegin(), Record);
584 Writer.AddSourceLocation(range.getEnd(), Record);
585 }
586 if (TL.hasAttrExprOperand()) {
587 Expr *operand = TL.getAttrExprOperand();
588 Record.push_back(operand ? 1 : 0);
589 if (operand) Writer.AddStmt(operand);
590 } else if (TL.hasAttrEnumOperand()) {
591 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
592 }
593}
John McCall17001972009-10-18 01:05:36 +0000594void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getNameLoc(), Record);
596}
John McCallcebee162009-10-18 09:09:24 +0000597void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
598 SubstTemplateTypeParmTypeLoc TL) {
599 Writer.AddSourceLocation(TL.getNameLoc(), Record);
600}
Douglas Gregorada4b792011-01-14 02:55:32 +0000601void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
602 SubstTemplateTypeParmPackTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
604}
John McCall17001972009-10-18 01:05:36 +0000605void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
606 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000607 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000608 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
609 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
610 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
611 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000612 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
613 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000614}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000615void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
617 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
618}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000619void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000620 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000621 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000622}
John McCalle78aac42010-03-10 03:28:59 +0000623void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
624 Writer.AddSourceLocation(TL.getNameLoc(), Record);
625}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000626void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000627 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000628 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000629 Writer.AddSourceLocation(TL.getNameLoc(), Record);
630}
John McCallc392f372010-06-11 00:33:02 +0000631void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
632 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000633 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000634 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000635 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000636 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000637 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
638 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
639 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000640 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
641 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000642}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000643void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
644 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
645}
John McCall17001972009-10-18 01:05:36 +0000646void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
647 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000648}
649void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
650 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000651 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
652 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
653 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
654 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000655}
John McCallfc93cf92009-10-22 22:37:11 +0000656void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
657 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000658}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000659void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
660 Writer.AddSourceLocation(TL.getKWLoc(), Record);
661 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
662 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
663}
John McCall8f115c62009-10-16 21:56:05 +0000664
Richard Smith01b2cb42014-07-26 06:37:51 +0000665void ASTWriter::WriteTypeAbbrevs() {
666 using namespace llvm;
667
668 BitCodeAbbrev *Abv;
669
670 // Abbreviation for TYPE_EXT_QUAL
671 Abv = new BitCodeAbbrev();
672 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
673 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
674 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
675 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
676
677 // Abbreviation for TYPE_FUNCTION_PROTO
678 Abv = new BitCodeAbbrev();
679 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
680 // FunctionType
681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
682 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
683 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
684 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
685 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
686 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
687 // FunctionProtoType
688 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
689 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
690 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
691 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
692 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
694 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
695 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
696 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
697}
698
Chris Lattner19cea4e2009-04-22 05:57:30 +0000699//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000700// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000701//===----------------------------------------------------------------------===//
702
Chris Lattner28fa4e62009-04-26 22:26:21 +0000703static void EmitBlockID(unsigned ID, const char *Name,
704 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000705 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000706 Record.clear();
707 Record.push_back(ID);
708 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
709
710 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000711 if (!Name || Name[0] == 0)
712 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000713 Record.clear();
714 while (*Name)
715 Record.push_back(*Name++);
716 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
717}
718
719static void EmitRecordID(unsigned ID, const char *Name,
720 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000721 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000722 Record.clear();
723 Record.push_back(ID);
724 while (*Name)
725 Record.push_back(*Name++);
726 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000727}
728
729static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000730 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000731#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000732 RECORD(STMT_STOP);
733 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000734 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000735 RECORD(STMT_NULL);
736 RECORD(STMT_COMPOUND);
737 RECORD(STMT_CASE);
738 RECORD(STMT_DEFAULT);
739 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000740 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000741 RECORD(STMT_IF);
742 RECORD(STMT_SWITCH);
743 RECORD(STMT_WHILE);
744 RECORD(STMT_DO);
745 RECORD(STMT_FOR);
746 RECORD(STMT_GOTO);
747 RECORD(STMT_INDIRECT_GOTO);
748 RECORD(STMT_CONTINUE);
749 RECORD(STMT_BREAK);
750 RECORD(STMT_RETURN);
751 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000752 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000753 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000754 RECORD(EXPR_PREDEFINED);
755 RECORD(EXPR_DECL_REF);
756 RECORD(EXPR_INTEGER_LITERAL);
757 RECORD(EXPR_FLOATING_LITERAL);
758 RECORD(EXPR_IMAGINARY_LITERAL);
759 RECORD(EXPR_STRING_LITERAL);
760 RECORD(EXPR_CHARACTER_LITERAL);
761 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000762 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000763 RECORD(EXPR_UNARY_OPERATOR);
764 RECORD(EXPR_SIZEOF_ALIGN_OF);
765 RECORD(EXPR_ARRAY_SUBSCRIPT);
766 RECORD(EXPR_CALL);
767 RECORD(EXPR_MEMBER);
768 RECORD(EXPR_BINARY_OPERATOR);
769 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
770 RECORD(EXPR_CONDITIONAL_OPERATOR);
771 RECORD(EXPR_IMPLICIT_CAST);
772 RECORD(EXPR_CSTYLE_CAST);
773 RECORD(EXPR_COMPOUND_LITERAL);
774 RECORD(EXPR_EXT_VECTOR_ELEMENT);
775 RECORD(EXPR_INIT_LIST);
776 RECORD(EXPR_DESIGNATED_INIT);
777 RECORD(EXPR_IMPLICIT_VALUE_INIT);
778 RECORD(EXPR_VA_ARG);
779 RECORD(EXPR_ADDR_LABEL);
780 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000781 RECORD(EXPR_CHOOSE);
782 RECORD(EXPR_GNU_NULL);
783 RECORD(EXPR_SHUFFLE_VECTOR);
784 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000785 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000786 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000787 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000788 RECORD(EXPR_OBJC_ARRAY_LITERAL);
789 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000790 RECORD(EXPR_OBJC_ENCODE);
791 RECORD(EXPR_OBJC_SELECTOR_EXPR);
792 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
793 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
794 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
795 RECORD(EXPR_OBJC_KVC_REF_EXPR);
796 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000797 RECORD(STMT_OBJC_FOR_COLLECTION);
798 RECORD(STMT_OBJC_CATCH);
799 RECORD(STMT_OBJC_FINALLY);
800 RECORD(STMT_OBJC_AT_TRY);
801 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
802 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000803 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000804 RECORD(STMT_CXX_CATCH);
805 RECORD(STMT_CXX_TRY);
806 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000807 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000808 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000809 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000810 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000811 RECORD(EXPR_CXX_STATIC_CAST);
812 RECORD(EXPR_CXX_DYNAMIC_CAST);
813 RECORD(EXPR_CXX_REINTERPRET_CAST);
814 RECORD(EXPR_CXX_CONST_CAST);
815 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000816 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000817 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000818 RECORD(EXPR_CXX_BOOL_LITERAL);
819 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000820 RECORD(EXPR_CXX_TYPEID_EXPR);
821 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000822 RECORD(EXPR_CXX_THIS);
823 RECORD(EXPR_CXX_THROW);
824 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000825 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000826 RECORD(EXPR_CXX_BIND_TEMPORARY);
827 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
828 RECORD(EXPR_CXX_NEW);
829 RECORD(EXPR_CXX_DELETE);
830 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
831 RECORD(EXPR_EXPR_WITH_CLEANUPS);
832 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
833 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
834 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
835 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
836 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000837 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000838 RECORD(EXPR_CXX_NOEXCEPT);
839 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000840 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
841 RECORD(EXPR_TYPE_TRAIT);
842 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000843 RECORD(EXPR_PACK_EXPANSION);
844 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000845 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000846 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000847 RECORD(EXPR_FUNCTION_PARM_PACK);
848 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000849 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000850 RECORD(EXPR_CXX_UUIDOF_EXPR);
851 RECORD(EXPR_CXX_UUIDOF_TYPE);
852 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000853#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000854}
Mike Stump11289f42009-09-09 15:08:12 +0000855
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000856void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000857 RecordData Record;
858 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000859
Sebastian Redl539c5062010-08-18 23:57:32 +0000860#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
861#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000862
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000863 // Control Block.
864 BLOCK(CONTROL_BLOCK);
865 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000866 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000867 RECORD(MODULE_NAME);
868 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000869 RECORD(IMPORTS);
870 RECORD(LANGUAGE_OPTIONS);
871 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000872 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000873 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000874 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000875 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000876 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000877 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000878 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000879 RECORD(PREPROCESSOR_OPTIONS);
880
Douglas Gregor108cb222012-10-19 00:45:00 +0000881 BLOCK(INPUT_FILES_BLOCK);
882 RECORD(INPUT_FILE);
883
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000884 // AST Top-Level Block.
885 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000886 RECORD(TYPE_OFFSET);
887 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000888 RECORD(IDENTIFIER_OFFSET);
889 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000890 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000891 RECORD(SPECIAL_TYPES);
892 RECORD(STATISTICS);
893 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000894 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000895 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000896 RECORD(SELECTOR_OFFSETS);
897 RECORD(METHOD_POOL);
898 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000899 RECORD(SOURCE_LOCATION_OFFSETS);
900 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000901 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000902 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000903 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000904 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000905 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000906 RECORD(SEMA_DECL_REFS);
907 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
908 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
909 RECORD(DECL_REPLACEMENTS);
910 RECORD(UPDATE_VISIBLE);
911 RECORD(DECL_UPDATE_OFFSETS);
912 RECORD(DECL_UPDATES);
913 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
914 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000915 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000916 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000917 RECORD(FP_PRAGMA_OPTIONS);
918 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000919 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000920 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000921 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000922 RECORD(MODULE_OFFSET_MAP);
923 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000924 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000925 RECORD(FILE_SORTED_DECLS);
926 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000927 RECORD(MERGED_DECLARATIONS);
928 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000929 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000930 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000931 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000932 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000933 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Douglas Gregor358cd442012-01-15 16:58:34 +0000934
Chris Lattner28fa4e62009-04-26 22:26:21 +0000935 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000936 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000937 RECORD(SM_SLOC_FILE_ENTRY);
938 RECORD(SM_SLOC_BUFFER_ENTRY);
939 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000940 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000941
Chris Lattner28fa4e62009-04-26 22:26:21 +0000942 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000943 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000944 RECORD(PP_MACRO_OBJECT_LIKE);
945 RECORD(PP_MACRO_FUNCTION_LIKE);
946 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000947
Douglas Gregor12bfa382009-10-17 00:13:19 +0000948 // Decls and Types block.
949 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000950 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000951 RECORD(TYPE_COMPLEX);
952 RECORD(TYPE_POINTER);
953 RECORD(TYPE_BLOCK_POINTER);
954 RECORD(TYPE_LVALUE_REFERENCE);
955 RECORD(TYPE_RVALUE_REFERENCE);
956 RECORD(TYPE_MEMBER_POINTER);
957 RECORD(TYPE_CONSTANT_ARRAY);
958 RECORD(TYPE_INCOMPLETE_ARRAY);
959 RECORD(TYPE_VARIABLE_ARRAY);
960 RECORD(TYPE_VECTOR);
961 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000962 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000963 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000964 RECORD(TYPE_TYPEDEF);
965 RECORD(TYPE_TYPEOF_EXPR);
966 RECORD(TYPE_TYPEOF);
967 RECORD(TYPE_RECORD);
968 RECORD(TYPE_ENUM);
969 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000970 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000971 RECORD(TYPE_DECLTYPE);
972 RECORD(TYPE_ELABORATED);
973 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
974 RECORD(TYPE_UNRESOLVED_USING);
975 RECORD(TYPE_INJECTED_CLASS_NAME);
976 RECORD(TYPE_OBJC_OBJECT);
977 RECORD(TYPE_TEMPLATE_TYPE_PARM);
978 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
979 RECORD(TYPE_DEPENDENT_NAME);
980 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
981 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
982 RECORD(TYPE_PAREN);
983 RECORD(TYPE_PACK_EXPANSION);
984 RECORD(TYPE_ATTRIBUTED);
985 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000986 RECORD(TYPE_AUTO);
987 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000988 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000989 RECORD(TYPE_DECAYED);
990 RECORD(TYPE_ADJUSTED);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000991 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +0000992 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000993 RECORD(DECL_ENUM);
994 RECORD(DECL_RECORD);
995 RECORD(DECL_ENUM_CONSTANT);
996 RECORD(DECL_FUNCTION);
997 RECORD(DECL_OBJC_METHOD);
998 RECORD(DECL_OBJC_INTERFACE);
999 RECORD(DECL_OBJC_PROTOCOL);
1000 RECORD(DECL_OBJC_IVAR);
1001 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001002 RECORD(DECL_OBJC_CATEGORY);
1003 RECORD(DECL_OBJC_CATEGORY_IMPL);
1004 RECORD(DECL_OBJC_IMPLEMENTATION);
1005 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1006 RECORD(DECL_OBJC_PROPERTY);
1007 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001008 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001009 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001010 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001011 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001012 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001013 RECORD(DECL_FILE_SCOPE_ASM);
1014 RECORD(DECL_BLOCK);
1015 RECORD(DECL_CONTEXT_LEXICAL);
1016 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001017 RECORD(DECL_NAMESPACE);
1018 RECORD(DECL_NAMESPACE_ALIAS);
1019 RECORD(DECL_USING);
1020 RECORD(DECL_USING_SHADOW);
1021 RECORD(DECL_USING_DIRECTIVE);
1022 RECORD(DECL_UNRESOLVED_USING_VALUE);
1023 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1024 RECORD(DECL_LINKAGE_SPEC);
1025 RECORD(DECL_CXX_RECORD);
1026 RECORD(DECL_CXX_METHOD);
1027 RECORD(DECL_CXX_CONSTRUCTOR);
1028 RECORD(DECL_CXX_DESTRUCTOR);
1029 RECORD(DECL_CXX_CONVERSION);
1030 RECORD(DECL_ACCESS_SPEC);
1031 RECORD(DECL_FRIEND);
1032 RECORD(DECL_FRIEND_TEMPLATE);
1033 RECORD(DECL_CLASS_TEMPLATE);
1034 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1035 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001036 RECORD(DECL_VAR_TEMPLATE);
1037 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1038 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001039 RECORD(DECL_FUNCTION_TEMPLATE);
1040 RECORD(DECL_TEMPLATE_TYPE_PARM);
1041 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1042 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1043 RECORD(DECL_STATIC_ASSERT);
1044 RECORD(DECL_CXX_BASE_SPECIFIERS);
1045 RECORD(DECL_INDIRECTFIELD);
1046 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1047
Douglas Gregor03412ba2011-06-03 02:27:19 +00001048 // Statements and Exprs can occur in the Decls and Types block.
1049 AddStmtsExprs(Stream, Record);
1050
Douglas Gregor92a96f52011-02-08 21:58:10 +00001051 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001052 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001053 RECORD(PPD_MACRO_DEFINITION);
1054 RECORD(PPD_INCLUSION_DIRECTIVE);
1055
Chris Lattner28fa4e62009-04-26 22:26:21 +00001056#undef RECORD
1057#undef BLOCK
1058 Stream.ExitBlock();
1059}
1060
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001061/// \brief Adjusts the given filename to only write out the portion of the
1062/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001063///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001064/// \param Filename the file name to adjust.
1065///
1066/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
1067/// the returned filename will be adjusted by this system root.
1068///
1069/// \returns either the original filename (if it needs no adjustment) or the
1070/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001071static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +00001072adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001073 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001074
Douglas Gregorc567ba22011-07-22 16:35:34 +00001075 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001076 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001077
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001078 // Verify that the filename and the system root have the same prefix.
1079 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001080 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001081 if (Filename[Pos] != isysroot[Pos])
1082 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001083
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001084 // We hit the end of the filename before we hit the end of the system root.
1085 if (!Filename[Pos])
1086 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001087
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001088 // If the file name has a '/' at the current position, skip over the '/'.
1089 // We distinguish sysroot-based includes from absolute includes by the
1090 // absence of '/' at the beginning of sysroot-based includes.
1091 if (Filename[Pos] == '/')
1092 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001093
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001094 return Filename + Pos;
1095}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001096
Ben Langmuir487ea142014-10-23 18:05:36 +00001097static ASTFileSignature getSignature() {
1098 while (1) {
1099 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1100 return S;
1101 // Rely on GetRandomNumber to eventually return non-zero...
1102 }
1103}
1104
Douglas Gregor112b9072012-10-18 05:31:06 +00001105/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001106void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1107 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001108 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001109 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001110 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1111 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001112
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001113 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001114 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1115 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1116 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1117 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1118 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1119 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1120 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1121 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1122 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1123 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1124 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001125 Record.push_back(VERSION_MAJOR);
1126 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001127 Record.push_back(CLANG_VERSION_MAJOR);
1128 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001129 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001130 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001131 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1132 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001133
Ben Langmuir487ea142014-10-23 18:05:36 +00001134 // Signature
1135 Record.clear();
1136 Record.push_back(getSignature());
1137 Stream.EmitRecord(SIGNATURE, Record);
1138
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001139 // Module name
1140 if (WritingModule) {
1141 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1142 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1144 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1145 RecordData Record;
1146 Record.push_back(MODULE_NAME);
1147 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1148 }
1149
1150 // Module map file
1151 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001152 Record.clear();
1153 auto addModMap = [&](const FileEntry *F) {
1154 SmallString<128> ModuleMap(F->getName());
1155 llvm::sys::fs::make_absolute(ModuleMap);
1156 AddString(ModuleMap.str(), Record);
1157 };
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001158
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001159 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1160
1161 // Primary module map file.
1162 addModMap(Map.getModuleMapFileForUniquing(WritingModule));
1163
1164 // Additional module map files.
1165 if (auto *AdditionalModMaps = Map.getAdditionalModuleMapFiles(WritingModule)) {
1166 Record.push_back(AdditionalModMaps->size());
1167 for (const FileEntry *F : *AdditionalModMaps)
1168 addModMap(F);
1169 } else {
1170 Record.push_back(0);
1171 }
1172
1173 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001174 }
1175
Douglas Gregor112b9072012-10-18 05:31:06 +00001176 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001177 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001178 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001179 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001180
1181 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1182 M != MEnd; ++M) {
1183 // Skip modules that weren't directly imported.
1184 if (!(*M)->isDirectlyImported())
1185 continue;
1186
1187 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001188 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001189 Record.push_back((*M)->File->getSize());
1190 Record.push_back((*M)->File->getModificationTime());
Ben Langmuir487ea142014-10-23 18:05:36 +00001191 Record.push_back((*M)->Signature);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001192 const std::string &FileName = (*M)->FileName;
1193 Record.push_back(FileName.size());
1194 Record.append(FileName.begin(), FileName.end());
1195 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001196 Stream.EmitRecord(IMPORTS, Record);
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Douglas Gregor112b9072012-10-18 05:31:06 +00001199 // Language options.
1200 Record.clear();
1201 const LangOptions &LangOpts = Context.getLangOpts();
1202#define LANGOPT(Name, Bits, Default, Description) \
1203 Record.push_back(LangOpts.Name);
1204#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1205 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1206#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001207#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1208#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001209
1210 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1211 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1212
1213 Record.push_back(LangOpts.CurrentModule.size());
1214 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001215
1216 // Comment options.
1217 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1218 for (CommentOptions::BlockCommandNamesTy::const_iterator
1219 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1220 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1221 I != IEnd; ++I) {
1222 AddString(*I, Record);
1223 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001224 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001225
Douglas Gregor112b9072012-10-18 05:31:06 +00001226 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1227
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001228 // Target options.
1229 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001230 const TargetInfo &Target = Context.getTargetInfo();
1231 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001232 AddString(TargetOpts.Triple, Record);
1233 AddString(TargetOpts.CPU, Record);
1234 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001235 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1236 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1237 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1238 }
1239 Record.push_back(TargetOpts.Features.size());
1240 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1241 AddString(TargetOpts.Features[I], Record);
1242 }
1243 Stream.EmitRecord(TARGET_OPTIONS, Record);
1244
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001245 // Diagnostic options.
1246 Record.clear();
1247 const DiagnosticOptions &DiagOpts
1248 = Context.getDiagnostics().getDiagnosticOptions();
1249#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1250#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1251 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1252#include "clang/Basic/DiagnosticOptions.def"
1253 Record.push_back(DiagOpts.Warnings.size());
1254 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1255 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001256 Record.push_back(DiagOpts.Remarks.size());
1257 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1258 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001259 // Note: we don't serialize the log or serialization file names, because they
1260 // are generally transient files and will almost always be overridden.
1261 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1262
Douglas Gregorc6317db2012-10-24 15:49:58 +00001263 // File system options.
1264 Record.clear();
1265 const FileSystemOptions &FSOpts
1266 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1267 AddString(FSOpts.WorkingDir, Record);
1268 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1269
Douglas Gregor2d302362012-10-24 16:50:34 +00001270 // Header search options.
1271 Record.clear();
1272 const HeaderSearchOptions &HSOpts
1273 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1274 AddString(HSOpts.Sysroot, Record);
1275
1276 // Include entries.
1277 Record.push_back(HSOpts.UserEntries.size());
1278 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1279 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1280 AddString(Entry.Path, Record);
1281 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001282 Record.push_back(Entry.IsFramework);
1283 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001284 }
1285
1286 // System header prefixes.
1287 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1288 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1289 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1290 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1291 }
1292
1293 AddString(HSOpts.ResourceDir, Record);
1294 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001295 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001296 Record.push_back(HSOpts.DisableModuleHash);
1297 Record.push_back(HSOpts.UseBuiltinIncludes);
1298 Record.push_back(HSOpts.UseStandardSystemIncludes);
1299 Record.push_back(HSOpts.UseStandardCXXIncludes);
1300 Record.push_back(HSOpts.UseLibcxx);
1301 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1302
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001303 // Preprocessor options.
1304 Record.clear();
1305 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1306
1307 // Macro definitions.
1308 Record.push_back(PPOpts.Macros.size());
1309 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1310 AddString(PPOpts.Macros[I].first, Record);
1311 Record.push_back(PPOpts.Macros[I].second);
1312 }
1313
1314 // Includes
1315 Record.push_back(PPOpts.Includes.size());
1316 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1317 AddString(PPOpts.Includes[I], Record);
1318
1319 // Macro includes
1320 Record.push_back(PPOpts.MacroIncludes.size());
1321 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1322 AddString(PPOpts.MacroIncludes[I], Record);
1323
Douglas Gregorb6368752012-10-24 23:41:50 +00001324 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001325 // Detailed record is important since it is used for the module cache hash.
1326 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001327 AddString(PPOpts.ImplicitPCHInclude, Record);
1328 AddString(PPOpts.ImplicitPTHInclude, Record);
1329 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1330 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1331
Douglas Gregora3b20262011-05-06 21:43:30 +00001332 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001333 SourceManager &SM = Context.getSourceManager();
1334 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1335 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001336 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1337 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001338 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1339 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1340
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001341 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001342
Michael J. Spencer740857f2010-12-21 16:45:57 +00001343 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001344
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001345 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001346 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001347 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001348 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001349 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001350 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001351 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001352 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001353
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001354 Record.clear();
1355 Record.push_back(SM.getMainFileID().getOpaqueValue());
1356 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1357
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001358 // Original PCH directory
1359 if (!OutputFile.empty() && OutputFile != "-") {
1360 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1361 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1362 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1363 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1364
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001365 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001366
1367 llvm::sys::fs::make_absolute(OutputPath);
1368 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1369
1370 RecordData Record;
1371 Record.push_back(ORIGINAL_PCH_DIR);
1372 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1373 }
1374
Douglas Gregor49491f72013-03-15 22:15:07 +00001375 WriteInputFiles(Context.SourceMgr,
1376 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001377 isysroot,
1378 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001379 Stream.ExitBlock();
1380}
1381
Douglas Gregor49491f72013-03-15 22:15:07 +00001382namespace {
1383 /// \brief An input file.
1384 struct InputFileEntry {
1385 const FileEntry *File;
1386 bool IsSystemFile;
1387 bool BufferOverridden;
1388 };
1389}
1390
1391void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1392 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001393 StringRef isysroot,
1394 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001395 using namespace llvm;
1396 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1397 RecordData Record;
1398
1399 // Create input-file abbreviation.
1400 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1401 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001402 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001403 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1404 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001405 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001406 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1407 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1408
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001409 // Get all ContentCache objects for files, sorted by whether the file is a
1410 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001411 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001412 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1413 // Get this source location entry.
1414 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001415 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001416
1417 // We only care about file entries that were not overridden.
1418 if (!SLoc->isFile())
1419 continue;
1420 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001421 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001422 continue;
1423
Douglas Gregor49491f72013-03-15 22:15:07 +00001424 InputFileEntry Entry;
1425 Entry.File = Cache->OrigEntry;
1426 Entry.IsSystemFile = Cache->IsSystemFile;
1427 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001428 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001429 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001430 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001431 SortedFiles.push_front(Entry);
1432 }
1433
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001434 unsigned UserFilesNum = 0;
1435 // Write out all of the input files.
1436 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001437 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001438 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001439 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001440
Douglas Gregor49491f72013-03-15 22:15:07 +00001441 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001442 if (InputFileID != 0)
1443 continue; // already recorded this file.
1444
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001445 // Record this entry's offset.
1446 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001447
1448 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001449
Douglas Gregor49491f72013-03-15 22:15:07 +00001450 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001451 ++UserFilesNum;
1452
Douglas Gregor72be3902012-10-19 00:38:02 +00001453 Record.clear();
1454 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001455 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001456
1457 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001458 Record.push_back(Entry.File->getSize());
1459 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001460
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001461 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001462 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001463
Douglas Gregor72be3902012-10-19 00:38:02 +00001464 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001465 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001466 SmallString<128> FilePath(Filename);
1467
1468 // Ask the file manager to fixup the relative path for us. This will
1469 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001470 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001471
1472 // FIXME: This call to make_absolute shouldn't be necessary, the
1473 // call to FixupRelativePath should always return an absolute path.
1474 llvm::sys::fs::make_absolute(FilePath);
1475 Filename = FilePath.c_str();
1476
1477 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1478
1479 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1480 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001481
Douglas Gregor112b9072012-10-18 05:31:06 +00001482 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001483
1484 // Create input file offsets abbreviation.
1485 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1486 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1487 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001488 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1489 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001490 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1491 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1492
1493 // Write input file offsets.
1494 Record.clear();
1495 Record.push_back(INPUT_FILE_OFFSETS);
1496 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001497 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001498 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001499}
1500
Douglas Gregora7f71a92009-04-10 03:52:48 +00001501//===----------------------------------------------------------------------===//
1502// Source Manager Serialization
1503//===----------------------------------------------------------------------===//
1504
1505/// \brief Create an abbreviation for the SLocEntry that refers to a
1506/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001507static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001508 using namespace llvm;
1509 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001510 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001515 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001516 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001517 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001518 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001520 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001521}
1522
1523/// \brief Create an abbreviation for the SLocEntry that refers to a
1524/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001525static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001526 using namespace llvm;
1527 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001528 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001534 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001535}
1536
1537/// \brief Create an abbreviation for the SLocEntry that refers to a
1538/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001539static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001540 using namespace llvm;
1541 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001542 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001544 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001545}
1546
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001547/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1548/// expansion.
1549static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001550 using namespace llvm;
1551 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001552 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001558 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001559}
1560
Douglas Gregor09b69892011-02-10 17:09:37 +00001561namespace {
1562 // Trait used for the on-disk hash table of header search information.
1563 class HeaderFileInfoTrait {
1564 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001565 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001566
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001567 // Keep track of the framework names we've used during serialization.
1568 SmallVector<char, 128> FrameworkStringData;
1569 llvm::StringMap<unsigned> FrameworkNameOffset;
1570
Douglas Gregor09b69892011-02-10 17:09:37 +00001571 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001572 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1573 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001574
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001575 struct key_type {
1576 const FileEntry *FE;
1577 const char *Filename;
1578 };
1579 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001580
1581 typedef HeaderFileInfo data_type;
1582 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001583 typedef unsigned hash_value_type;
1584 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001585
Justin Bogner25463f12014-04-18 20:27:24 +00001586 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001587 // The hash is based only on size/time of the file, so that the reader can
1588 // match even when symlinking or excess path elements ("foo/../", "../")
1589 // change the form of the name. However, complete path is still the key.
1590 return llvm::hash_combine(key.FE->getSize(),
1591 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001592 }
1593
1594 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001595 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001596 using namespace llvm::support;
1597 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001598 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001599 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001600 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001601 if (Data.isModuleHeader)
1602 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001603 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001604 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001605 }
1606
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001607 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001608 using namespace llvm::support;
1609 endian::Writer<little> LE(Out);
1610 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001611 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001612 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001613 KeyLen -= 8;
1614 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001615 }
1616
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001617 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001618 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001619 using namespace llvm::support;
1620 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001621 uint64_t Start = Out.tell(); (void)Start;
1622
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001623 unsigned char Flags = (Data.HeaderRole << 6)
1624 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001625 | (Data.isPragmaOnce << 4)
1626 | (Data.DirInfo << 2)
1627 | (Data.Resolved << 1)
1628 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001629 LE.write<uint8_t>(Flags);
1630 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001631
1632 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001633 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001634 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001635 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001636
1637 unsigned Offset = 0;
1638 if (!Data.Framework.empty()) {
1639 // If this header refers into a framework, save the framework name.
1640 llvm::StringMap<unsigned>::iterator Pos
1641 = FrameworkNameOffset.find(Data.Framework);
1642 if (Pos == FrameworkNameOffset.end()) {
1643 Offset = FrameworkStringData.size() + 1;
1644 FrameworkStringData.append(Data.Framework.begin(),
1645 Data.Framework.end());
1646 FrameworkStringData.push_back(0);
1647
1648 FrameworkNameOffset[Data.Framework] = Offset;
1649 } else
1650 Offset = Pos->second;
1651 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001652 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001653
1654 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001655 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001656 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001657 }
1658
Douglas Gregor09b69892011-02-10 17:09:37 +00001659 assert(Out.tell() - Start == DataLen && "Wrong data length");
1660 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001661
1662 const char *strings_begin() const { return FrameworkStringData.begin(); }
1663 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001664 };
1665} // end anonymous namespace
1666
1667/// \brief Write the header search block for the list of files that
1668///
1669/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001670void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001671 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001672 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1673
1674 if (FilesByUID.size() > HS.header_file_size())
1675 FilesByUID.resize(HS.header_file_size());
1676
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001677 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001678 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001679 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001680 unsigned NumHeaderSearchEntries = 0;
1681 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1682 const FileEntry *File = FilesByUID[UID];
1683 if (!File)
1684 continue;
1685
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001686 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1687 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001688 HeaderFileInfo HFI;
1689 if (!HS.tryGetFileInfo(File, HFI) ||
1690 (HFI.External && Chain) ||
1691 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001692 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001693
1694 // Turn the file name into an absolute path, if it isn't already.
1695 const char *Filename = File->getName();
1696 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1697
1698 // If we performed any translation on the file name at all, we need to
1699 // save this string, since the generator will refer to it later.
1700 if (Filename != File->getName()) {
1701 Filename = strdup(Filename);
1702 SavedStrings.push_back(Filename);
1703 }
1704
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001705 HeaderFileInfoTrait::key_type key = { File, Filename };
1706 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001707 ++NumHeaderSearchEntries;
1708 }
1709
1710 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001711 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001712 uint32_t BucketOffset;
1713 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001714 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001715 llvm::raw_svector_ostream Out(TableData);
1716 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001717 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001718 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1719 }
1720
1721 // Create a blob abbreviation
1722 using namespace llvm;
1723 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1724 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1729 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1730
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001731 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001732 RecordData Record;
1733 Record.push_back(HEADER_SEARCH_TABLE);
1734 Record.push_back(BucketOffset);
1735 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001736 Record.push_back(TableData.size());
1737 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001738 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1739
1740 // Free all of the strings we had to duplicate.
1741 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001742 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001743}
1744
Douglas Gregora7f71a92009-04-10 03:52:48 +00001745/// \brief Writes the block containing the serialized form of the
1746/// source manager.
1747///
1748/// TODO: We should probably use an on-disk hash table (stored in a
1749/// blob), indexed based on the file name, so that we only create
1750/// entries for files that we actually need. In the common case (no
1751/// errors), we probably won't have to create file entries for any of
1752/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001753void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001754 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001755 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001756 RecordData Record;
1757
Chris Lattner0910e3b2009-04-10 17:16:57 +00001758 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001759 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001760
1761 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001762 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1763 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1764 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001765 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001766
Douglas Gregor258ae542009-04-27 06:38:32 +00001767 // Write out the source location entry table. We skip the first
1768 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001769 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001770 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001771 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1772 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001773 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001774 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001775 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001776 FileID FID = FileID::get(I);
1777 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001778
Douglas Gregor258ae542009-04-27 06:38:32 +00001779 // Record the offset of this source-location entry.
1780 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1781
1782 // Figure out which record code to use.
1783 unsigned Code;
1784 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001785 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1786 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001787 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001788 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001789 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001790 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001791 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001792 Record.clear();
1793 Record.push_back(Code);
1794
Douglas Gregor925296b2011-07-19 16:10:42 +00001795 // Starting offset of this entry within this module, so skip the dummy.
1796 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001797 if (SLoc->isFile()) {
1798 const SrcMgr::FileInfo &File = SLoc->getFile();
1799 Record.push_back(File.getIncludeLoc().getRawEncoding());
1800 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1801 Record.push_back(File.hasLineDirectives());
1802
1803 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001804 if (Content->OrigEntry) {
1805 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001806 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001807
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001808 // The source location entry is a file. Emit input file ID.
1809 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1810 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001811
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001812 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001813
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001814 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001815 if (FDI != FileDeclIDs.end()) {
1816 Record.push_back(FDI->second->FirstDeclIndex);
1817 Record.push_back(FDI->second->DeclIDs.size());
1818 } else {
1819 Record.push_back(0);
1820 Record.push_back(0);
1821 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001822
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001823 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001824
1825 if (Content->BufferOverridden) {
1826 Record.clear();
1827 Record.push_back(SM_SLOC_BUFFER_BLOB);
1828 const llvm::MemoryBuffer *Buffer
1829 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1830 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1831 StringRef(Buffer->getBufferStart(),
1832 Buffer->getBufferSize() + 1));
1833 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001834 } else {
1835 // The source location entry is a buffer. The blob associated
1836 // with this entry contains the contents of the buffer.
1837
1838 // We add one to the size so that we capture the trailing NULL
1839 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1840 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001841 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001842 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001843 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001844 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001845 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001846 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001847 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001848 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001849 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001850 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001851
Douglas Gregor925296b2011-07-19 16:10:42 +00001852 if (strcmp(Name, "<built-in>") == 0) {
1853 PreloadSLocs.push_back(SLocEntryOffsets.size());
1854 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001855 }
1856 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001857 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001858 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001859 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1860 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001861 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1862 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001863
1864 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001865 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001866 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001867 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001868 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001869 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001870 }
1871 }
1872
Douglas Gregor8f45df52009-04-16 22:23:12 +00001873 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001874
1875 if (SLocEntryOffsets.empty())
1876 return;
1877
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001878 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001879 // table is used for lazily loading source-location information.
1880 using namespace llvm;
1881 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001882 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1886 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001887
Douglas Gregor258ae542009-04-27 06:38:32 +00001888 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001889 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001890 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001891 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001892 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001893
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001894 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001895 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001896 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001897
1898 // Write the line table. It depends on remapping working, so it must come
1899 // after the source location offsets.
1900 if (SourceMgr.hasLineTable()) {
1901 LineTableInfo &LineTable = SourceMgr.getLineTable();
1902
1903 Record.clear();
1904 // Emit the file names
1905 Record.push_back(LineTable.getNumFilenames());
1906 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1907 // Emit the file name
1908 const char *Filename = LineTable.getFilename(I);
1909 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1910 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1911 Record.push_back(FilenameLen);
1912 if (FilenameLen)
1913 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1914 }
1915
1916 // Emit the line entries
1917 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1918 L != LEnd; ++L) {
1919 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001920 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001921 continue;
1922
1923 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001924 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001925
1926 // Emit the line entries
1927 Record.push_back(L->second.size());
1928 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1929 LEEnd = L->second.end();
1930 LE != LEEnd; ++LE) {
1931 Record.push_back(LE->FileOffset);
1932 Record.push_back(LE->LineNo);
1933 Record.push_back(LE->FilenameID);
1934 Record.push_back((unsigned)LE->FileKind);
1935 Record.push_back(LE->IncludeOffset);
1936 }
1937 }
1938 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1939 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001940}
1941
Douglas Gregorc5046832009-04-27 18:38:38 +00001942//===----------------------------------------------------------------------===//
1943// Preprocessor Serialization
1944//===----------------------------------------------------------------------===//
1945
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001946namespace {
1947class ASTMacroTableTrait {
1948public:
1949 typedef IdentID key_type;
1950 typedef key_type key_type_ref;
1951
1952 struct Data {
1953 uint32_t MacroDirectivesOffset;
1954 };
1955
1956 typedef Data data_type;
1957 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001958 typedef unsigned hash_value_type;
1959 typedef unsigned offset_type;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001960
Justin Bogner25463f12014-04-18 20:27:24 +00001961 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001962 return llvm::hash_value(IdID);
1963 }
1964
1965 std::pair<unsigned,unsigned>
1966 static EmitKeyDataLength(raw_ostream& Out,
1967 key_type_ref Key, data_type_ref Data) {
1968 unsigned KeyLen = 4; // IdentID.
1969 unsigned DataLen = 4; // MacroDirectivesOffset.
1970 return std::make_pair(KeyLen, DataLen);
1971 }
1972
1973 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001974 using namespace llvm::support;
1975 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001976 }
1977
1978 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1979 unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001980 using namespace llvm::support;
1981 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001982 }
1983};
1984} // end anonymous namespace
1985
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001986static int compareMacroDirectives(
1987 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1988 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1989 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001990}
1991
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001992static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1993 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001994 if (MacroInfo *MI = MD->getMacroInfo())
1995 if (MI->isBuiltinMacro())
1996 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001997
1998 if (IsModule) {
Richard Smithe657bbd2014-07-18 22:13:40 +00001999 // Re-export any imported directives.
Richard Smithdaa69e02014-07-25 04:40:03 +00002000 if (MD->isImported())
2001 return false;
Richard Smithe657bbd2014-07-18 22:13:40 +00002002
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002003 SourceLocation Loc = MD->getLocation();
2004 if (Loc.isInvalid())
2005 return true;
2006 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2007 return true;
2008 }
2009
2010 return false;
2011}
2012
Chris Lattnereeffaef2009-04-10 17:15:23 +00002013/// \brief Writes the block containing the serialized form of the
2014/// preprocessor.
2015///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002016void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002017 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2018 if (PPRec)
2019 WritePreprocessorDetail(*PPRec);
2020
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002021 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002022
Chris Lattner0af3ba12009-04-13 01:29:17 +00002023 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2024 if (PP.getCounterValue() != 0) {
2025 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002026 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002027 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002028 }
2029
2030 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002031 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002032
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002033 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002034 // FIXME: use diagnostics subsystem for localization etc.
2035 if (PP.SawDateOrTime())
2036 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00002037
Douglas Gregor796d76a2010-10-20 22:00:55 +00002038
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002039 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002040 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002041
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002042 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002043 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002044 MacroDirectives;
2045 for (Preprocessor::macro_iterator
2046 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2047 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002048 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002049 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002050 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002051
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002052 // Sort the set of macro definitions that need to be serialized by the
2053 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002054 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2055 &compareMacroDirectives);
2056
Justin Bognerbb094f02014-04-18 19:57:06 +00002057 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002058
2059 // Emit the macro directives as a list and associate the offset with the
2060 // identifier they belong to.
2061 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2062 const IdentifierInfo *Name = MacroDirectives[I].first;
2063 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2064 MacroDirective *MD = MacroDirectives[I].second;
2065
2066 // If the macro or identifier need no updates, don't write the macro history
2067 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002068 // FIXME: Chain the macro history instead of re-writing it.
2069 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002070 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2071 continue;
2072
2073 // Emit the macro directives in reverse source order.
2074 for (; MD; MD = MD->getPrevious()) {
2075 if (shouldIgnoreMacro(MD, IsModule, PP))
2076 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002077
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002078 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002079 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002080 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002081 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2082 Record.push_back(InfoID);
Richard Smithdaa69e02014-07-25 04:40:03 +00002083 Record.push_back(DefMD->getOwningModuleID());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002084 Record.push_back(DefMD->isAmbiguous());
Richard Smithdaa69e02014-07-25 04:40:03 +00002085 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2086 Record.push_back(UndefMD->getOwningModuleID());
2087 } else {
2088 auto *VisMD = cast<VisibilityMacroDirective>(MD);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002089 Record.push_back(VisMD->isPublic());
2090 }
Richard Smithdaa69e02014-07-25 04:40:03 +00002091
2092 if (MD->isImported()) {
2093 auto Overrides = MD->getOverriddenModules();
2094 Record.push_back(Overrides.size());
2095 for (auto Override : Overrides)
2096 Record.push_back(Override);
2097 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002098 }
2099 if (Record.empty())
2100 continue;
2101
2102 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2103 Record.clear();
2104
2105 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2106
2107 IdentID NameID = getIdentifierRef(Name);
2108 ASTMacroTableTrait::Data data;
2109 data.MacroDirectivesOffset = MacroDirectiveOffset;
2110 Generator.insert(NameID, data);
2111 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002112
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002113 /// \brief Offsets of each of the macros into the bitstream, indexed by
2114 /// the local macro ID
2115 ///
2116 /// For each identifier that is associated with a macro, this map
2117 /// provides the offset into the bitstream where that macro is
2118 /// defined.
2119 std::vector<uint32_t> MacroOffsets;
2120
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002121 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2122 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2123 MacroInfo *MI = MacroInfosToEmit[I].MI;
2124 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002125
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002126 if (ID < FirstMacroID) {
2127 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2128 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002129 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002130
2131 // Record the local offset of this macro.
2132 unsigned Index = ID - FirstMacroID;
2133 if (Index == MacroOffsets.size())
2134 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2135 else {
2136 if (Index > MacroOffsets.size())
2137 MacroOffsets.resize(Index + 1);
2138
2139 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2140 }
2141
2142 AddIdentifierRef(Name, Record);
2143 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2144 AddSourceLocation(MI->getDefinitionLoc(), Record);
2145 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2146 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002147 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002148 unsigned Code;
2149 if (MI->isObjectLike()) {
2150 Code = PP_MACRO_OBJECT_LIKE;
2151 } else {
2152 Code = PP_MACRO_FUNCTION_LIKE;
2153
2154 Record.push_back(MI->isC99Varargs());
2155 Record.push_back(MI->isGNUVarargs());
2156 Record.push_back(MI->hasCommaPasting());
2157 Record.push_back(MI->getNumArgs());
2158 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2159 I != E; ++I)
2160 AddIdentifierRef(*I, Record);
2161 }
2162
2163 // If we have a detailed preprocessing record, record the macro definition
2164 // ID that corresponds to this macro.
2165 if (PPRec)
2166 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2167
2168 Stream.EmitRecord(Code, Record);
2169 Record.clear();
2170
2171 // Emit the tokens array.
2172 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2173 // Note that we know that the preprocessor does not have any annotation
2174 // tokens in it because they are created by the parser, and thus can't
2175 // be in a macro definition.
2176 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002177 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002178 Stream.EmitRecord(PP_TOKEN, Record);
2179 Record.clear();
2180 }
2181 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002182 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002183
Douglas Gregor92a96f52011-02-08 21:58:10 +00002184 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002185
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002186 // Create the on-disk hash table in a buffer.
2187 SmallString<4096> MacroTable;
2188 uint32_t BucketOffset;
2189 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002190 using namespace llvm::support;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002191 llvm::raw_svector_ostream Out(MacroTable);
2192 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002193 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002194 BucketOffset = Generator.Emit(Out);
2195 }
2196
2197 // Write the macro table
2198 using namespace llvm;
2199 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2200 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2203 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2204
2205 Record.push_back(MACRO_TABLE);
2206 Record.push_back(BucketOffset);
2207 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2208 Record.clear();
2209
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002210 // Write the offsets table for macro IDs.
2211 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002212 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);
2219 Record.clear();
2220 Record.push_back(MACRO_OFFSET);
2221 Record.push_back(MacroOffsets.size());
2222 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2223 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2224 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002225}
2226
2227void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002228 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002229 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002230
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002231 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002232
Douglas Gregor92a96f52011-02-08 21:58:10 +00002233 // Enter the preprocessor block.
2234 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002235
Douglas Gregoraae92242010-03-19 21:51:54 +00002236 // If the preprocessor has a preprocessing record, emit it.
2237 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002238 using namespace llvm;
2239
2240 // Set up the abbreviation for
2241 unsigned InclusionAbbrev = 0;
2242 {
2243 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2244 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2250 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2251 }
2252
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002253 unsigned FirstPreprocessorEntityID
2254 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2255 + NUM_PREDEF_PP_ENTITY_IDS;
2256 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002257 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002258 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2259 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002260 E != EEnd;
2261 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002262 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002263
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002264 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2265 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002266
Douglas Gregor92a96f52011-02-08 21:58:10 +00002267 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002268 // Record this macro definition's ID.
2269 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002270
Douglas Gregor92a96f52011-02-08 21:58:10 +00002271 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002272 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2273 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002274 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002275
Chandler Carrutha88a22182011-07-14 08:20:46 +00002276 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002277 Record.push_back(ME->isBuiltinMacro());
2278 if (ME->isBuiltinMacro())
2279 AddIdentifierRef(ME->getName(), Record);
2280 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002281 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002282 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002283 continue;
2284 }
2285
2286 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2287 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002288 Record.push_back(ID->getFileName().size());
2289 Record.push_back(ID->wasInQuotes());
2290 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002291 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002292 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002293 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002294 // Check that the FileEntry is not null because it was not resolved and
2295 // we create a PCH even with compiler errors.
2296 if (ID->getFile())
2297 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002298 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2299 continue;
2300 }
2301
2302 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2303 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002304 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002305
Douglas Gregoraae92242010-03-19 21:51:54 +00002306 // Write the offsets table for the preprocessing record.
2307 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002308 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2309
Douglas Gregoraae92242010-03-19 21:51:54 +00002310 // Write the offsets table for identifier IDs.
2311 using namespace llvm;
2312 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002313 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002316 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002317
Douglas Gregoraae92242010-03-19 21:51:54 +00002318 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002319 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002320 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002321 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2322 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002323 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002324}
2325
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002326unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2327 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2328 if (Known != SubmoduleIDs.end())
2329 return Known->second;
2330
2331 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2332}
2333
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002334unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2335 if (!Mod)
2336 return 0;
2337
2338 llvm::DenseMap<Module *, unsigned>::const_iterator
2339 Known = SubmoduleIDs.find(Mod);
2340 if (Known != SubmoduleIDs.end())
2341 return Known->second;
2342
2343 return 0;
2344}
2345
Douglas Gregor253eefe2011-12-01 00:59:36 +00002346/// \brief Compute the number of modules within the given tree (including the
2347/// given module).
2348static unsigned getNumberOfModules(Module *Mod) {
2349 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002350 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2351 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002352 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002353 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002354
2355 return ChildModules + 1;
2356}
2357
Douglas Gregorde3ef502011-11-30 23:21:26 +00002358void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002359 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002360 // FIXME: This feels like it belongs somewhere else, but there are no
2361 // other consumers of this information.
2362 SourceManager &SrcMgr = PP->getSourceManager();
2363 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002364 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002365 if (Module *ImportedFrom
2366 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2367 SrcMgr))) {
2368 ImportedFrom->Imports.push_back(I->getImportedModule());
2369 }
2370 }
2371
Douglas Gregor69021972011-11-30 17:33:56 +00002372 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002373 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002374
2375 // Write the abbreviations needed for the submodules block.
2376 using namespace llvm;
2377 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2378 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2390 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2391
2392 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002393 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2395 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2396
2397 Abbrev = new BitCodeAbbrev();
2398 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2400 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002401
2402 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002403 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2405 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2406
2407 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002408 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2410 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2411
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002412 Abbrev = new BitCodeAbbrev();
2413 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002416 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2417
Douglas Gregor59527662012-10-15 06:28:11 +00002418 Abbrev = new BitCodeAbbrev();
2419 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2421 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2422
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002423 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002424 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2426 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2427
2428 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002429 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2431 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2432
2433 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002434 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2435 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2436 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2437
2438 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002439 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2440 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2442 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2443
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002444 Abbrev = new BitCodeAbbrev();
2445 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2447 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2448
Douglas Gregorfb912652013-03-20 21:10:35 +00002449 Abbrev = new BitCodeAbbrev();
2450 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2452 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2453 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2454
Douglas Gregor253eefe2011-12-01 00:59:36 +00002455 // Write the submodule metadata block.
2456 RecordData Record;
2457 Record.push_back(getNumberOfModules(WritingModule));
2458 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2459 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2460
Douglas Gregor69021972011-11-30 17:33:56 +00002461 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002462 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002463 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002464 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002465 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002466 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002467 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002468
2469 // Emit the definition of the block.
2470 Record.clear();
2471 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002472 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002473 if (Mod->Parent) {
2474 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2475 Record.push_back(SubmoduleIDs[Mod->Parent]);
2476 } else {
2477 Record.push_back(0);
2478 }
2479 Record.push_back(Mod->IsFramework);
2480 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002481 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002482 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002483 Record.push_back(Mod->InferSubmodules);
2484 Record.push_back(Mod->InferExplicitSubmodules);
2485 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002486 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002487 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2488
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002489 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002490 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002491 Record.clear();
2492 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002493 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002494 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002495 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002496 }
2497
Douglas Gregor69021972011-11-30 17:33:56 +00002498 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002499 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002500 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002501 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002502 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002503 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002504 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2505 Record.clear();
2506 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2507 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2508 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002509 }
Richard Smith306d8922014-10-22 23:50:56 +00002510
Douglas Gregor69021972011-11-30 17:33:56 +00002511 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002512 struct {
2513 unsigned Kind;
2514 unsigned Abbrev;
2515 ArrayRef<const FileEntry*> Headers;
2516 } HeaderLists[] = {
2517 {SUBMODULE_HEADER, HeaderAbbrev, Mod->NormalHeaders},
2518 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Mod->TextualHeaders},
2519 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Mod->PrivateHeaders},
2520 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2521 Mod->PrivateTextualHeaders},
2522 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Mod->ExcludedHeaders},
2523 {SUBMODULE_TOPHEADER, TopHeaderAbbrev,
2524 Mod->getTopHeaders(PP->getFileManager())}
2525 };
2526 for (auto &HL : HeaderLists) {
Douglas Gregor69021972011-11-30 17:33:56 +00002527 Record.clear();
Richard Smith202210b2014-10-24 20:23:01 +00002528 Record.push_back(HL.Kind);
2529 for (auto *H : HL.Headers)
2530 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002531 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002532
2533 // Emit the imports.
2534 if (!Mod->Imports.empty()) {
2535 Record.clear();
2536 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002537 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002538 assert(ImportedID && "Unknown submodule!");
2539 Record.push_back(ImportedID);
2540 }
2541 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2542 }
2543
Douglas Gregor24bb9232011-12-02 18:58:38 +00002544 // Emit the exports.
2545 if (!Mod->Exports.empty()) {
2546 Record.clear();
2547 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002548 if (Module *Exported = Mod->Exports[I].getPointer()) {
2549 unsigned ExportedID = SubmoduleIDs[Exported];
2550 assert(ExportedID > 0 && "Unknown submodule ID?");
2551 Record.push_back(ExportedID);
2552 } else {
2553 Record.push_back(0);
2554 }
2555
Douglas Gregor24bb9232011-12-02 18:58:38 +00002556 Record.push_back(Mod->Exports[I].getInt());
2557 }
2558 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2559 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002560
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002561 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2562 // Might be unnecessary as use declarations are only used to build the
2563 // module itself.
2564
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002565 // Emit the link libraries.
2566 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2567 Record.clear();
2568 Record.push_back(SUBMODULE_LINK_LIBRARY);
2569 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2570 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2571 Mod->LinkLibraries[I].Library);
2572 }
2573
Douglas Gregorfb912652013-03-20 21:10:35 +00002574 // Emit the conflicts.
2575 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2576 Record.clear();
2577 Record.push_back(SUBMODULE_CONFLICT);
2578 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2579 assert(OtherID && "Unknown submodule!");
2580 Record.push_back(OtherID);
2581 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2582 Mod->Conflicts[I].Message);
2583 }
2584
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002585 // Emit the configuration macros.
2586 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2587 Record.clear();
2588 Record.push_back(SUBMODULE_CONFIG_MACRO);
2589 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2590 Mod->ConfigMacros[I]);
2591 }
2592
Douglas Gregor69021972011-11-30 17:33:56 +00002593 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002594 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2595 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002596 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002597 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002598 }
2599
2600 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002601
2602 assert((NextSubmoduleID - FirstSubmoduleID
2603 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002604}
2605
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002606serialization::SubmoduleID
2607ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002608 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002609 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002610
2611 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002612 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002613 Module *OwningMod
2614 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002615 if (!OwningMod)
2616 return 0;
2617
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002618 // Check whether this submodule is part of our own module.
2619 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002620 return 0;
2621
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002622 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002623}
2624
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002625void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2626 bool isModule) {
2627 // Make sure set diagnostic pragmas don't affect the translation unit that
2628 // imports the module.
2629 // FIXME: Make diagnostic pragma sections work properly with modules.
2630 if (isModule)
2631 return;
2632
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002633 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2634 DiagStateIDMap;
2635 unsigned CurrID = 0;
2636 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002637 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002638 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002639 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2640 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002641 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002642 if (point.Loc.isInvalid())
2643 continue;
2644
2645 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002646 unsigned &DiagStateID = DiagStateIDMap[point.State];
2647 Record.push_back(DiagStateID);
2648
2649 if (DiagStateID == 0) {
2650 DiagStateID = ++CurrID;
2651 for (DiagnosticsEngine::DiagState::const_iterator
2652 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2653 if (I->second.isPragma()) {
2654 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002655 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002656 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002657 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002658 Record.push_back(-1); // mark the end of the diag/map pairs for this
2659 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002660 }
2661 }
2662
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002663 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002664 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002665}
2666
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002667void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2668 if (CXXBaseSpecifiersOffsets.empty())
2669 return;
2670
2671 RecordData Record;
2672
2673 // Create a blob abbreviation for the C++ base specifiers offsets.
2674 using namespace llvm;
2675
2676 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2677 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2678 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2679 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2680 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2681
Douglas Gregorc27b2872011-08-04 00:01:48 +00002682 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002683 Record.clear();
2684 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2685 Record.push_back(CXXBaseSpecifiersOffsets.size());
2686 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002687 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002688}
2689
Douglas Gregorc5046832009-04-27 18:38:38 +00002690//===----------------------------------------------------------------------===//
2691// Type Serialization
2692//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002693
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002694/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002695void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002696 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002697 if (Idx.getIndex() == 0) // we haven't seen this type before.
2698 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002699
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002700 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002701
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002702 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002703 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002704 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002705 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002706 else if (TypeOffsets.size() < Index) {
2707 TypeOffsets.resize(Index + 1);
2708 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002709 }
2710
2711 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002712
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002713 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002714 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002715 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002716
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002717 if (T.hasLocalNonFastQualifiers()) {
2718 Qualifiers Qs = T.getLocalQualifiers();
2719 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002720 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002721 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002722 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002723 } else {
2724 switch (T->getTypeClass()) {
2725 // For all of the concrete, non-dependent types, call the
2726 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002728 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002729#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002730#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002731 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002732 }
2733
2734 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002735 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002736
2737 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002738 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002739}
2740
Douglas Gregorc5046832009-04-27 18:38:38 +00002741//===----------------------------------------------------------------------===//
2742// Declaration Serialization
2743//===----------------------------------------------------------------------===//
2744
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002745/// \brief Write the block containing all of the declaration IDs
2746/// lexically declared within the given DeclContext.
2747///
2748/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2749/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002750uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002751 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002752 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002753 return 0;
2754
Douglas Gregor8f45df52009-04-16 22:23:12 +00002755 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002756 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002757 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002758 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002759 for (const auto *D : DC->decls())
2760 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002761
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002762 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002763 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002764 return Offset;
2765}
2766
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002767void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002768 using namespace llvm;
2769 RecordData Record;
2770
2771 // Write the type offsets array
2772 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002773 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2777 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2778 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002779 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002780 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002781 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002782 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002783
2784 // Write the declaration offsets array
2785 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002786 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2790 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2791 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002792 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002793 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002794 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002795 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002796}
2797
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002798void ASTWriter::WriteFileDeclIDsMap() {
2799 using namespace llvm;
2800 RecordData Record;
2801
2802 // Join the vectors of DeclIDs from all files.
2803 SmallVector<DeclID, 256> FileSortedIDs;
2804 for (FileDeclIDsTy::iterator
2805 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2806 DeclIDInFileInfo &Info = *FI->second;
2807 Info.FirstDeclIndex = FileSortedIDs.size();
2808 for (LocDeclIDsTy::iterator
2809 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2810 FileSortedIDs.push_back(DI->second);
2811 }
2812
2813 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2814 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2817 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2818 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002819 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002820 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2821}
2822
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002823void ASTWriter::WriteComments() {
2824 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002825 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002826 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002827 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2828 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002829 I != E; ++I) {
2830 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002831 AddSourceRange((*I)->getSourceRange(), Record);
2832 Record.push_back((*I)->getKind());
2833 Record.push_back((*I)->isTrailingComment());
2834 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002835 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2836 }
2837 Stream.ExitBlock();
2838}
2839
Douglas Gregorc5046832009-04-27 18:38:38 +00002840//===----------------------------------------------------------------------===//
2841// Global Method Pool and Selector Serialization
2842//===----------------------------------------------------------------------===//
2843
Douglas Gregore84a9da2009-04-20 20:36:09 +00002844namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002845// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002846class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002847 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002848
2849public:
2850 typedef Selector key_type;
2851 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002852
Sebastian Redl834bb972010-08-04 17:20:04 +00002853 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002854 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002855 ObjCMethodList Instance, Factory;
2856 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002857 typedef const data_type& data_type_ref;
2858
Justin Bogner25463f12014-04-18 20:27:24 +00002859 typedef unsigned hash_value_type;
2860 typedef unsigned offset_type;
2861
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002862 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002863
Justin Bogner25463f12014-04-18 20:27:24 +00002864 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002865 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002866 }
Mike Stump11289f42009-09-09 15:08:12 +00002867
2868 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002869 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002870 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002871 using namespace llvm::support;
2872 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002873 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002874 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002875 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2876 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002877 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002878 if (Method->Method)
2879 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002880 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002881 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002882 if (Method->Method)
2883 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002884 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002885 return std::make_pair(KeyLen, DataLen);
2886 }
Mike Stump11289f42009-09-09 15:08:12 +00002887
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002888 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002889 using namespace llvm::support;
2890 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002891 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002892 assert((Start >> 32) == 0 && "Selector key offset too large");
2893 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002894 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002895 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002896 if (N == 0)
2897 N = 1;
2898 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002899 LE.write<uint32_t>(
2900 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002901 }
Mike Stump11289f42009-09-09 15:08:12 +00002902
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002903 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002904 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002905 using namespace llvm::support;
2906 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002907 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002908 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002909 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002910 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002911 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002912 if (Method->Method)
2913 ++NumInstanceMethods;
2914
2915 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002916 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002917 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002918 if (Method->Method)
2919 ++NumFactoryMethods;
2920
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002921 unsigned InstanceBits = Methods.Instance.getBits();
2922 assert(InstanceBits < 4);
2923 unsigned NumInstanceMethodsAndBits =
2924 (NumInstanceMethods << 2) | InstanceBits;
2925 unsigned FactoryBits = Methods.Factory.getBits();
2926 assert(FactoryBits < 4);
2927 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Justin Bognere1c147c2014-03-28 22:03:19 +00002928 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2929 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002930 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002931 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002932 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002933 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002934 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002935 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002936 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002937 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002938
2939 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002940 }
2941};
2942} // end anonymous namespace
2943
Sebastian Redla19a67f2010-08-03 21:58:15 +00002944/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002945///
2946/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002947/// in an on-disk hash table indexed by the selector. The hash table also
2948/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002949void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002950 using namespace llvm;
2951
Sebastian Redla19a67f2010-08-03 21:58:15 +00002952 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002953 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002954 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002955 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002956 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002957 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002958 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002959 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002960
Sebastian Redla19a67f2010-08-03 21:58:15 +00002961 // Create the on-disk hash table representation. We walk through every
2962 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002963 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002964 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002965 I = SelectorIDs.begin(), E = SelectorIDs.end();
2966 I != E; ++I) {
2967 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002968 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002969 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002970 I->second,
2971 ObjCMethodList(),
2972 ObjCMethodList()
2973 };
2974 if (F != SemaRef.MethodPool.end()) {
2975 Data.Instance = F->second.first;
2976 Data.Factory = F->second.second;
2977 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002978 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002979 // changed.
2980 if (Chain && I->second < FirstSelectorID) {
2981 // Selector already exists. Did it change?
2982 bool changed = false;
2983 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002984 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002985 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002986 changed = true;
2987 }
2988 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002989 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002990 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002991 changed = true;
2992 }
2993 if (!changed)
2994 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002995 } else if (Data.Instance.Method || Data.Factory.Method) {
2996 // A new method pool entry.
2997 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002998 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002999 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003000 }
3001
Douglas Gregorc78d3462009-04-24 21:10:55 +00003002 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003003 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003004 uint32_t BucketOffset;
3005 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003006 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003007 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003008 llvm::raw_svector_ostream Out(MethodPool);
3009 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003010 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003011 BucketOffset = Generator.Emit(Out, Trait);
3012 }
3013
3014 // Create a blob abbreviation
3015 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003016 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3020 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3021
Douglas Gregor95c13f52009-04-25 17:48:32 +00003022 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003023 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003024 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003025 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003026 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003027 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00003028
3029 // Create a blob abbreviation for the selector table offsets.
3030 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003031 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3035 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3036
3037 // Write the selector offsets table.
3038 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003039 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003040 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003041 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003042 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003043 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003044 }
3045}
3046
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003047/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003048void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003049 using namespace llvm;
3050 if (SemaRef.ReferencedSelectors.empty())
3051 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003052
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003053 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003054
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003055 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003056 // very tricky to fix, and given that @selector shouldn't really appear in
3057 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003058 for (DenseMap<Selector, SourceLocation>::iterator S =
3059 SemaRef.ReferencedSelectors.begin(),
3060 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3061 Selector Sel = (*S).first;
3062 SourceLocation Loc = (*S).second;
3063 AddSelectorRef(Sel, Record);
3064 AddSourceLocation(Loc, Record);
3065 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003066 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003067}
3068
Douglas Gregorc5046832009-04-27 18:38:38 +00003069//===----------------------------------------------------------------------===//
3070// Identifier Table Serialization
3071//===----------------------------------------------------------------------===//
3072
Douglas Gregorc78d3462009-04-24 21:10:55 +00003073namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003074class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003075 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00003076 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003077 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003078 bool IsModule;
3079
Douglas Gregor1d583f22009-04-28 21:18:29 +00003080 /// \brief Determines whether this is an "interesting" identifier
3081 /// that needs a full IdentifierInfo structure written into the hash
3082 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003083 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003084 if (II->isPoisoned() ||
3085 II->isExtensionToken() ||
3086 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003087 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003088 II->getFETokenInfo<void>())
3089 return true;
3090
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003091 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00003092 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003093
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003094 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003095 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003096 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003097
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003098 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3099 if (!IsModule)
3100 return !shouldIgnoreMacro(Macro, IsModule, PP);
Richard Smithdaa69e02014-07-25 04:40:03 +00003101
3102 MacroState State;
3103 if (getFirstPublicSubmoduleMacro(Macro, State))
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003104 return true;
3105 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003106
3107 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003108 }
3109
Richard Smithdaa69e02014-07-25 04:40:03 +00003110 enum class SubmoduleMacroState {
3111 /// We've seen nothing about this macro.
3112 None,
3113 /// We've seen a public visibility directive.
3114 Public,
3115 /// We've either exported a macro for this module or found that the
3116 /// module's definition of this macro is private.
3117 Done
3118 };
3119 typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
Richard Smith49f906a2014-03-01 00:08:04 +00003120
3121 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003122 getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3123 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3124 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003125 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003126 }
3127
Richard Smith49f906a2014-03-01 00:08:04 +00003128 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003129 getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
Richard Smith49f906a2014-03-01 00:08:04 +00003130 if (MacroDirective *NextMD =
Richard Smithdaa69e02014-07-25 04:40:03 +00003131 getPublicSubmoduleMacro(MD->getPrevious(), State))
3132 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003133 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003134 }
3135
Richard Smithdaa69e02014-07-25 04:40:03 +00003136 /// \brief Traverses the macro directives history and returns the next
3137 /// public macro definition or undefinition that has not been found so far.
3138 ///
Richard Smith49f906a2014-03-01 00:08:04 +00003139 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003140 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00003141 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
Richard Smithdaa69e02014-07-25 04:40:03 +00003142 MacroState &State) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003143 if (!MD)
Craig Toppera13603a2014-05-22 05:54:18 +00003144 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003145
Richard Smith49f906a2014-03-01 00:08:04 +00003146 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003147 for (; MD; MD = MD->getPrevious()) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003148 // Once we hit an ignored macro, we're done: the rest of the chain
3149 // will all be ignored macros.
3150 if (shouldIgnoreMacro(MD, IsModule, PP))
3151 break;
Richard Smith57721ac2014-07-21 04:10:40 +00003152
Richard Smithdaa69e02014-07-25 04:40:03 +00003153 // If this macro was imported, re-export it.
3154 if (MD->isImported())
3155 return MD;
Richard Smith57721ac2014-07-21 04:10:40 +00003156
Richard Smithdaa69e02014-07-25 04:40:03 +00003157 SubmoduleID ModID = getSubmoduleID(MD);
3158 auto &S = State[ModID];
3159 assert(ModID && "found macro in no submodule");
Richard Smith49f906a2014-03-01 00:08:04 +00003160
Richard Smithdaa69e02014-07-25 04:40:03 +00003161 if (S == SubmoduleMacroState::Done)
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003162 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003163
Richard Smithdaa69e02014-07-25 04:40:03 +00003164 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3165 // The latest visibility directive for a name in a submodule affects all
3166 // the directives that come before it.
3167 if (S == SubmoduleMacroState::None)
3168 S = VisMD->isPublic() ? SubmoduleMacroState::Public
3169 : SubmoduleMacroState::Done;
3170 } else {
3171 S = SubmoduleMacroState::Done;
Richard Smith49f906a2014-03-01 00:08:04 +00003172 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003173 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003174 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003175
Craig Toppera13603a2014-05-22 05:54:18 +00003176 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003177 }
3178
Richard Smithdaa69e02014-07-25 04:40:03 +00003179 ArrayRef<SubmoduleID>
3180 getOverriddenSubmodules(MacroDirective *MD,
3181 SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3182 assert(!isa<VisibilityMacroDirective>(MD) &&
3183 "only #define and #undef can override");
3184 if (MD->isImported())
3185 return MD->getOverriddenModules();
3186
3187 ScratchSpace.clear();
3188 SubmoduleID ModID = getSubmoduleID(MD);
3189 for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3190 if (shouldIgnoreMacro(MD, IsModule, PP))
3191 break;
3192
3193 // If this is a definition from a submodule import, that submodule's
3194 // definition is overridden by the definition or undefinition that we
3195 // started with.
3196 if (MD->isImported()) {
3197 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3198 SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3199 assert(DefModuleID && "imported macro has no owning module");
3200 ScratchSpace.push_back(DefModuleID);
3201 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3202 // If we override a #undef, we override anything that #undef overrides.
3203 // We don't need to override it, since an active #undef doesn't affect
3204 // the meaning of a macro.
3205 auto Overrides = UndefMD->getOverriddenModules();
3206 ScratchSpace.insert(ScratchSpace.end(),
3207 Overrides.begin(), Overrides.end());
3208 }
3209 }
3210
3211 // Stop once we leave the original macro's submodule.
3212 //
3213 // Either this submodule #included another submodule of the same
3214 // module or it just happened to be built after the other module.
3215 // In the former case, we override the submodule's macro.
3216 //
3217 // FIXME: In the latter case, we shouldn't do so, but we can't tell
3218 // these cases apart.
3219 //
3220 // FIXME: We can leave this submodule and re-enter it if it #includes a
3221 // header within a different submodule of the same module. In such cases
3222 // the overrides list will be incomplete.
3223 SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3224 if (DirectiveModuleID != ModID) {
3225 if (DirectiveModuleID && !MD->isImported())
3226 ScratchSpace.push_back(DirectiveModuleID);
3227 break;
3228 }
3229 }
3230
3231 std::sort(ScratchSpace.begin(), ScratchSpace.end());
3232 ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3233 ScratchSpace.end());
3234 return ScratchSpace;
3235 }
3236
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003237 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Richard Smith57721ac2014-07-21 04:10:40 +00003238 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003239 }
3240
Douglas Gregore84a9da2009-04-20 20:36:09 +00003241public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003242 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003243 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003244
Sebastian Redl539c5062010-08-18 23:57:32 +00003245 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003246 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003247
Justin Bogner25463f12014-04-18 20:27:24 +00003248 typedef unsigned hash_value_type;
3249 typedef unsigned offset_type;
3250
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003251 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3252 IdentifierResolver &IdResolver, bool IsModule)
3253 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003254
Justin Bogner25463f12014-04-18 20:27:24 +00003255 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003256 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003257 }
Mike Stump11289f42009-09-09 15:08:12 +00003258
3259 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003260 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003261 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003262 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Craig Toppera13603a2014-05-22 05:54:18 +00003263 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003264 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003265 DataLen += 2; // 2 bytes for builtin ID
3266 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003267 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003268 DataLen += 4; // MacroDirectives offset.
3269 if (IsModule) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003270 MacroState State;
3271 SmallVector<SubmoduleID, 16> Scratch;
3272 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3273 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003274 DataLen += 4; // MacroInfo ID or ModuleID.
Richard Smithdaa69e02014-07-25 04:40:03 +00003275 if (unsigned NumOverrides =
3276 getOverriddenSubmodules(MD, Scratch).size())
3277 DataLen += 4 * (1 + NumOverrides);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003278 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003279 DataLen += 4; // 0 terminator.
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003280 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003281 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003282
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003283 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3284 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003285 D != DEnd; ++D)
Richard Smithdaa69e02014-07-25 04:40:03 +00003286 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003287 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003288 using namespace llvm::support;
3289 endian::Writer<little> LE(Out);
3290
3291 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003292 // We emit the key length after the data length so that every
3293 // string is preceded by a 16-bit length. This matches the PTH
3294 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003295 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003296 return std::make_pair(KeyLen, DataLen);
3297 }
Mike Stump11289f42009-09-09 15:08:12 +00003298
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003299 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003300 unsigned KeyLen) {
3301 // Record the location of the key data. This is used when generating
3302 // the mapping from persistent IDs to strings.
3303 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003304 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003305 }
Mike Stump11289f42009-09-09 15:08:12 +00003306
Richard Smith49f906a2014-03-01 00:08:04 +00003307 static void emitMacroOverrides(raw_ostream &Out,
Craig Topper00bbdcf2014-06-28 23:22:23 +00003308 ArrayRef<SubmoduleID> Overridden) {
Richard Smith49f906a2014-03-01 00:08:04 +00003309 if (!Overridden.empty()) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003310 using namespace llvm::support;
3311 endian::Writer<little> LE(Out);
3312 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Richard Smithdaa69e02014-07-25 04:40:03 +00003313 for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3314 assert(Overridden[I] && "zero module ID for override");
Justin Bognere1c147c2014-03-28 22:03:19 +00003315 LE.write<uint32_t>(Overridden[I]);
Richard Smithdaa69e02014-07-25 04:40:03 +00003316 }
Richard Smith49f906a2014-03-01 00:08:04 +00003317 }
3318 }
3319
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003320 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003321 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003322 using namespace llvm::support;
3323 endian::Writer<little> LE(Out);
Craig Toppera13603a2014-05-22 05:54:18 +00003324 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003325 if (!isInterestingIdentifier(II, Macro)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003326 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003327 return;
3328 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003329
Justin Bognere1c147c2014-03-28 22:03:19 +00003330 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003331 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3332 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003333 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003334 Bits = 0;
3335 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003336 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003337 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003338 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3339 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003340 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003341 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003342 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003343
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003344 if (HadMacroDefinition) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003345 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003346 if (IsModule) {
3347 // Write the IDs of macros coming from different submodules.
Richard Smithdaa69e02014-07-25 04:40:03 +00003348 MacroState State;
3349 SmallVector<SubmoduleID, 16> Scratch;
3350 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3351 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003352 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003353 // FIXME: If this macro directive was created by #pragma pop_macros,
3354 // or if it was created implicitly by resolving conflicting macros,
3355 // it may be for a different submodule from the one in the MacroInfo
3356 // object. If so, we should write out its owning ModuleID.
3357 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Richard Smith49f906a2014-03-01 00:08:04 +00003358 assert(InfoID);
Justin Bognere1c147c2014-03-28 22:03:19 +00003359 LE.write<uint32_t>(InfoID << 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003360 } else {
Richard Smithdaa69e02014-07-25 04:40:03 +00003361 auto *UndefMD = cast<UndefMacroDirective>(MD);
3362 SubmoduleID Mod = UndefMD->isImported()
3363 ? UndefMD->getOwningModuleID()
3364 : getSubmoduleID(UndefMD);
3365 LE.write<uint32_t>((Mod << 1) | 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003366 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003367 emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003368 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003369 LE.write<uint32_t>(0xdeadbeef);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003370 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003371 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003372
Douglas Gregora868bbd2009-04-21 22:25:48 +00003373 // Emit the declaration IDs in reverse order, because the
3374 // IdentifierResolver provides the declarations as they would be
3375 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003376 // "stat"), but the ASTReader adds declarations to the end of the list
3377 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003378 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003379 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3380 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003381 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003382 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003383 D != DEnd; ++D)
Justin Bognere1c147c2014-03-28 22:03:19 +00003384 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003385 }
3386
3387 /// \brief Returns the most recent local decl or the given decl if there are
3388 /// no local ones. The given decl is assumed to be the most recent one.
3389 Decl *getMostRecentLocalDecl(Decl *Orig) {
3390 // The only way a "from AST file" decl would be more recent from a local one
3391 // is if it came from a module.
3392 if (!PP.getLangOpts().Modules)
3393 return Orig;
3394
3395 // Look for a local in the decl chain.
3396 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3397 if (!D->isFromASTFile())
3398 return D;
3399 // If we come up a decl from a (chained-)PCH stop since we won't find a
3400 // local one.
3401 if (D->getOwningModuleID() == 0)
3402 break;
3403 }
3404
3405 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003406 }
3407};
3408} // end anonymous namespace
3409
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003410/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003411///
3412/// The identifier table consists of a blob containing string data
3413/// (the actual identifiers themselves) and a separate "offsets" index
3414/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003415void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3416 IdentifierResolver &IdResolver,
3417 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003418 using namespace llvm;
3419
3420 // Create and write out the blob that contains the identifier
3421 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003422 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003423 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003424 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003425
Douglas Gregore6648fb2009-04-28 20:33:11 +00003426 // Look for any identifiers that were named while processing the
3427 // headers, but are otherwise not needed. We add these to the hash
3428 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003429 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003430 // file.
3431 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3432 IDEnd = PP.getIdentifierTable().end();
3433 ID != IDEnd; ++ID)
3434 getIdentifierRef(ID->second);
3435
Sebastian Redlff4a2952010-07-23 23:49:55 +00003436 // Create the on-disk hash table representation. We only store offsets
3437 // for identifiers that appear here for the first time.
3438 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003439 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003440 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3441 ID != IDEnd; ++ID) {
3442 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003443 if (!Chain || !ID->first->isFromAST() ||
3444 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003445 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003446 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003447 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003448
Douglas Gregore84a9da2009-04-20 20:36:09 +00003449 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003450 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003451 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003452 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003453 using namespace llvm::support;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003454 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003455 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003456 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003457 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003458 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003459 }
3460
3461 // Create a blob abbreviation
3462 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003463 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003466 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003467
3468 // Write the identifier table
3469 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003470 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003471 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003472 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003473 }
3474
3475 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003476 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003477 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3481 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3482
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003483#ifndef NDEBUG
3484 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3485 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3486#endif
3487
Douglas Gregor0e149972009-04-25 19:10:14 +00003488 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003489 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003490 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003491 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003492 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003493 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003494}
3495
Douglas Gregorc5046832009-04-27 18:38:38 +00003496//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003497// DeclContext's Name Lookup Table Serialization
3498//===----------------------------------------------------------------------===//
3499
Richard Smithbb853c72014-08-13 01:23:33 +00003500/// Determine the declaration that should be put into the name lookup table to
3501/// represent the given declaration in this module. This is usually D itself,
3502/// but if D was imported and merged into a local declaration, we want the most
3503/// recent local declaration instead. The chosen declaration will be the most
3504/// recent declaration in any module that imports this one.
3505static NamedDecl *getDeclForLocalLookup(NamedDecl *D) {
Richard Smith8c913ec2014-08-14 02:21:01 +00003506 if (!D->isFromASTFile())
3507 return D;
3508
3509 if (Decl *Redecl = D->getPreviousDecl()) {
3510 // For Redeclarable decls, a prior declaration might be local.
3511 for (; Redecl; Redecl = Redecl->getPreviousDecl())
3512 if (!Redecl->isFromASTFile())
3513 return cast<NamedDecl>(Redecl);
3514 } else if (Decl *First = D->getCanonicalDecl()) {
3515 // For Mergeable decls, the first decl might be local.
3516 if (!First->isFromASTFile())
3517 return cast<NamedDecl>(First);
3518 }
3519
3520 // All declarations are imported. Our most recent declaration will also be
3521 // the most recent one in anyone who imports us.
Richard Smithbb853c72014-08-13 01:23:33 +00003522 return D;
3523}
3524
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003525namespace {
3526// Trait used for the on-disk hash table used in the method pool.
3527class ASTDeclContextNameLookupTrait {
3528 ASTWriter &Writer;
3529
3530public:
3531 typedef DeclarationName key_type;
3532 typedef key_type key_type_ref;
3533
3534 typedef DeclContext::lookup_result data_type;
3535 typedef const data_type& data_type_ref;
3536
Justin Bogner25463f12014-04-18 20:27:24 +00003537 typedef unsigned hash_value_type;
3538 typedef unsigned offset_type;
3539
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003540 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3541
Justin Bogner25463f12014-04-18 20:27:24 +00003542 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003543 llvm::FoldingSetNodeID ID;
3544 ID.AddInteger(Name.getNameKind());
3545
3546 switch (Name.getNameKind()) {
3547 case DeclarationName::Identifier:
3548 ID.AddString(Name.getAsIdentifierInfo()->getName());
3549 break;
3550 case DeclarationName::ObjCZeroArgSelector:
3551 case DeclarationName::ObjCOneArgSelector:
3552 case DeclarationName::ObjCMultiArgSelector:
3553 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3554 break;
3555 case DeclarationName::CXXConstructorName:
3556 case DeclarationName::CXXDestructorName:
3557 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003558 break;
3559 case DeclarationName::CXXOperatorName:
3560 ID.AddInteger(Name.getCXXOverloadedOperator());
3561 break;
3562 case DeclarationName::CXXLiteralOperatorName:
3563 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3564 case DeclarationName::CXXUsingDirective:
3565 break;
3566 }
3567
3568 return ID.ComputeHash();
3569 }
3570
3571 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003572 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003573 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003574 using namespace llvm::support;
3575 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003576 unsigned KeyLen = 1;
3577 switch (Name.getNameKind()) {
3578 case DeclarationName::Identifier:
3579 case DeclarationName::ObjCZeroArgSelector:
3580 case DeclarationName::ObjCOneArgSelector:
3581 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003582 case DeclarationName::CXXLiteralOperatorName:
3583 KeyLen += 4;
3584 break;
3585 case DeclarationName::CXXOperatorName:
3586 KeyLen += 1;
3587 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003588 case DeclarationName::CXXConstructorName:
3589 case DeclarationName::CXXDestructorName:
3590 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003591 case DeclarationName::CXXUsingDirective:
3592 break;
3593 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003594 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003595
3596 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003597 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003598 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003599
3600 return std::make_pair(KeyLen, DataLen);
3601 }
3602
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003603 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003604 using namespace llvm::support;
3605 endian::Writer<little> LE(Out);
3606 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003607 switch (Name.getNameKind()) {
3608 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003609 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003610 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003611 case DeclarationName::ObjCZeroArgSelector:
3612 case DeclarationName::ObjCOneArgSelector:
3613 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003614 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003615 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003616 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003617 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3618 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003619 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003620 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003621 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003622 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003623 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003624 case DeclarationName::CXXConstructorName:
3625 case DeclarationName::CXXDestructorName:
3626 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003627 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003628 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003629 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003630
3631 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003632 }
3633
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003634 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003635 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003636 using namespace llvm::support;
3637 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003638 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003639 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003640 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3641 I != E; ++I)
Richard Smithbb853c72014-08-13 01:23:33 +00003642 LE.write<uint32_t>(Writer.GetDeclRef(getDeclForLocalLookup(*I)));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003643
3644 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3645 }
3646};
3647} // end anonymous namespace
3648
Richard Smithcd45dbc2014-04-19 03:48:30 +00003649template<typename Visitor>
3650static void visitLocalLookupResults(const DeclContext *ConstDC,
3651 bool NeedToReconcileExternalVisibleStorage,
3652 Visitor AddLookupResult) {
3653 // FIXME: We need to build the lookups table, which is logically const.
3654 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Richard Smith961eae52014-03-25 01:14:22 +00003655 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3656
Richard Smith961eae52014-03-25 01:14:22 +00003657 SmallVector<DeclarationName, 16> ExternalNames;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003658 for (auto &Lookup : *DC->buildLookup()) {
Richard Smith961eae52014-03-25 01:14:22 +00003659 if (Lookup.second.hasExternalDecls() ||
Richard Smithcd45dbc2014-04-19 03:48:30 +00003660 NeedToReconcileExternalVisibleStorage) {
Richard Smith961eae52014-03-25 01:14:22 +00003661 // We don't know for sure what declarations are found by this name,
3662 // because the external source might have a different set from the set
3663 // that are in the lookup map, and we can't update it now without
3664 // risking invalidating our lookup iterator. So add it to a queue to
3665 // deal with later.
3666 ExternalNames.push_back(Lookup.first);
3667 continue;
3668 }
3669
3670 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3671 }
3672
3673 // Add the names we needed to defer. Note, this shouldn't add any new decls
3674 // to the list we need to serialize: any new declarations we find here should
3675 // be imported from an external source.
3676 // FIXME: What if the external source isn't an ASTReader?
3677 for (const auto &Name : ExternalNames)
Richard Smithcd45dbc2014-04-19 03:48:30 +00003678 AddLookupResult(Name, DC->lookup(Name));
3679}
3680
3681void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
3682 if (UpdatedDeclContexts.insert(DC) && WritingAST) {
3683 // Ensure we emit all the visible declarations.
3684 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3685 [&](DeclarationName Name,
3686 DeclContext::lookup_const_result Result) {
3687 for (auto *Decl : Result)
Richard Smithbb853c72014-08-13 01:23:33 +00003688 GetDeclRef(getDeclForLocalLookup(Decl));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003689 });
3690 }
3691}
3692
3693uint32_t
3694ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3695 llvm::SmallVectorImpl<char> &LookupTable) {
3696 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3697
3698 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3699 Generator;
3700 ASTDeclContextNameLookupTrait Trait(*this);
3701
3702 // Create the on-disk hash table representation.
3703 DeclarationName ConstructorName;
3704 DeclarationName ConversionName;
3705 SmallVector<NamedDecl *, 8> ConstructorDecls;
3706 SmallVector<NamedDecl *, 4> ConversionDecls;
3707
3708 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3709 [&](DeclarationName Name,
3710 DeclContext::lookup_result Result) {
3711 if (Result.empty())
3712 return;
3713
3714 // Different DeclarationName values of certain kinds are mapped to
3715 // identical serialized keys, because we don't want to use type
3716 // identifiers in the keys (since type ids are local to the module).
3717 switch (Name.getNameKind()) {
3718 case DeclarationName::CXXConstructorName:
3719 // There may be different CXXConstructorName DeclarationName values
3720 // in a DeclContext because a UsingDecl that inherits constructors
3721 // has the DeclarationName of the inherited constructors.
3722 if (!ConstructorName)
3723 ConstructorName = Name;
3724 ConstructorDecls.append(Result.begin(), Result.end());
3725 return;
3726
3727 case DeclarationName::CXXConversionFunctionName:
3728 if (!ConversionName)
3729 ConversionName = Name;
3730 ConversionDecls.append(Result.begin(), Result.end());
3731 return;
3732
3733 default:
3734 break;
3735 }
3736
3737 Generator.insert(Name, Result, Trait);
3738 });
Richard Smith961eae52014-03-25 01:14:22 +00003739
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003740 // Add the constructors.
3741 if (!ConstructorDecls.empty()) {
3742 Generator.insert(ConstructorName,
3743 DeclContext::lookup_result(ConstructorDecls.begin(),
3744 ConstructorDecls.end()),
3745 Trait);
3746 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003747
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003748 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003749 if (!ConversionDecls.empty()) {
3750 Generator.insert(ConversionName,
3751 DeclContext::lookup_result(ConversionDecls.begin(),
3752 ConversionDecls.end()),
3753 Trait);
3754 }
3755
3756 // Create the on-disk hash table in a buffer.
3757 llvm::raw_svector_ostream Out(LookupTable);
3758 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003759 using namespace llvm::support;
3760 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003761 return Generator.Emit(Out, Trait);
3762}
3763
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003764/// \brief Write the block containing all of the declaration IDs
3765/// visible from the given DeclContext.
3766///
3767/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003768/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003769uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3770 DeclContext *DC) {
3771 if (DC->getPrimaryContext() != DC)
3772 return 0;
3773
3774 // Since there is no name lookup into functions or methods, don't bother to
3775 // build a visible-declarations table for these entities.
3776 if (DC->isFunctionOrMethod())
3777 return 0;
3778
3779 // If not in C++, we perform name lookup for the translation unit via the
3780 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003781 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003782 return 0;
3783
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003784 // Serialize the contents of the mapping used for lookup. Note that,
3785 // although we have two very different code paths, the serialized
3786 // representation is the same for both cases: a declaration name,
3787 // followed by a size, followed by references to the visible
3788 // declarations that have that name.
3789 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003790 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003791 if (!Map || Map->empty())
3792 return 0;
3793
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003794 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003795 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003796 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003797
3798 // Write the lookup table
3799 RecordData Record;
3800 Record.push_back(DECL_CONTEXT_VISIBLE);
3801 Record.push_back(BucketOffset);
3802 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3803 LookupTable.str());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003804 ++NumVisibleDeclContexts;
3805 return Offset;
3806}
3807
Sebastian Redla4071b42010-08-24 00:50:09 +00003808/// \brief Write an UPDATE_VISIBLE block for the given context.
3809///
3810/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3811/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003812/// (in C++), for namespaces, and for classes with forward-declared unscoped
3813/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003814void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003815 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003816 if (!Map || Map->empty())
3817 return;
3818
Sebastian Redla4071b42010-08-24 00:50:09 +00003819 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003820 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003821 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003822
3823 // Write the lookup table
3824 RecordData Record;
3825 Record.push_back(UPDATE_VISIBLE);
3826 Record.push_back(getDeclID(cast<Decl>(DC)));
3827 Record.push_back(BucketOffset);
3828 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3829}
3830
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003831/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3832void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3833 RecordData Record;
3834 Record.push_back(Opts.fp_contract);
3835 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3836}
3837
3838/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3839void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003840 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003841 return;
3842
3843 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3844 RecordData Record;
3845#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3846#include "clang/Basic/OpenCLExtensions.def"
3847 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3848}
3849
Douglas Gregor358cd442012-01-15 16:58:34 +00003850void ASTWriter::WriteRedeclarations() {
3851 RecordData LocalRedeclChains;
3852 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3853
3854 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3855 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003856 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003857
3858 Decl *MostRecent = First->getMostRecentDecl();
3859
3860 // If we only have a single declaration, there is no point in storing
3861 // a redeclaration chain.
3862 if (First == MostRecent)
3863 continue;
3864
3865 unsigned Offset = LocalRedeclChains.size();
3866 unsigned Size = 0;
3867 LocalRedeclChains.push_back(0); // Placeholder for the size.
3868
3869 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003870 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003871 Prev = Prev->getPreviousDecl()) {
3872 if (!Prev->isFromASTFile()) {
3873 AddDeclRef(Prev, LocalRedeclChains);
3874 ++Size;
3875 }
3876 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003877
3878 if (!First->isFromASTFile() && Chain) {
3879 Decl *FirstFromAST = MostRecent;
3880 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3881 if (Prev->isFromASTFile())
3882 FirstFromAST = Prev;
3883 }
3884
Richard Smith2516ba22014-08-11 18:35:44 +00003885 // FIXME: Do we need to do this for the first declaration from each
3886 // redeclaration chain that was merged into this one?
Douglas Gregor6168bd22013-02-18 15:53:43 +00003887 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3888 }
3889
Douglas Gregor358cd442012-01-15 16:58:34 +00003890 LocalRedeclChains[Offset] = Size;
3891
3892 // Reverse the set of local redeclarations, so that we store them in
3893 // order (since we found them in reverse order).
3894 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3895
Douglas Gregor6168bd22013-02-18 15:53:43 +00003896 // Add the mapping from the first ID from the AST to the set of local
3897 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003898 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3899 LocalRedeclsMap.push_back(Info);
3900
3901 assert(N == Redeclarations.size() &&
3902 "Deserialized a declaration we shouldn't have");
3903 }
3904
3905 if (LocalRedeclChains.empty())
3906 return;
3907
3908 // Sort the local redeclarations map by the first declaration ID,
3909 // since the reader will be performing binary searches on this information.
3910 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3911
3912 // Emit the local redeclarations map.
3913 using namespace llvm;
3914 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3915 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3918 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3919
3920 RecordData Record;
3921 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3922 Record.push_back(LocalRedeclsMap.size());
3923 Stream.EmitRecordWithBlob(AbbrevID, Record,
3924 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3925 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3926
3927 // Emit the redeclaration chains.
3928 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3929}
3930
Douglas Gregor404cdde2012-01-27 01:47:08 +00003931void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003932 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003933 RecordData Categories;
3934
3935 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3936 unsigned Size = 0;
3937 unsigned StartIndex = Categories.size();
3938
3939 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3940
3941 // Allocate space for the size.
3942 Categories.push_back(0);
3943
3944 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003945 for (ObjCInterfaceDecl::known_categories_iterator
3946 Cat = Class->known_categories_begin(),
3947 CatEnd = Class->known_categories_end();
3948 Cat != CatEnd; ++Cat, ++Size) {
3949 assert(getDeclID(*Cat) != 0 && "Bogus category");
3950 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003951 }
3952
3953 // Update the size.
3954 Categories[StartIndex] = Size;
3955
3956 // Record this interface -> category map.
3957 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3958 CategoriesMap.push_back(CatInfo);
3959 }
3960
3961 // Sort the categories map by the definition ID, since the reader will be
3962 // performing binary searches on this information.
3963 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3964
3965 // Emit the categories map.
3966 using namespace llvm;
3967 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3968 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3969 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3971 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3972
3973 RecordData Record;
3974 Record.push_back(OBJC_CATEGORIES_MAP);
3975 Record.push_back(CategoriesMap.size());
3976 Stream.EmitRecordWithBlob(AbbrevID, Record,
3977 reinterpret_cast<char*>(CategoriesMap.data()),
3978 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3979
3980 // Emit the category lists.
3981 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3982}
3983
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003984void ASTWriter::WriteMergedDecls() {
3985 if (!Chain || Chain->MergedDecls.empty())
3986 return;
3987
3988 RecordData Record;
3989 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3990 IEnd = Chain->MergedDecls.end();
3991 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003992 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Richard Smithcd45dbc2014-04-19 03:48:30 +00003993 : GetDeclRef(I->first);
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003994 assert(CanonID && "Merged declaration not known?");
3995
3996 Record.push_back(CanonID);
3997 Record.push_back(I->second.size());
3998 Record.append(I->second.begin(), I->second.end());
3999 }
4000 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
4001}
4002
Richard Smithe40f2ba2013-08-07 21:41:30 +00004003void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4004 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4005
4006 if (LPTMap.empty())
4007 return;
4008
4009 RecordData Record;
4010 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4011 ItEnd = LPTMap.end();
4012 It != ItEnd; ++It) {
4013 LateParsedTemplate *LPT = It->second;
4014 AddDeclRef(It->first, Record);
4015 AddDeclRef(LPT->D, Record);
4016 Record.push_back(LPT->Toks.size());
4017
4018 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4019 TokEnd = LPT->Toks.end();
4020 TokIt != TokEnd; ++TokIt) {
4021 AddToken(*TokIt, Record);
4022 }
4023 }
4024 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4025}
4026
Dario Domizioli13a0a382014-05-23 12:13:25 +00004027/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4028void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4029 RecordData Record;
4030 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4031 AddSourceLocation(PragmaLoc, Record);
4032 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4033}
4034
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004035//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004036// General Serialization Routines
4037//===----------------------------------------------------------------------===//
4038
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004039/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004040void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4041 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004042 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004043 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4044 e = Attrs.end(); i != e; ++i){
4045 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004046 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004047 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004048
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004049#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004050
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004051 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004052}
4053
John McCallf413f5e2013-05-03 00:10:13 +00004054void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4055 AddSourceLocation(Tok.getLocation(), Record);
4056 Record.push_back(Tok.getLength());
4057
4058 // FIXME: When reading literal tokens, reconstruct the literal pointer
4059 // if it is needed.
4060 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4061 // FIXME: Should translate token kind to a stable encoding.
4062 Record.push_back(Tok.getKind());
4063 // FIXME: Should translate token flags to a stable encoding.
4064 Record.push_back(Tok.getFlags());
4065}
4066
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004067void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004068 Record.push_back(Str.size());
4069 Record.insert(Record.end(), Str.begin(), Str.end());
4070}
4071
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004072void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4073 RecordDataImpl &Record) {
4074 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004075 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004076 Record.push_back(*Minor + 1);
4077 else
4078 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004079 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004080 Record.push_back(*Subminor + 1);
4081 else
4082 Record.push_back(0);
4083}
4084
Douglas Gregore84a9da2009-04-20 20:36:09 +00004085/// \brief Note that the identifier II occurs at the given offset
4086/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004087void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004088 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004089 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004090 // up earlier in the chain and thus don't need an offset.
4091 if (ID >= FirstIdentID)
4092 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004093}
4094
Douglas Gregor95c13f52009-04-25 17:48:32 +00004095/// \brief Note that the selector Sel occurs at the given offset
4096/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004097void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004098 unsigned ID = SelectorIDs[Sel];
4099 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004100 // Don't record offsets for selectors that are also available in a different
4101 // file.
4102 if (ID < FirstSelectorID)
4103 return;
4104 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004105}
4106
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004107ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Richard Smith01b2cb42014-07-26 06:37:51 +00004108 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4109 WritingModule(nullptr), WritingAST(false),
4110 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4111 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4112 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4113 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4114 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4115 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4116 NextSubmoduleID(FirstSubmoduleID),
4117 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4118 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4119 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4120 NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4121 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4122 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004123 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004124 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004125 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4126 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4127 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004128
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004129ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004130 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004131}
4132
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004133void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004134 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004135 Module *WritingModule, StringRef isysroot,
4136 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004137 WritingAST = true;
4138
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004139 ASTHasCompilerErrors = hasErrors;
4140
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004141 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004142 Stream.Emit((unsigned)'C', 8);
4143 Stream.Emit((unsigned)'P', 8);
4144 Stream.Emit((unsigned)'C', 8);
4145 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004146
Chris Lattner28fa4e62009-04-26 22:26:21 +00004147 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004148
Douglas Gregoreda8e122011-08-09 15:13:55 +00004149 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004150 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004151 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004152 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004153 Context = nullptr;
4154 PP = nullptr;
4155 this->WritingModule = nullptr;
4156
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004157 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004158}
4159
Douglas Gregora94a1542011-07-27 21:45:57 +00004160template<typename Vector>
4161static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4162 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004163 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4164 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004165 Writer.AddDeclRef(*I, Record);
4166 }
4167}
4168
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004169void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004170 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004171 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004172 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004173 using namespace llvm;
4174
Craig Toppera13603a2014-05-22 05:54:18 +00004175 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004176
Douglas Gregorcf68c582011-12-01 22:20:10 +00004177 // Make sure that the AST reader knows to finalize itself.
4178 if (Chain)
4179 Chain->finalizeForWriting();
4180
Sebastian Redl143413f2010-07-12 22:02:52 +00004181 ASTContext &Context = SemaRef.Context;
4182 Preprocessor &PP = SemaRef.PP;
4183
Douglas Gregordab42432011-08-12 00:15:20 +00004184 // Set up predefined declaration IDs.
4185 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00004186 if (Context.ObjCIdDecl)
4187 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00004188 if (Context.ObjCSelDecl)
4189 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00004190 if (Context.ObjCClassDecl)
4191 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00004192 if (Context.ObjCProtocolClassDecl)
4193 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00004194 if (Context.Int128Decl)
4195 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4196 if (Context.UInt128Decl)
4197 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00004198 if (Context.ObjCInstanceTypeDecl)
4199 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004200 if (Context.BuiltinVaListDecl)
4201 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4202
Douglas Gregor851443c2011-08-12 01:39:19 +00004203 if (!Chain) {
4204 // Make sure that we emit IdentifierInfos (and any attached
4205 // declarations) for builtins. We don't need to do this when we're
4206 // emitting chained PCH files, because all of the builtins will be
4207 // in the original PCH file.
4208 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004209 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004210 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00004211 if (!Context.getLangOpts().NoBuiltin) {
4212 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4213 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004214 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4215 getIdentifierRef(&Table.get(BuiltinNames[I]));
4216 }
4217
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004218 // If there are any out-of-date identifiers, bring them up to date.
4219 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00004220 // Find out-of-date identifiers.
4221 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004222 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4223 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00004224 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004225 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00004226 OutOfDate.push_back(ID->second);
4227 }
4228
4229 // Update the out-of-date identifiers.
4230 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4231 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4232 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004233 }
4234
Richard Smithcd45dbc2014-04-19 03:48:30 +00004235 // If we saw any DeclContext updates before we started writing the AST file,
4236 // make sure all visible decls in those DeclContexts are written out.
4237 if (!UpdatedDeclContexts.empty()) {
4238 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4239 UpdatedDeclContexts.clear();
4240 for (auto *DC : OldUpdatedDeclContexts)
4241 AddUpdatedDeclContext(DC);
4242 }
4243
Chris Lattner0c797362009-09-08 18:19:27 +00004244 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004245 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004246 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004247 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004248 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004249
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004250 // Build a record containing all of the file scoped decls in this file.
4251 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004252 if (!isModule)
4253 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4254 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004255
Douglas Gregor851443c2011-08-12 01:39:19 +00004256 // Build a record containing all of the delegating constructors we still need
4257 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004258 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004259 if (!isModule)
4260 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004261
Douglas Gregor851443c2011-08-12 01:39:19 +00004262 // Write the set of weak, undeclared identifiers. We always write the
4263 // entire table, since later PCH files in a PCH chain are only interested in
4264 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004265 RecordData WeakUndeclaredIdentifiers;
4266 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004267 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004268 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4269 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4270 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4271 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4272 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4273 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4274 }
4275 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004276
Richard Smith78165b52013-01-10 23:43:47 +00004277 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004278 // declarations in this header file. Generally, this record will be
4279 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00004280 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004281 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00004282 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00004283 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00004284 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4285 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00004286 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004287 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00004288 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00004289 }
4290
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004291 // Build a record containing all of the ext_vector declarations.
4292 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004293 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004294
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004295 // Build a record containing all of the VTable uses information.
4296 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004297 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004298 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4299 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4300 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4301 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4302 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004303 }
4304
Nico Weber72889432014-09-06 01:25:55 +00004305 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4306 RecordData UnusedLocalTypedefNameCandidates;
4307 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4308 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4309
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004310 // Build a record containing all of dynamic classes declarations.
4311 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004312 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004313
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004314 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004315 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004316 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004317 I = SemaRef.PendingInstantiations.begin(),
4318 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4319 AddDeclRef(I->first, PendingInstantiations);
4320 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004321 }
4322 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4323 "There are local ones at end of translation unit!");
4324
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004325 // Build a record containing some declaration references.
4326 RecordData SemaDeclRefs;
4327 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4328 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4329 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4330 }
4331
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004332 RecordData CUDASpecialDeclRefs;
4333 if (Context.getcudaConfigureCallDecl()) {
4334 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4335 }
4336
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004337 // Build a record containing all of the known namespaces.
4338 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004339 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004340 I = SemaRef.KnownNamespaces.begin(),
4341 IEnd = SemaRef.KnownNamespaces.end();
4342 I != IEnd; ++I) {
4343 if (!I->second)
4344 AddDeclRef(I->first, KnownNamespaces);
4345 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004346
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004347 // Build a record of all used, undefined objects that require definitions.
4348 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004349
4350 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004351 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004352 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4353 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004354 AddDeclRef(I->first, UndefinedButUsed);
4355 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004356 }
4357
Douglas Gregor112b9072012-10-18 05:31:06 +00004358 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004359 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004360
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004361 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004362 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004363 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004364
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004365 // This is so that older clang versions, before the introduction
4366 // of the control block, can read and reject the newer PCH format.
4367 Record.clear();
4368 Record.push_back(VERSION_MAJOR);
4369 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4370
Douglas Gregor851443c2011-08-12 01:39:19 +00004371 // Create a lexical update block containing all of the declarations in the
4372 // translation unit that do not come from other AST files.
4373 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4374 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004375 for (const auto *I : TU->noload_decls()) {
4376 if (!I->isFromASTFile())
4377 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004378 }
4379
4380 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4381 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4382 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4383 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4384 Record.clear();
4385 Record.push_back(TU_UPDATE_LEXICAL);
4386 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4387 data(NewGlobalDecls));
4388
4389 // And a visible updates block for the translation unit.
4390 Abv = new llvm::BitCodeAbbrev();
4391 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4392 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4393 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4394 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4395 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4396 WriteDeclContextVisibleUpdate(TU);
4397
4398 // If the translation unit has an anonymous namespace, and we don't already
4399 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004400 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004401 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4402 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004403 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004404 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004405 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004406
Richard Smith5652c0f2014-03-21 01:48:23 +00004407 // Add update records for all mangling numbers and static local numbers.
4408 // These aren't really update records, but this is a convenient way of
4409 // tagging this rare extra data onto the declarations.
4410 for (const auto &Number : Context.MangleNumbers)
4411 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004412 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4413 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004414 for (const auto &Number : Context.StaticLocalNumbers)
4415 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004416 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4417 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004418
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004419 // Make sure visible decls, added to DeclContexts previously loaded from
4420 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004421 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004422 I = UpdatingVisibleDecls.begin(),
4423 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4424 GetDeclRef(*I);
4425 }
4426
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004427 // Make sure all decls associated with an identifier are registered for
4428 // serialization.
4429 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4430 IDEnd = PP.getIdentifierTable().end();
4431 ID != IDEnd; ++ID) {
4432 const IdentifierInfo *II = ID->second;
4433 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4434 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4435 DEnd = SemaRef.IdResolver.end();
4436 D != DEnd; ++D) {
4437 GetDeclRef(*D);
4438 }
4439 }
4440 }
4441
Douglas Gregor5204bde2011-08-02 16:26:37 +00004442 // Form the record of special types.
4443 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004444 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004445 AddTypeRef(Context.getFILEType(), SpecialTypes);
4446 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4447 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4448 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4449 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004450 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004451 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004452
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004453 if (Chain) {
4454 // Write the mapping information describing our module dependencies and how
4455 // each of those modules were mapped into our own offset/ID space, so that
4456 // the reader can build the appropriate mapping to its own offset/ID space.
4457 // The map consists solely of a blob with the following format:
4458 // *(module-name-len:i16 module-name:len*i8
4459 // source-location-offset:i32
4460 // identifier-id:i32
4461 // preprocessed-entity-id:i32
4462 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004463 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004464 // selector-id:i32
4465 // declaration-id:i32
4466 // c++-base-specifiers-id:i32
4467 // type-id:i32)
4468 //
4469 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4470 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4472 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004473 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004474 {
4475 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004476 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004477 using namespace llvm::support;
4478 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004479 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004480 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004481 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004482
Ben Langmuir785180e2014-10-20 16:27:30 +00004483 // Note: if a base ID was uint max, it would not be possible to load
4484 // another module after it or have more than one entity inside it.
4485 uint32_t None = std::numeric_limits<uint32_t>::max();
4486
4487 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4488 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4489 if (ShouldWrite)
4490 LE.write<uint32_t>(BaseID);
4491 else
4492 LE.write<uint32_t>(None);
4493 };
4494
Ben Langmuirfe971d92014-08-16 04:54:18 +00004495 // These values should be unique within a chain, since they will be read
4496 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004497 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4498 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4499 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4500 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4501 M->NumPreprocessedEntities);
4502 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4503 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4504 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4505 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004506 }
4507 }
4508 Record.clear();
4509 Record.push_back(MODULE_OFFSET_MAP);
4510 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4511 Buffer.data(), Buffer.size());
4512 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004513
Richard Smithb9eab6d2014-03-20 19:44:17 +00004514 RecordData DeclUpdatesOffsetsRecord;
4515
Richard Smith59442b42014-03-20 20:07:19 +00004516 // Keep writing types, declarations, and declaration update records
4517 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004518 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4519 WriteTypeAbbrevs();
4520 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004521 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4522 E = DeclsToRewrite.end();
4523 I != E; ++I)
4524 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004525 do {
4526 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4527 while (!DeclTypesToEmit.empty()) {
4528 DeclOrType DOT = DeclTypesToEmit.front();
4529 DeclTypesToEmit.pop();
4530 if (DOT.isType())
4531 WriteType(DOT.getType());
4532 else
4533 WriteDecl(Context, DOT.getDecl());
4534 }
4535 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004536 Stream.ExitBlock();
4537
Richard Smithb9eab6d2014-03-20 19:44:17 +00004538 DoneWritingDeclsAndTypes = true;
4539
4540 // These things can only be done once we've written out decls and types.
4541 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004542 if (!DeclUpdatesOffsetsRecord.empty())
4543 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004544 WriteCXXBaseSpecifiersOffsets();
4545 WriteFileDeclIDsMap();
4546 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4547
4548 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004549 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004550 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004551 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004552 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004553 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004554 WriteFPPragmaOptions(SemaRef.getFPOptions());
4555 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004556 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004557
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004558 // If we're emitting a module, write out the submodule information.
4559 if (WritingModule)
4560 WriteSubmodules(WritingModule);
4561
Douglas Gregor5204bde2011-08-02 16:26:37 +00004562 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4563
Douglas Gregord4df8652009-04-22 22:02:47 +00004564 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004565 if (!EagerlyDeserializedDecls.empty())
4566 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004567
4568 // Write the record containing tentative definitions.
4569 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004570 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004571
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004572 // Write the record containing unused file scoped decls.
4573 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004574 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004575
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004576 // Write the record containing weak undeclared identifiers.
4577 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004578 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004579 WeakUndeclaredIdentifiers);
4580
Richard Smith78165b52013-01-10 23:43:47 +00004581 // Write the record containing locally-scoped extern "C" definitions.
4582 if (!LocallyScopedExternCDecls.empty())
4583 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4584 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004585
4586 // Write the record containing ext_vector type names.
4587 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004588 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004589
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004590 // Write the record containing VTable uses information.
4591 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004592 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004593
4594 // Write the record containing dynamic classes declarations.
4595 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004596 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004597
Nico Weber72889432014-09-06 01:25:55 +00004598 // Write the record containing potentially unused local typedefs.
4599 if (!UnusedLocalTypedefNameCandidates.empty())
4600 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4601 UnusedLocalTypedefNameCandidates);
4602
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004603 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004604 if (!PendingInstantiations.empty())
4605 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004606
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004607 // Write the record containing declaration references of Sema.
4608 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004609 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004610
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004611 // Write the record containing CUDA-specific declaration references.
4612 if (!CUDASpecialDeclRefs.empty())
4613 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004614
4615 // Write the delegating constructors.
4616 if (!DelegatingCtorDecls.empty())
4617 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004618
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004619 // Write the known namespaces.
4620 if (!KnownNamespaces.empty())
4621 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004622
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004623 // Write the undefined internal functions and variables, and inline functions.
4624 if (!UndefinedButUsed.empty())
4625 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004626
Douglas Gregor851443c2011-08-12 01:39:19 +00004627 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004628 for (auto *DC : UpdatedDeclContexts)
4629 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004630
Douglas Gregor959bb062011-12-03 01:15:29 +00004631 if (!WritingModule) {
4632 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004633 struct ModuleInfo {
4634 uint64_t ID;
4635 Module *M;
4636 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4637 };
Richard Smith56be7542014-03-21 00:33:59 +00004638 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004639 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004640 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004641 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4642 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004643 }
Richard Smith56be7542014-03-21 00:33:59 +00004644
4645 if (!Imports.empty()) {
4646 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4647 return A.ID < B.ID;
4648 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004649 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4650 return A.ID == B.ID;
4651 };
Richard Smith56be7542014-03-21 00:33:59 +00004652
4653 // Sort and deduplicate module IDs.
4654 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004655 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004656 Imports.end());
4657
4658 RecordData ImportedModules;
4659 for (const auto &Import : Imports) {
4660 ImportedModules.push_back(Import.ID);
4661 // FIXME: If the module has macros imported then later has declarations
4662 // imported, this location won't be the right one as a location for the
4663 // declaration imports.
4664 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4665 }
4666
Douglas Gregor959bb062011-12-03 01:15:29 +00004667 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4668 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004669 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004670
Douglas Gregor851443c2011-08-12 01:39:19 +00004671 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004672 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004673 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004674 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004675 WriteLateParsedTemplates(SemaRef);
Dario Domizioli13a0a382014-05-23 12:13:25 +00004676 if(!WritingModule)
4677 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004678
Douglas Gregor08f01292009-04-17 22:13:46 +00004679 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004680 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004681 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004682 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004683 Record.push_back(NumLexicalDeclContexts);
4684 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004685 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004686 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004687}
4688
Richard Smithb9eab6d2014-03-20 19:44:17 +00004689void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004690 if (DeclUpdates.empty())
4691 return;
4692
Richard Smith59442b42014-03-20 20:07:19 +00004693 DeclUpdateMap LocalUpdates;
4694 LocalUpdates.swap(DeclUpdates);
4695
Richard Smith6ef42932014-03-20 21:02:00 +00004696 for (auto &DeclUpdate : LocalUpdates) {
4697 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004698 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004699 continue; // The decl will be written completely,no need to store updates.
4700
Richard Smithd28ac5b2014-03-22 23:33:22 +00004701 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004702 RecordData Record;
4703 for (auto &Update : DeclUpdate.second) {
4704 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4705
4706 Record.push_back(Kind);
4707 switch (Kind) {
4708 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4709 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4710 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004711 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004712 Record.push_back(GetDeclRef(Update.getDecl()));
4713 break;
4714
Richard Smith4d235792014-08-07 18:53:08 +00004715 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004716 // An updated body is emitted last, so that the reader doesn't need
4717 // to skip over the lazy body to reach statements for other records.
4718 Record.pop_back();
4719 HasUpdatedBody = true;
4720 break;
4721
Richard Smith4d235792014-08-07 18:53:08 +00004722 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4723 AddSourceLocation(Update.getLoc(), Record);
4724 break;
4725
Richard Smithcd45dbc2014-04-19 03:48:30 +00004726 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4727 auto *RD = cast<CXXRecordDecl>(D);
4728 AddUpdatedDeclContext(RD->getPrimaryContext());
4729 AddCXXDefinitionData(RD, Record);
4730 Record.push_back(WriteDeclContextLexicalBlock(
4731 *Context, const_cast<CXXRecordDecl *>(RD)));
4732
4733 // This state is sometimes updated by template instantiation, when we
4734 // switch from the specialization referring to the template declaration
4735 // to it referring to the template definition.
4736 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4737 Record.push_back(MSInfo->getTemplateSpecializationKind());
4738 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4739 } else {
4740 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4741 Record.push_back(Spec->getTemplateSpecializationKind());
4742 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004743
4744 // The instantiation might have been resolved to a partial
4745 // specialization. If so, record which one.
4746 auto From = Spec->getInstantiatedFrom();
4747 if (auto PartialSpec =
4748 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4749 Record.push_back(true);
4750 AddDeclRef(PartialSpec, Record);
4751 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4752 Record);
4753 } else {
4754 Record.push_back(false);
4755 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004756 }
4757 Record.push_back(RD->getTagKind());
4758 AddSourceLocation(RD->getLocation(), Record);
4759 AddSourceLocation(RD->getLocStart(), Record);
4760 AddSourceLocation(RD->getRBraceLoc(), Record);
4761
4762 // Instantiation may change attributes; write them all out afresh.
4763 Record.push_back(D->hasAttrs());
4764 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004765 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4766 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004767
4768 // FIXME: Ensure we don't get here for explicit instantiations.
4769 break;
4770 }
4771
Richard Smith564417a2014-03-20 21:47:22 +00004772 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4773 addExceptionSpec(
4774 *this,
4775 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4776 Record);
4777 break;
4778
Richard Smith6ef42932014-03-20 21:02:00 +00004779 case UPD_CXX_DEDUCED_RETURN_TYPE:
4780 Record.push_back(GetOrCreateTypeID(Update.getType()));
4781 break;
4782
4783 case UPD_DECL_MARKED_USED:
4784 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004785
4786 case UPD_MANGLING_NUMBER:
4787 case UPD_STATIC_LOCAL_NUMBER:
4788 Record.push_back(Update.getNumber());
4789 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004790 }
4791 }
4792
Richard Smithd28ac5b2014-03-22 23:33:22 +00004793 if (HasUpdatedBody) {
4794 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004795 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004796 Record.push_back(Def->isInlined());
4797 AddSourceLocation(Def->getInnerLocStart(), Record);
4798 AddFunctionDefinition(Def, Record);
Richard Smith4d235792014-08-07 18:53:08 +00004799 if (auto *DD = dyn_cast<CXXDestructorDecl>(Def))
4800 Record.push_back(GetDeclRef(DD->getOperatorDelete()));
Richard Smithd28ac5b2014-03-22 23:33:22 +00004801 }
4802
Richard Smithcd45dbc2014-04-19 03:48:30 +00004803 OffsetsRecord.push_back(GetDeclRef(D));
4804 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4805
Richard Smith6ef42932014-03-20 21:02:00 +00004806 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004807
Richard Smithb9eab6d2014-03-20 19:44:17 +00004808 // Flush any statements that were written as part of this update record.
4809 FlushStmts();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004810
4811 // Flush C++ base specifiers, if there are any.
4812 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004813 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004814}
4815
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004816void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004817 if (ReplacedDecls.empty())
4818 return;
4819
4820 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004821 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4822 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004823 Record.push_back(I->ID);
4824 Record.push_back(I->Offset);
4825 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004826 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004827 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004828}
4829
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004830void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004831 Record.push_back(Loc.getRawEncoding());
4832}
4833
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004834void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004835 AddSourceLocation(Range.getBegin(), Record);
4836 AddSourceLocation(Range.getEnd(), Record);
4837}
4838
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004839void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004840 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004841 const uint64_t *Words = Value.getRawData();
4842 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004843}
4844
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004845void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004846 Record.push_back(Value.isUnsigned());
4847 AddAPInt(Value, Record);
4848}
4849
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004850void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004851 AddAPInt(Value.bitcastToAPInt(), Record);
4852}
4853
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004854void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004855 Record.push_back(getIdentifierRef(II));
4856}
4857
Sebastian Redl539c5062010-08-18 23:57:32 +00004858IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004859 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004860 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004861
Sebastian Redl539c5062010-08-18 23:57:32 +00004862 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004863 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004864 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004865 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004866}
4867
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004868MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004869 // Don't emit builtin macros like __LINE__ to the AST file unless they
4870 // have been redefined by the header (in which case they are not
4871 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004872 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004873 return 0;
4874
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004875 MacroID &ID = MacroIDs[MI];
4876 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004877 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004878 MacroInfoToEmitData Info = { Name, MI, ID };
4879 MacroInfosToEmit.push_back(Info);
4880 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004881 return ID;
4882}
4883
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004884MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004885 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004886 return 0;
4887
4888 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4889 return MacroIDs[MI];
4890}
4891
4892uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4893 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4894 return IdentMacroDirectivesOffsetMap[Name];
4895}
4896
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004897void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004898 Record.push_back(getSelectorRef(SelRef));
4899}
4900
Sebastian Redl539c5062010-08-18 23:57:32 +00004901SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004902 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004903 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004904 }
4905
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004906 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004907 if (SID == 0 && Chain) {
4908 // This might trigger a ReadSelector callback, which will set the ID for
4909 // this selector.
4910 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004911 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004912 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004913 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004914 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004915 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004916 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004917 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004918}
4919
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004920void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004921 AddDeclRef(Temp->getDestructor(), Record);
4922}
4923
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004924void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4925 CXXBaseSpecifier const *BasesEnd,
4926 RecordDataImpl &Record) {
4927 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4928 CXXBaseSpecifiersToWrite.push_back(
4929 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4930 Bases, BasesEnd));
4931 Record.push_back(NextCXXBaseSpecifiersID++);
4932}
4933
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004934void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004935 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004936 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004937 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004938 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004939 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004940 break;
4941 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004942 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004943 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004944 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004945 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004946 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004947 break;
4948 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004949 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004950 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004951 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004952 break;
John McCall0ad16662009-10-29 08:12:44 +00004953 case TemplateArgument::Null:
4954 case TemplateArgument::Integral:
4955 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004956 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004957 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004958 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004959 break;
4960 }
4961}
4962
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004963void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004964 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004965 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004966
4967 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4968 bool InfoHasSameExpr
4969 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4970 Record.push_back(InfoHasSameExpr);
4971 if (InfoHasSameExpr)
4972 return; // Avoid storing the same expr twice.
4973 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004974 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4975 Record);
4976}
4977
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004978void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4979 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004980 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004981 AddTypeRef(QualType(), Record);
4982 return;
4983 }
4984
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004985 AddTypeLoc(TInfo->getTypeLoc(), Record);
4986}
4987
4988void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4989 AddTypeRef(TL.getType(), Record);
4990
John McCall8f115c62009-10-16 21:56:05 +00004991 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004992 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004993 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004994}
4995
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004996void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004997 Record.push_back(GetOrCreateTypeID(T));
4998}
4999
Douglas Gregoreda8e122011-08-09 15:13:55 +00005000TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005001 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00005002 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005003 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
5004}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005005
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005006TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005007 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00005008 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005009 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005010}
5011
5012TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
5013 if (T.isNull())
5014 return TypeIdx();
5015 assert(!T.getLocalFastQualifiers());
5016
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00005017 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005018 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005019 if (DoneWritingDeclsAndTypes) {
5020 assert(0 && "New type seen after serializing all the types to emit!");
5021 return TypeIdx();
5022 }
5023
Douglas Gregor1970d882009-04-26 03:49:13 +00005024 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00005025 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005026 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00005027 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00005028 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005029 return Idx;
5030}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005031
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005032TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005033 if (T.isNull())
5034 return TypeIdx();
5035 assert(!T.getLocalFastQualifiers());
5036
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005037 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5038 assert(I != TypeIdxs.end() && "Type not emitted!");
5039 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005040}
5041
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005042void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005043 Record.push_back(GetDeclRef(D));
5044}
5045
Sebastian Redl539c5062010-08-18 23:57:32 +00005046DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005047 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005048
5049 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005050 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005051 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005052
5053 // If D comes from an AST file, its declaration ID is already known and
5054 // fixed.
5055 if (D->isFromASTFile())
5056 return D->getGlobalID();
5057
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005058 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005059 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005060 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005061 if (DoneWritingDeclsAndTypes) {
5062 assert(0 && "New decl seen after serializing all the decls to emit!");
5063 return 0;
5064 }
5065
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005066 // We haven't seen this declaration before. Give it a new ID and
5067 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005068 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005069 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005070 }
5071
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005072 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005073}
5074
Sebastian Redl539c5062010-08-18 23:57:32 +00005075DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005076 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005077 return 0;
5078
Douglas Gregorb3163e52012-01-05 22:33:30 +00005079 // If D comes from an AST file, its declaration ID is already known and
5080 // fixed.
5081 if (D->isFromASTFile())
5082 return D->getGlobalID();
5083
Douglas Gregore84a9da2009-04-20 20:36:09 +00005084 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5085 return DeclIDs[D];
5086}
5087
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005088void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005089 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005090 assert(D);
5091
5092 SourceLocation Loc = D->getLocation();
5093 if (Loc.isInvalid())
5094 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005095
5096 // We only keep track of the file-level declarations of each file.
5097 if (!D->getLexicalDeclContext()->isFileContext())
5098 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005099 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5100 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005101 if (isa<ParmVarDecl>(D))
5102 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005103
5104 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005105 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005106 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005107 FileID FID;
5108 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005109 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005110 if (FID.isInvalid())
5111 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005112 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005113
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005114 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005115 if (!Info)
5116 Info = new DeclIDInFileInfo();
5117
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005118 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005119 LocDeclIDsTy &Decls = Info->DeclIDs;
5120
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005121 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005122 Decls.push_back(LocDecl);
5123 return;
5124 }
5125
Benjamin Kramer45025c02013-08-24 13:22:59 +00005126 LocDeclIDsTy::iterator I =
5127 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005128
5129 Decls.insert(I, LocDecl);
5130}
5131
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005132void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005133 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005134 Record.push_back(Name.getNameKind());
5135 switch (Name.getNameKind()) {
5136 case DeclarationName::Identifier:
5137 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5138 break;
5139
5140 case DeclarationName::ObjCZeroArgSelector:
5141 case DeclarationName::ObjCOneArgSelector:
5142 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005143 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005144 break;
5145
5146 case DeclarationName::CXXConstructorName:
5147 case DeclarationName::CXXDestructorName:
5148 case DeclarationName::CXXConversionFunctionName:
5149 AddTypeRef(Name.getCXXNameType(), Record);
5150 break;
5151
5152 case DeclarationName::CXXOperatorName:
5153 Record.push_back(Name.getCXXOverloadedOperator());
5154 break;
5155
Alexis Hunt3d221f22009-11-29 07:34:05 +00005156 case DeclarationName::CXXLiteralOperatorName:
5157 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5158 break;
5159
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005160 case DeclarationName::CXXUsingDirective:
5161 // No extra data to emit
5162 break;
5163 }
5164}
Chris Lattnerca025db2010-05-07 21:43:38 +00005165
Richard Smithd08aeb62014-08-28 01:33:39 +00005166unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5167 assert(needsAnonymousDeclarationNumber(D) &&
5168 "expected an anonymous declaration");
5169
5170 // Number the anonymous declarations within this context, if we've not
5171 // already done so.
5172 auto It = AnonymousDeclarationNumbers.find(D);
5173 if (It == AnonymousDeclarationNumbers.end()) {
5174 unsigned Index = 0;
5175 for (Decl *LexicalD : D->getLexicalDeclContext()->decls()) {
5176 auto *ND = dyn_cast<NamedDecl>(LexicalD);
5177 if (!ND || !needsAnonymousDeclarationNumber(ND))
5178 continue;
5179 AnonymousDeclarationNumbers[ND] = Index++;
5180 }
5181
5182 It = AnonymousDeclarationNumbers.find(D);
5183 assert(It != AnonymousDeclarationNumbers.end() &&
5184 "declaration not found within its lexical context");
5185 }
5186
5187 return It->second;
5188}
5189
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005190void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005191 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005192 switch (Name.getNameKind()) {
5193 case DeclarationName::CXXConstructorName:
5194 case DeclarationName::CXXDestructorName:
5195 case DeclarationName::CXXConversionFunctionName:
5196 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5197 break;
5198
5199 case DeclarationName::CXXOperatorName:
5200 AddSourceLocation(
5201 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5202 Record);
5203 AddSourceLocation(
5204 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5205 Record);
5206 break;
5207
5208 case DeclarationName::CXXLiteralOperatorName:
5209 AddSourceLocation(
5210 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5211 Record);
5212 break;
5213
5214 case DeclarationName::Identifier:
5215 case DeclarationName::ObjCZeroArgSelector:
5216 case DeclarationName::ObjCOneArgSelector:
5217 case DeclarationName::ObjCMultiArgSelector:
5218 case DeclarationName::CXXUsingDirective:
5219 break;
5220 }
5221}
5222
5223void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005224 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005225 AddDeclarationName(NameInfo.getName(), Record);
5226 AddSourceLocation(NameInfo.getLoc(), Record);
5227 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5228}
5229
5230void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005231 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005232 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005233 Record.push_back(Info.NumTemplParamLists);
5234 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5235 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5236}
5237
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005238void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005239 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005240 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005241 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005242 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005243
5244 // Push each of the NNS's onto a stack for serialization in reverse order.
5245 while (NNS) {
5246 NestedNames.push_back(NNS);
5247 NNS = NNS->getPrefix();
5248 }
5249
5250 Record.push_back(NestedNames.size());
5251 while(!NestedNames.empty()) {
5252 NNS = NestedNames.pop_back_val();
5253 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5254 Record.push_back(Kind);
5255 switch (Kind) {
5256 case NestedNameSpecifier::Identifier:
5257 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5258 break;
5259
5260 case NestedNameSpecifier::Namespace:
5261 AddDeclRef(NNS->getAsNamespace(), Record);
5262 break;
5263
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005264 case NestedNameSpecifier::NamespaceAlias:
5265 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5266 break;
5267
Chris Lattnerca025db2010-05-07 21:43:38 +00005268 case NestedNameSpecifier::TypeSpec:
5269 case NestedNameSpecifier::TypeSpecWithTemplate:
5270 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5271 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5272 break;
5273
5274 case NestedNameSpecifier::Global:
5275 // Don't need to write an associated value.
5276 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005277
5278 case NestedNameSpecifier::Super:
5279 AddDeclRef(NNS->getAsRecordDecl(), Record);
5280 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005281 }
5282 }
5283}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005284
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005285void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5286 RecordDataImpl &Record) {
5287 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005288 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005289 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005290
5291 // Push each of the nested-name-specifiers's onto a stack for
5292 // serialization in reverse order.
5293 while (NNS) {
5294 NestedNames.push_back(NNS);
5295 NNS = NNS.getPrefix();
5296 }
5297
5298 Record.push_back(NestedNames.size());
5299 while(!NestedNames.empty()) {
5300 NNS = NestedNames.pop_back_val();
5301 NestedNameSpecifier::SpecifierKind Kind
5302 = NNS.getNestedNameSpecifier()->getKind();
5303 Record.push_back(Kind);
5304 switch (Kind) {
5305 case NestedNameSpecifier::Identifier:
5306 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5307 AddSourceRange(NNS.getLocalSourceRange(), Record);
5308 break;
5309
5310 case NestedNameSpecifier::Namespace:
5311 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5312 AddSourceRange(NNS.getLocalSourceRange(), Record);
5313 break;
5314
5315 case NestedNameSpecifier::NamespaceAlias:
5316 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5317 AddSourceRange(NNS.getLocalSourceRange(), Record);
5318 break;
5319
5320 case NestedNameSpecifier::TypeSpec:
5321 case NestedNameSpecifier::TypeSpecWithTemplate:
5322 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5323 AddTypeLoc(NNS.getTypeLoc(), Record);
5324 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5325 break;
5326
5327 case NestedNameSpecifier::Global:
5328 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5329 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005330
5331 case NestedNameSpecifier::Super:
5332 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5333 AddSourceRange(NNS.getLocalSourceRange(), Record);
5334 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005335 }
5336 }
5337}
5338
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005339void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005340 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005341 Record.push_back(Kind);
5342 switch (Kind) {
5343 case TemplateName::Template:
5344 AddDeclRef(Name.getAsTemplateDecl(), Record);
5345 break;
5346
5347 case TemplateName::OverloadedTemplate: {
5348 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5349 Record.push_back(OvT->size());
5350 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5351 I != E; ++I)
5352 AddDeclRef(*I, Record);
5353 break;
5354 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005355
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005356 case TemplateName::QualifiedTemplate: {
5357 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5358 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5359 Record.push_back(QualT->hasTemplateKeyword());
5360 AddDeclRef(QualT->getTemplateDecl(), Record);
5361 break;
5362 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005363
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005364 case TemplateName::DependentTemplate: {
5365 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5366 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5367 Record.push_back(DepT->isIdentifier());
5368 if (DepT->isIdentifier())
5369 AddIdentifierRef(DepT->getIdentifier(), Record);
5370 else
5371 Record.push_back(DepT->getOperator());
5372 break;
5373 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005374
5375 case TemplateName::SubstTemplateTemplateParm: {
5376 SubstTemplateTemplateParmStorage *subst
5377 = Name.getAsSubstTemplateTemplateParm();
5378 AddDeclRef(subst->getParameter(), Record);
5379 AddTemplateName(subst->getReplacement(), Record);
5380 break;
5381 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005382
5383 case TemplateName::SubstTemplateTemplateParmPack: {
5384 SubstTemplateTemplateParmPackStorage *SubstPack
5385 = Name.getAsSubstTemplateTemplateParmPack();
5386 AddDeclRef(SubstPack->getParameterPack(), Record);
5387 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5388 break;
5389 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005390 }
5391}
5392
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005393void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005394 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005395 Record.push_back(Arg.getKind());
5396 switch (Arg.getKind()) {
5397 case TemplateArgument::Null:
5398 break;
5399 case TemplateArgument::Type:
5400 AddTypeRef(Arg.getAsType(), Record);
5401 break;
5402 case TemplateArgument::Declaration:
5403 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005404 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005405 break;
5406 case TemplateArgument::NullPtr:
5407 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005408 break;
5409 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005410 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005411 AddTypeRef(Arg.getIntegralType(), Record);
5412 break;
5413 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005414 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5415 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005416 case TemplateArgument::TemplateExpansion:
5417 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005418 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005419 Record.push_back(*NumExpansions + 1);
5420 else
5421 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005422 break;
5423 case TemplateArgument::Expression:
5424 AddStmt(Arg.getAsExpr());
5425 break;
5426 case TemplateArgument::Pack:
5427 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005428 for (const auto &P : Arg.pack_elements())
5429 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005430 break;
5431 }
5432}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005433
5434void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005435ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005436 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005437 assert(TemplateParams && "No TemplateParams!");
5438 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5439 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5440 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5441 Record.push_back(TemplateParams->size());
5442 for (TemplateParameterList::const_iterator
5443 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5444 P != PEnd; ++P)
5445 AddDeclRef(*P, Record);
5446}
5447
5448/// \brief Emit a template argument list.
5449void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005450ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005451 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005452 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005453 Record.push_back(TemplateArgs->size());
5454 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005455 AddTemplateArgument(TemplateArgs->get(i), Record);
5456}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005457
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005458void
5459ASTWriter::AddASTTemplateArgumentListInfo
5460(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5461 assert(ASTTemplArgList && "No ASTTemplArgList!");
5462 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5463 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5464 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5465 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5466 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5467 AddTemplateArgumentLoc(TemplArgs[i], Record);
5468}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005469
5470void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005471ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005472 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005473 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005474 I = Set.begin(), E = Set.end(); I != E; ++I) {
5475 AddDeclRef(I.getDecl(), Record);
5476 Record.push_back(I.getAccess());
5477 }
5478}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005479
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005480void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005481 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005482 Record.push_back(Base.isVirtual());
5483 Record.push_back(Base.isBaseOfClass());
5484 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005485 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005486 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005487 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005488 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5489 : SourceLocation(),
5490 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005491}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005492
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005493void ASTWriter::FlushCXXBaseSpecifiers() {
5494 RecordData Record;
5495 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5496 Record.clear();
5497
5498 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005499 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005500 if (Index == CXXBaseSpecifiersOffsets.size())
5501 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5502 else {
5503 if (Index > CXXBaseSpecifiersOffsets.size())
5504 CXXBaseSpecifiersOffsets.resize(Index + 1);
5505 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5506 }
5507
5508 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5509 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5510 Record.push_back(BEnd - B);
5511 for (; B != BEnd; ++B)
5512 AddCXXBaseSpecifier(*B, Record);
5513 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005514
5515 // Flush any expressions that were written as part of the base specifiers.
5516 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005517 }
5518
5519 CXXBaseSpecifiersToWrite.clear();
5520}
5521
Alexis Hunt1d792652011-01-08 20:30:50 +00005522void ASTWriter::AddCXXCtorInitializers(
5523 const CXXCtorInitializer * const *CtorInitializers,
5524 unsigned NumCtorInitializers,
5525 RecordDataImpl &Record) {
5526 Record.push_back(NumCtorInitializers);
5527 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5528 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005529
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005530 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005531 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005532 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005533 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005534 } else if (Init->isDelegatingInitializer()) {
5535 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005536 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005537 } else if (Init->isMemberInitializer()){
5538 Record.push_back(CTOR_INITIALIZER_MEMBER);
5539 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005540 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005541 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5542 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005543 }
Francois Pichetd583da02010-12-04 09:14:42 +00005544
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005545 AddSourceLocation(Init->getMemberLocation(), Record);
5546 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005547 AddSourceLocation(Init->getLParenLoc(), Record);
5548 AddSourceLocation(Init->getRParenLoc(), Record);
5549 Record.push_back(Init->isWritten());
5550 if (Init->isWritten()) {
5551 Record.push_back(Init->getSourceOrder());
5552 } else {
5553 Record.push_back(Init->getNumArrayIndices());
5554 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5555 AddDeclRef(Init->getArrayIndex(i), Record);
5556 }
5557 }
5558}
5559
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005560void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005561 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005562 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005563 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005564 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005565 Record.push_back(Data.Aggregate);
5566 Record.push_back(Data.PlainOldData);
5567 Record.push_back(Data.Empty);
5568 Record.push_back(Data.Polymorphic);
5569 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005570 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005571 Record.push_back(Data.HasNoNonEmptyBases);
5572 Record.push_back(Data.HasPrivateFields);
5573 Record.push_back(Data.HasProtectedFields);
5574 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005575 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005576 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005577 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005578 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005579 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005580 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5581 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5582 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5583 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5584 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5585 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005586 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005587 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005588 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005589 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005590 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005591 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005592 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005593 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005594 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005595 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005596 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5597 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5598 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5599 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005600 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005601
5602 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005603 if (Data.NumBases > 0)
5604 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5605 Record);
5606
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005607 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5608 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005609 if (Data.NumVBases > 0)
5610 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5611 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005612
Richard Smitha4ba74c2013-08-30 04:46:40 +00005613 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5614 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005615 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005616 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005617
5618 // Add lambda-specific data.
5619 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005620 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005621 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005622 Record.push_back(Lambda.IsGenericLambda);
5623 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005624 Record.push_back(Lambda.NumCaptures);
5625 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005626 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005627 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005628 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005629 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005630 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005631 AddSourceLocation(Capture.getLocation(), Record);
5632 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005633 Record.push_back(Capture.getCaptureKind());
5634 switch (Capture.getCaptureKind()) {
5635 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005636 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005637 break;
5638 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005639 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005640 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005641 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005642 AddDeclRef(Var, Record);
5643 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5644 : SourceLocation(),
5645 Record);
5646 break;
5647 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005648 }
5649 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005650}
5651
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005652void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005653 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005654 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005655 assert(FirstDeclID == NextDeclID &&
5656 FirstTypeID == NextTypeID &&
5657 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005658 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005659 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005660 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005661 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005662
Sebastian Redl07a89a82010-07-30 00:29:29 +00005663 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005664
Douglas Gregordf0c1512011-08-18 04:12:04 +00005665 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5666 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5667 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005668 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005669 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005670 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005671 NextDeclID = FirstDeclID;
5672 NextTypeID = FirstTypeID;
5673 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005674 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005675 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005676 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005677}
5678
Sebastian Redl539c5062010-08-18 23:57:32 +00005679void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005680 // Always keep the highest ID. See \p TypeRead() for more information.
5681 IdentID &StoredID = IdentifierIDs[II];
5682 if (ID > StoredID)
5683 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005684}
5685
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005686void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005687 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005688 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005689 if (ID > StoredID)
5690 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005691}
5692
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005693void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005694 // Always take the highest-numbered type index. This copes with an interesting
5695 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005696 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005697 // keep the higher-numbered entry so that we can properly write it out to
5698 // the AST file.
5699 TypeIdx &StoredIdx = TypeIdxs[T];
5700 if (Idx.getIndex() >= StoredIdx.getIndex())
5701 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005702}
5703
Sebastian Redl539c5062010-08-18 23:57:32 +00005704void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005705 // Always keep the highest ID. See \p TypeRead() for more information.
5706 SelectorID &StoredID = SelectorIDs[S];
5707 if (ID > StoredID)
5708 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005709}
Douglas Gregor91096292010-10-02 19:29:26 +00005710
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005711void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005712 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005713 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005714 MacroDefinitions[MD] = ID;
5715}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005716
Douglas Gregore37a85a2011-12-02 17:30:13 +00005717void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5718 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5719 SubmoduleIDs[Mod] = ID;
5720}
5721
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005722void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005723 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005724 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005725 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5726 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005727 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005728 // A forward reference was mutated into a definition. Rewrite it.
5729 // FIXME: This happens during template instantiation, should we
5730 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005731 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5732 "completed a tag from another module but not by instantiation?");
5733 DeclUpdates[RD].push_back(
5734 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005735 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005736 }
5737}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005738
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005739void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5740 // TU and namespaces are handled elsewhere.
5741 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5742 return;
5743
Douglas Gregorb3722e22011-09-09 23:01:35 +00005744 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005745 return; // Not a source decl added to a DeclContext from PCH.
5746
Douglas Gregor9f782892013-01-21 15:25:38 +00005747 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005748 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005749 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005750 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005751}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005752
5753void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5754 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005755 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005756 return; // Not a source member added to a class from PCH.
5757 if (!isa<CXXMethodDecl>(D))
5758 return; // We are interested in lazily declared implicit methods.
5759
5760 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005761 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005762 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005763 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005764}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005765
5766void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5767 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005768 // The specializations set is kept in the canonical template.
5769 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005770 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005771 return; // Not a source specialization added to a template from PCH.
5772
Richard Smithe9a8bc32014-09-30 00:45:29 +00005773 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005774 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5775 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005776}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005777
Larisse Voufo39a1e502013-08-06 01:03:05 +00005778void ASTWriter::AddedCXXTemplateSpecialization(
5779 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5780 // The specializations set is kept in the canonical template.
Larisse Voufo39a1e502013-08-06 01:03:05 +00005781 TD = TD->getCanonicalDecl();
5782 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5783 return; // Not a source specialization added to a template from PCH.
5784
Richard Smithe9a8bc32014-09-30 00:45:29 +00005785 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005786 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5787 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005788}
5789
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005790void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5791 const FunctionDecl *D) {
5792 // The specializations set is kept in the canonical template.
5793 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005794 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005795 return; // Not a source specialization added to a template from PCH.
5796
Richard Smithe9a8bc32014-09-30 00:45:29 +00005797 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005798 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5799 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005800}
5801
Richard Smith564417a2014-03-20 21:47:22 +00005802void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5803 assert(!WritingAST && "Already writing the AST!");
5804 FD = FD->getCanonicalDecl();
5805 if (!FD->isFromASTFile())
5806 return; // Not a function declared in PCH and defined outside.
5807
5808 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5809}
5810
Richard Smith1fa5d642013-05-11 05:45:24 +00005811void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5812 assert(!WritingAST && "Already writing the AST!");
5813 FD = FD->getCanonicalDecl();
5814 if (!FD->isFromASTFile())
5815 return; // Not a function declared in PCH and defined outside.
5816
Aaron Ballman4f45b712014-03-21 15:22:56 +00005817 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith1fa5d642013-05-11 05:45:24 +00005818}
5819
Sebastian Redlab238a72011-04-24 16:28:06 +00005820void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005821 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005822 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005823 return; // Declaration not imported from PCH.
5824
Richard Smith4d235792014-08-07 18:53:08 +00005825 // Implicit function decl from a PCH was defined.
5826 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005827}
5828
Richard Smithd28ac5b2014-03-22 23:33:22 +00005829void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5830 assert(!WritingAST && "Already writing the AST!");
5831 if (!D->isFromASTFile())
5832 return;
5833
Richard Smithd28ac5b2014-03-22 23:33:22 +00005834 DeclUpdates[D].push_back(
Richard Smith4d235792014-08-07 18:53:08 +00005835 DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005836}
5837
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005838void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005839 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005840 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005841 return;
5842
5843 // Since the actual instantiation is delayed, this really means that we need
5844 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005845 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005846 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5847 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005848}
5849
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005850void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5851 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005852 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005853 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005854 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005855
5856 assert(IFD->getDefinition() && "Category on a class without a definition?");
5857 ObjCClassesWithCategories.insert(
5858 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005859}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005860
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005861
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005862void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5863 const ObjCPropertyDecl *OrigProp,
5864 const ObjCCategoryDecl *ClassExt) {
5865 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5866 if (!D)
5867 return;
5868
5869 assert(!WritingAST && "Already writing the AST!");
5870 if (!D->isFromASTFile())
5871 return; // Declaration not imported from PCH.
5872
5873 RewriteDecl(D);
5874}
Eli Friedman276dd182013-09-05 00:02:25 +00005875
5876void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5877 assert(!WritingAST && "Already writing the AST!");
5878 if (!D->isFromASTFile())
5879 return;
5880
Aaron Ballman4f45b712014-03-21 15:22:56 +00005881 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005882}