blob: 02663ad43d43dc5bc88e2ee21ee2a8e7ef845a15 [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
Richard Smith54cc3c22014-12-11 20:50:24 +00001061/// \brief Prepares a path for being written to an AST file by converting it
1062/// to an absolute path and removing nested './'s.
1063///
1064/// \return \c true if the path was changed.
1065bool cleanPathForOutput(FileManager &FileMgr, SmallVectorImpl<char> &Path) {
1066 bool Changed = false;
1067
1068 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
1069 llvm::sys::fs::make_absolute(Path);
1070 Changed = true;
1071 }
1072
1073 return Changed | FileMgr.removeDotPaths(Path);
1074}
1075
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001076/// \brief Adjusts the given filename to only write out the portion of the
1077/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001078///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001079/// \param Filename the file name to adjust.
1080///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001081/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1082/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001083///
1084/// \returns either the original filename (if it needs no adjustment) or the
1085/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001086static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001087adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001088 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001089
Richard Smith7ed1bc92014-12-05 22:42:13 +00001090 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001091 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001092
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001093 // Verify that the filename and the system root have the same prefix.
1094 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001095 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1096 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001097 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001098
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001099 // We hit the end of the filename before we hit the end of the system root.
1100 if (!Filename[Pos])
1101 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001102
Richard Smith7ed1bc92014-12-05 22:42:13 +00001103 // If there's not a path separator at the end of the base directory nor
1104 // immediately after it, then this isn't within the base directory.
1105 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1106 if (!llvm::sys::path::is_separator(BaseDir.back()))
1107 return Filename;
1108 } else {
1109 // If the file name has a '/' at the current position, skip over the '/'.
1110 // We distinguish relative paths from absolute paths by the
1111 // absence of '/' at the beginning of relative paths.
1112 //
1113 // FIXME: This is wrong. We distinguish them by asking if the path is
1114 // absolute, which isn't the same thing. And there might be multiple '/'s
1115 // in a row. Use a better mechanism to indicate whether we have emitted an
1116 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001117 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001118 }
Mike Stump11289f42009-09-09 15:08:12 +00001119
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001120 return Filename + Pos;
1121}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001122
Ben Langmuir487ea142014-10-23 18:05:36 +00001123static ASTFileSignature getSignature() {
1124 while (1) {
1125 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1126 return S;
1127 // Rely on GetRandomNumber to eventually return non-zero...
1128 }
1129}
1130
Douglas Gregor112b9072012-10-18 05:31:06 +00001131/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001132void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1133 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001134 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001135 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001136 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1137 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001138
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001139 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001140 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1141 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1142 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1143 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1144 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1145 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1146 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1147 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1148 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1149 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1150 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001151 Record.push_back(VERSION_MAJOR);
1152 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001153 Record.push_back(CLANG_VERSION_MAJOR);
1154 Record.push_back(CLANG_VERSION_MINOR);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001155 assert((!WritingModule || isysroot.empty()) &&
1156 "writing module as a relocatable PCH?");
Douglas Gregorc567ba22011-07-22 16:35:34 +00001157 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001158 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001159 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1160 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001161
Ben Langmuir487ea142014-10-23 18:05:36 +00001162 // Signature
1163 Record.clear();
1164 Record.push_back(getSignature());
1165 Stream.EmitRecord(SIGNATURE, Record);
1166
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001167 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001168 // Module name
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001169 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1170 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1172 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1173 RecordData Record;
1174 Record.push_back(MODULE_NAME);
1175 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1176 }
1177
Richard Smith7ed1bc92014-12-05 22:42:13 +00001178 if (WritingModule && WritingModule->Directory) {
1179 // Module directory.
1180 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1181 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1183 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1184 RecordData Record;
1185 Record.push_back(MODULE_DIRECTORY);
1186
1187 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001188 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001189 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1190
1191 // Write out all other paths relative to the base directory if possible.
1192 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1193 } else if (!isysroot.empty()) {
1194 // Write out paths relative to the sysroot if possible.
1195 BaseDirectory = isysroot;
1196 }
1197
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001198 // Module map file
1199 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001200 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001201
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001202 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1203
1204 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001205 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001206
1207 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001208 if (auto *AdditionalModMaps =
1209 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001210 Record.push_back(AdditionalModMaps->size());
1211 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001212 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001213 } else {
1214 Record.push_back(0);
1215 }
1216
1217 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001218 }
1219
Douglas Gregor112b9072012-10-18 05:31:06 +00001220 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001221 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001222 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001223 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001224
1225 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1226 M != MEnd; ++M) {
1227 // Skip modules that weren't directly imported.
1228 if (!(*M)->isDirectlyImported())
1229 continue;
1230
1231 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001232 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001233 Record.push_back((*M)->File->getSize());
1234 Record.push_back((*M)->File->getModificationTime());
Ben Langmuir487ea142014-10-23 18:05:36 +00001235 Record.push_back((*M)->Signature);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001236 AddPath((*M)->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001237 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001238 Stream.EmitRecord(IMPORTS, Record);
1239 }
Mike Stump11289f42009-09-09 15:08:12 +00001240
Douglas Gregor112b9072012-10-18 05:31:06 +00001241 // Language options.
1242 Record.clear();
1243 const LangOptions &LangOpts = Context.getLangOpts();
1244#define LANGOPT(Name, Bits, Default, Description) \
1245 Record.push_back(LangOpts.Name);
1246#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1247 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001248#include "clang/Basic/LangOptions.def"
1249#define SANITIZER(NAME, ID) \
1250 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001251#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001252
1253 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1254 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1255
1256 Record.push_back(LangOpts.CurrentModule.size());
1257 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001258
1259 // Comment options.
1260 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1261 for (CommentOptions::BlockCommandNamesTy::const_iterator
1262 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1263 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1264 I != IEnd; ++I) {
1265 AddString(*I, Record);
1266 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001267 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001268
Douglas Gregor112b9072012-10-18 05:31:06 +00001269 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1270
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001271 // Target options.
1272 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001273 const TargetInfo &Target = Context.getTargetInfo();
1274 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001275 AddString(TargetOpts.Triple, Record);
1276 AddString(TargetOpts.CPU, Record);
1277 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001278 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1279 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1280 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1281 }
1282 Record.push_back(TargetOpts.Features.size());
1283 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1284 AddString(TargetOpts.Features[I], Record);
1285 }
1286 Stream.EmitRecord(TARGET_OPTIONS, Record);
1287
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001288 // Diagnostic options.
1289 Record.clear();
1290 const DiagnosticOptions &DiagOpts
1291 = Context.getDiagnostics().getDiagnosticOptions();
1292#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1293#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1294 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1295#include "clang/Basic/DiagnosticOptions.def"
1296 Record.push_back(DiagOpts.Warnings.size());
1297 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1298 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001299 Record.push_back(DiagOpts.Remarks.size());
1300 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1301 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001302 // Note: we don't serialize the log or serialization file names, because they
1303 // are generally transient files and will almost always be overridden.
1304 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1305
Douglas Gregorc6317db2012-10-24 15:49:58 +00001306 // File system options.
1307 Record.clear();
1308 const FileSystemOptions &FSOpts
1309 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1310 AddString(FSOpts.WorkingDir, Record);
1311 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1312
Douglas Gregor2d302362012-10-24 16:50:34 +00001313 // Header search options.
1314 Record.clear();
1315 const HeaderSearchOptions &HSOpts
1316 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1317 AddString(HSOpts.Sysroot, Record);
1318
1319 // Include entries.
1320 Record.push_back(HSOpts.UserEntries.size());
1321 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1322 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1323 AddString(Entry.Path, Record);
1324 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001325 Record.push_back(Entry.IsFramework);
1326 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001327 }
1328
1329 // System header prefixes.
1330 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1331 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1332 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1333 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1334 }
1335
1336 AddString(HSOpts.ResourceDir, Record);
1337 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001338 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001339 Record.push_back(HSOpts.DisableModuleHash);
1340 Record.push_back(HSOpts.UseBuiltinIncludes);
1341 Record.push_back(HSOpts.UseStandardSystemIncludes);
1342 Record.push_back(HSOpts.UseStandardCXXIncludes);
1343 Record.push_back(HSOpts.UseLibcxx);
1344 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1345
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001346 // Preprocessor options.
1347 Record.clear();
1348 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1349
1350 // Macro definitions.
1351 Record.push_back(PPOpts.Macros.size());
1352 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1353 AddString(PPOpts.Macros[I].first, Record);
1354 Record.push_back(PPOpts.Macros[I].second);
1355 }
1356
1357 // Includes
1358 Record.push_back(PPOpts.Includes.size());
1359 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1360 AddString(PPOpts.Includes[I], Record);
1361
1362 // Macro includes
1363 Record.push_back(PPOpts.MacroIncludes.size());
1364 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1365 AddString(PPOpts.MacroIncludes[I], Record);
1366
Douglas Gregorb6368752012-10-24 23:41:50 +00001367 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001368 // Detailed record is important since it is used for the module cache hash.
1369 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001370 AddString(PPOpts.ImplicitPCHInclude, Record);
1371 AddString(PPOpts.ImplicitPTHInclude, Record);
1372 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1373 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1374
Douglas Gregora3b20262011-05-06 21:43:30 +00001375 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001376 SourceManager &SM = Context.getSourceManager();
1377 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1378 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001379 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1380 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001381 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1382 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1383
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001384 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001385 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001386 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001387 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001388 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001389
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001390 Record.clear();
1391 Record.push_back(SM.getMainFileID().getOpaqueValue());
1392 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1393
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001394 // Original PCH directory
1395 if (!OutputFile.empty() && OutputFile != "-") {
1396 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1397 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1399 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1400
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001401 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001402
1403 llvm::sys::fs::make_absolute(OutputPath);
1404 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1405
1406 RecordData Record;
1407 Record.push_back(ORIGINAL_PCH_DIR);
1408 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1409 }
1410
Douglas Gregor49491f72013-03-15 22:15:07 +00001411 WriteInputFiles(Context.SourceMgr,
1412 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001413 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001414 Stream.ExitBlock();
1415}
1416
Douglas Gregor49491f72013-03-15 22:15:07 +00001417namespace {
1418 /// \brief An input file.
1419 struct InputFileEntry {
1420 const FileEntry *File;
1421 bool IsSystemFile;
1422 bool BufferOverridden;
1423 };
1424}
1425
1426void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1427 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001428 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001429 using namespace llvm;
1430 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1431 RecordData Record;
1432
1433 // Create input-file abbreviation.
1434 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1435 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001436 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001437 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1438 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001439 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001440 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1441 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1442
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001443 // Get all ContentCache objects for files, sorted by whether the file is a
1444 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001445 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001446 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1447 // Get this source location entry.
1448 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001449 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001450
1451 // We only care about file entries that were not overridden.
1452 if (!SLoc->isFile())
1453 continue;
1454 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001455 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001456 continue;
1457
Douglas Gregor49491f72013-03-15 22:15:07 +00001458 InputFileEntry Entry;
1459 Entry.File = Cache->OrigEntry;
1460 Entry.IsSystemFile = Cache->IsSystemFile;
1461 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001462 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001463 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001464 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001465 SortedFiles.push_front(Entry);
1466 }
1467
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001468 unsigned UserFilesNum = 0;
1469 // Write out all of the input files.
1470 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001471 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001472 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001473 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001474
Douglas Gregor49491f72013-03-15 22:15:07 +00001475 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001476 if (InputFileID != 0)
1477 continue; // already recorded this file.
1478
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001479 // Record this entry's offset.
1480 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001481
1482 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001483
Douglas Gregor49491f72013-03-15 22:15:07 +00001484 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001485 ++UserFilesNum;
1486
Douglas Gregor72be3902012-10-19 00:38:02 +00001487 Record.clear();
1488 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001489 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001490
1491 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001492 Record.push_back(Entry.File->getSize());
1493 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001494
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001495 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001496 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001497
Richard Smith7ed1bc92014-12-05 22:42:13 +00001498 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1499 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001500
Douglas Gregor112b9072012-10-18 05:31:06 +00001501 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001502
1503 // Create input file offsets abbreviation.
1504 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1505 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1506 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001507 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1508 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001509 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1510 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1511
1512 // Write input file offsets.
1513 Record.clear();
1514 Record.push_back(INPUT_FILE_OFFSETS);
1515 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001516 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001517 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001518}
1519
Douglas Gregora7f71a92009-04-10 03:52:48 +00001520//===----------------------------------------------------------------------===//
1521// Source Manager Serialization
1522//===----------------------------------------------------------------------===//
1523
1524/// \brief Create an abbreviation for the SLocEntry that refers to a
1525/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001526static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001527 using namespace llvm;
1528 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001529 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001534 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001539 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001540}
1541
1542/// \brief Create an abbreviation for the SLocEntry that refers to a
1543/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001544static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001545 using namespace llvm;
1546 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001547 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001553 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001554}
1555
1556/// \brief Create an abbreviation for the SLocEntry that refers to a
1557/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001558static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001559 using namespace llvm;
1560 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001561 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001563 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001564}
1565
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001566/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1567/// expansion.
1568static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001569 using namespace llvm;
1570 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001571 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001577 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001578}
1579
Douglas Gregor09b69892011-02-10 17:09:37 +00001580namespace {
1581 // Trait used for the on-disk hash table of header search information.
1582 class HeaderFileInfoTrait {
1583 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001584 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001585
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001586 // Keep track of the framework names we've used during serialization.
1587 SmallVector<char, 128> FrameworkStringData;
1588 llvm::StringMap<unsigned> FrameworkNameOffset;
1589
Douglas Gregor09b69892011-02-10 17:09:37 +00001590 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001591 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1592 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001593
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001594 struct key_type {
1595 const FileEntry *FE;
1596 const char *Filename;
1597 };
1598 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001599
1600 typedef HeaderFileInfo data_type;
1601 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001602 typedef unsigned hash_value_type;
1603 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001604
Justin Bogner25463f12014-04-18 20:27:24 +00001605 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001606 // The hash is based only on size/time of the file, so that the reader can
1607 // match even when symlinking or excess path elements ("foo/../", "../")
1608 // change the form of the name. However, complete path is still the key.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001609 //
1610 // FIXME: Using the mtime here will cause problems for explicit module
1611 // imports.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001612 return llvm::hash_combine(key.FE->getSize(),
1613 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001614 }
1615
1616 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001617 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001618 using namespace llvm::support;
1619 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001620 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001621 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001622 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001623 if (Data.isModuleHeader)
1624 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001625 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001626 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001627 }
1628
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001629 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001630 using namespace llvm::support;
1631 endian::Writer<little> LE(Out);
1632 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001633 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001634 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001635 KeyLen -= 8;
1636 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001637 }
1638
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001639 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001640 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001641 using namespace llvm::support;
1642 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001643 uint64_t Start = Out.tell(); (void)Start;
1644
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001645 unsigned char Flags = (Data.HeaderRole << 6)
1646 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001647 | (Data.isPragmaOnce << 4)
1648 | (Data.DirInfo << 2)
1649 | (Data.Resolved << 1)
1650 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001651 LE.write<uint8_t>(Flags);
1652 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001653
1654 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001655 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001656 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001657 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001658
1659 unsigned Offset = 0;
1660 if (!Data.Framework.empty()) {
1661 // If this header refers into a framework, save the framework name.
1662 llvm::StringMap<unsigned>::iterator Pos
1663 = FrameworkNameOffset.find(Data.Framework);
1664 if (Pos == FrameworkNameOffset.end()) {
1665 Offset = FrameworkStringData.size() + 1;
1666 FrameworkStringData.append(Data.Framework.begin(),
1667 Data.Framework.end());
1668 FrameworkStringData.push_back(0);
1669
1670 FrameworkNameOffset[Data.Framework] = Offset;
1671 } else
1672 Offset = Pos->second;
1673 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001674 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001675
1676 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001677 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001678 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001679 }
1680
Douglas Gregor09b69892011-02-10 17:09:37 +00001681 assert(Out.tell() - Start == DataLen && "Wrong data length");
1682 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001683
1684 const char *strings_begin() const { return FrameworkStringData.begin(); }
1685 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001686 };
1687} // end anonymous namespace
1688
1689/// \brief Write the header search block for the list of files that
1690///
1691/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001692void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001693 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001694 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1695
1696 if (FilesByUID.size() > HS.header_file_size())
1697 FilesByUID.resize(HS.header_file_size());
1698
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001699 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001700 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001701 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001702 unsigned NumHeaderSearchEntries = 0;
1703 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1704 const FileEntry *File = FilesByUID[UID];
1705 if (!File)
1706 continue;
1707
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001708 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1709 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001710 HeaderFileInfo HFI;
1711 if (!HS.tryGetFileInfo(File, HFI) ||
1712 (HFI.External && Chain) ||
1713 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001714 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001715
Richard Smith7ed1bc92014-12-05 22:42:13 +00001716 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001717 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001718 SmallString<128> FilenameTmp(Filename);
1719 if (PreparePathForOutput(FilenameTmp)) {
1720 // If we performed any translation on the file name at all, we need to
1721 // save this string, since the generator will refer to it later.
1722 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001723 SavedStrings.push_back(Filename);
1724 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001725
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001726 HeaderFileInfoTrait::key_type key = { File, Filename };
1727 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001728 ++NumHeaderSearchEntries;
1729 }
1730
1731 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001732 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001733 uint32_t BucketOffset;
1734 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001735 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001736 llvm::raw_svector_ostream Out(TableData);
1737 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001738 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001739 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1740 }
1741
1742 // Create a blob abbreviation
1743 using namespace llvm;
1744 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1745 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1750 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1751
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001752 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001753 RecordData Record;
1754 Record.push_back(HEADER_SEARCH_TABLE);
1755 Record.push_back(BucketOffset);
1756 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001757 Record.push_back(TableData.size());
1758 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001759 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1760
1761 // Free all of the strings we had to duplicate.
1762 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001763 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001764}
1765
Douglas Gregora7f71a92009-04-10 03:52:48 +00001766/// \brief Writes the block containing the serialized form of the
1767/// source manager.
1768///
1769/// TODO: We should probably use an on-disk hash table (stored in a
1770/// blob), indexed based on the file name, so that we only create
1771/// entries for files that we actually need. In the common case (no
1772/// errors), we probably won't have to create file entries for any of
1773/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001774void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001775 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001776 RecordData Record;
1777
Chris Lattner0910e3b2009-04-10 17:16:57 +00001778 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001779 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001780
1781 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001782 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1783 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1784 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001785 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001786
Douglas Gregor258ae542009-04-27 06:38:32 +00001787 // Write out the source location entry table. We skip the first
1788 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001789 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001790 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001791 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1792 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001793 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001794 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001795 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001796 FileID FID = FileID::get(I);
1797 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001798
Douglas Gregor258ae542009-04-27 06:38:32 +00001799 // Record the offset of this source-location entry.
1800 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1801
1802 // Figure out which record code to use.
1803 unsigned Code;
1804 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001805 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1806 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001807 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001808 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001809 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001810 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001811 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001812 Record.clear();
1813 Record.push_back(Code);
1814
Douglas Gregor925296b2011-07-19 16:10:42 +00001815 // Starting offset of this entry within this module, so skip the dummy.
1816 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001817 if (SLoc->isFile()) {
1818 const SrcMgr::FileInfo &File = SLoc->getFile();
1819 Record.push_back(File.getIncludeLoc().getRawEncoding());
1820 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1821 Record.push_back(File.hasLineDirectives());
1822
1823 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001824 if (Content->OrigEntry) {
1825 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001826 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001827
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001828 // The source location entry is a file. Emit input file ID.
1829 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1830 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001831
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001832 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001833
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001834 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001835 if (FDI != FileDeclIDs.end()) {
1836 Record.push_back(FDI->second->FirstDeclIndex);
1837 Record.push_back(FDI->second->DeclIDs.size());
1838 } else {
1839 Record.push_back(0);
1840 Record.push_back(0);
1841 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001842
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001843 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001844
1845 if (Content->BufferOverridden) {
1846 Record.clear();
1847 Record.push_back(SM_SLOC_BUFFER_BLOB);
1848 const llvm::MemoryBuffer *Buffer
1849 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1850 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1851 StringRef(Buffer->getBufferStart(),
1852 Buffer->getBufferSize() + 1));
1853 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001854 } else {
1855 // The source location entry is a buffer. The blob associated
1856 // with this entry contains the contents of the buffer.
1857
1858 // We add one to the size so that we capture the trailing NULL
1859 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1860 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001861 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001862 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001863 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001864 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001865 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001866 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001867 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001868 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001869 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001870 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001871
Douglas Gregor925296b2011-07-19 16:10:42 +00001872 if (strcmp(Name, "<built-in>") == 0) {
1873 PreloadSLocs.push_back(SLocEntryOffsets.size());
1874 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001875 }
1876 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001877 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001878 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001879 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1880 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001881 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1882 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001883
1884 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001885 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001886 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001887 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001888 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001889 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001890 }
1891 }
1892
Douglas Gregor8f45df52009-04-16 22:23:12 +00001893 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001894
1895 if (SLocEntryOffsets.empty())
1896 return;
1897
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001898 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001899 // table is used for lazily loading source-location information.
1900 using namespace llvm;
1901 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001902 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1906 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001907
Douglas Gregor258ae542009-04-27 06:38:32 +00001908 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001909 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001910 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001911 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001912 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001913
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001914 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001915 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001916 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001917
1918 // Write the line table. It depends on remapping working, so it must come
1919 // after the source location offsets.
1920 if (SourceMgr.hasLineTable()) {
1921 LineTableInfo &LineTable = SourceMgr.getLineTable();
1922
1923 Record.clear();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001924 // Emit the file names.
Douglas Gregor925296b2011-07-19 16:10:42 +00001925 Record.push_back(LineTable.getNumFilenames());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001926 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I)
1927 AddPath(LineTable.getFilename(I), Record);
Douglas Gregor925296b2011-07-19 16:10:42 +00001928
1929 // Emit the line entries
1930 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1931 L != LEnd; ++L) {
1932 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001933 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001934 continue;
1935
1936 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001937 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001938
1939 // Emit the line entries
1940 Record.push_back(L->second.size());
1941 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1942 LEEnd = L->second.end();
1943 LE != LEEnd; ++LE) {
1944 Record.push_back(LE->FileOffset);
1945 Record.push_back(LE->LineNo);
1946 Record.push_back(LE->FilenameID);
1947 Record.push_back((unsigned)LE->FileKind);
1948 Record.push_back(LE->IncludeOffset);
1949 }
1950 }
1951 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1952 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001953}
1954
Douglas Gregorc5046832009-04-27 18:38:38 +00001955//===----------------------------------------------------------------------===//
1956// Preprocessor Serialization
1957//===----------------------------------------------------------------------===//
1958
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001959namespace {
1960class ASTMacroTableTrait {
1961public:
1962 typedef IdentID key_type;
1963 typedef key_type key_type_ref;
1964
1965 struct Data {
1966 uint32_t MacroDirectivesOffset;
1967 };
1968
1969 typedef Data data_type;
1970 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001971 typedef unsigned hash_value_type;
1972 typedef unsigned offset_type;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001973
Justin Bogner25463f12014-04-18 20:27:24 +00001974 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001975 return llvm::hash_value(IdID);
1976 }
1977
1978 std::pair<unsigned,unsigned>
1979 static EmitKeyDataLength(raw_ostream& Out,
1980 key_type_ref Key, data_type_ref Data) {
1981 unsigned KeyLen = 4; // IdentID.
1982 unsigned DataLen = 4; // MacroDirectivesOffset.
1983 return std::make_pair(KeyLen, DataLen);
1984 }
1985
1986 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001987 using namespace llvm::support;
1988 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001989 }
1990
1991 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1992 unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001993 using namespace llvm::support;
1994 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001995 }
1996};
1997} // end anonymous namespace
1998
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001999static int compareMacroDirectives(
2000 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
2001 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
2002 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002003}
2004
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002005static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2006 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002007 if (MacroInfo *MI = MD->getMacroInfo())
2008 if (MI->isBuiltinMacro())
2009 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002010
2011 if (IsModule) {
Richard Smithe657bbd2014-07-18 22:13:40 +00002012 // Re-export any imported directives.
Richard Smithdaa69e02014-07-25 04:40:03 +00002013 if (MD->isImported())
2014 return false;
Richard Smithe657bbd2014-07-18 22:13:40 +00002015
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002016 SourceLocation Loc = MD->getLocation();
2017 if (Loc.isInvalid())
2018 return true;
2019 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2020 return true;
2021 }
2022
2023 return false;
2024}
2025
Chris Lattnereeffaef2009-04-10 17:15:23 +00002026/// \brief Writes the block containing the serialized form of the
2027/// preprocessor.
2028///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002029void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002030 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2031 if (PPRec)
2032 WritePreprocessorDetail(*PPRec);
2033
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002034 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002035
Chris Lattner0af3ba12009-04-13 01:29:17 +00002036 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2037 if (PP.getCounterValue() != 0) {
2038 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002039 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002040 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002041 }
2042
2043 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002044 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002045
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002046 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002047 // FIXME: use diagnostics subsystem for localization etc.
2048 if (PP.SawDateOrTime())
2049 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00002050
Douglas Gregor796d76a2010-10-20 22:00:55 +00002051
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002052 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002053 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002054
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002055 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002056 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002057 MacroDirectives;
2058 for (Preprocessor::macro_iterator
2059 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2060 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002061 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002062 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002063 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002064
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002065 // Sort the set of macro definitions that need to be serialized by the
2066 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002067 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2068 &compareMacroDirectives);
2069
Justin Bognerbb094f02014-04-18 19:57:06 +00002070 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002071
2072 // Emit the macro directives as a list and associate the offset with the
2073 // identifier they belong to.
2074 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2075 const IdentifierInfo *Name = MacroDirectives[I].first;
2076 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2077 MacroDirective *MD = MacroDirectives[I].second;
2078
2079 // If the macro or identifier need no updates, don't write the macro history
2080 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002081 // FIXME: Chain the macro history instead of re-writing it.
2082 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002083 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2084 continue;
2085
2086 // Emit the macro directives in reverse source order.
2087 for (; MD; MD = MD->getPrevious()) {
2088 if (shouldIgnoreMacro(MD, IsModule, PP))
2089 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002090
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002091 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002092 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002093 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002094 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2095 Record.push_back(InfoID);
Richard Smithdaa69e02014-07-25 04:40:03 +00002096 Record.push_back(DefMD->getOwningModuleID());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002097 Record.push_back(DefMD->isAmbiguous());
Richard Smithdaa69e02014-07-25 04:40:03 +00002098 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2099 Record.push_back(UndefMD->getOwningModuleID());
2100 } else {
2101 auto *VisMD = cast<VisibilityMacroDirective>(MD);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002102 Record.push_back(VisMD->isPublic());
2103 }
Richard Smithdaa69e02014-07-25 04:40:03 +00002104
2105 if (MD->isImported()) {
2106 auto Overrides = MD->getOverriddenModules();
2107 Record.push_back(Overrides.size());
2108 for (auto Override : Overrides)
2109 Record.push_back(Override);
2110 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002111 }
2112 if (Record.empty())
2113 continue;
2114
2115 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2116 Record.clear();
2117
2118 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2119
2120 IdentID NameID = getIdentifierRef(Name);
2121 ASTMacroTableTrait::Data data;
2122 data.MacroDirectivesOffset = MacroDirectiveOffset;
2123 Generator.insert(NameID, data);
2124 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002125
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002126 /// \brief Offsets of each of the macros into the bitstream, indexed by
2127 /// the local macro ID
2128 ///
2129 /// For each identifier that is associated with a macro, this map
2130 /// provides the offset into the bitstream where that macro is
2131 /// defined.
2132 std::vector<uint32_t> MacroOffsets;
2133
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002134 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2135 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2136 MacroInfo *MI = MacroInfosToEmit[I].MI;
2137 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002138
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002139 if (ID < FirstMacroID) {
2140 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2141 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002142 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002143
2144 // Record the local offset of this macro.
2145 unsigned Index = ID - FirstMacroID;
2146 if (Index == MacroOffsets.size())
2147 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2148 else {
2149 if (Index > MacroOffsets.size())
2150 MacroOffsets.resize(Index + 1);
2151
2152 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2153 }
2154
2155 AddIdentifierRef(Name, Record);
2156 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2157 AddSourceLocation(MI->getDefinitionLoc(), Record);
2158 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2159 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002160 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002161 unsigned Code;
2162 if (MI->isObjectLike()) {
2163 Code = PP_MACRO_OBJECT_LIKE;
2164 } else {
2165 Code = PP_MACRO_FUNCTION_LIKE;
2166
2167 Record.push_back(MI->isC99Varargs());
2168 Record.push_back(MI->isGNUVarargs());
2169 Record.push_back(MI->hasCommaPasting());
2170 Record.push_back(MI->getNumArgs());
2171 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2172 I != E; ++I)
2173 AddIdentifierRef(*I, Record);
2174 }
2175
2176 // If we have a detailed preprocessing record, record the macro definition
2177 // ID that corresponds to this macro.
2178 if (PPRec)
2179 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2180
2181 Stream.EmitRecord(Code, Record);
2182 Record.clear();
2183
2184 // Emit the tokens array.
2185 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2186 // Note that we know that the preprocessor does not have any annotation
2187 // tokens in it because they are created by the parser, and thus can't
2188 // be in a macro definition.
2189 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002190 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002191 Stream.EmitRecord(PP_TOKEN, Record);
2192 Record.clear();
2193 }
2194 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002195 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002196
Douglas Gregor92a96f52011-02-08 21:58:10 +00002197 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002198
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002199 // Create the on-disk hash table in a buffer.
2200 SmallString<4096> MacroTable;
2201 uint32_t BucketOffset;
2202 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002203 using namespace llvm::support;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002204 llvm::raw_svector_ostream Out(MacroTable);
2205 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002206 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002207 BucketOffset = Generator.Emit(Out);
2208 }
2209
2210 // Write the macro table
2211 using namespace llvm;
2212 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2213 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2216 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2217
2218 Record.push_back(MACRO_TABLE);
2219 Record.push_back(BucketOffset);
2220 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2221 Record.clear();
2222
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002223 // Write the offsets table for macro IDs.
2224 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002225 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002226 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2230
2231 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2232 Record.clear();
2233 Record.push_back(MACRO_OFFSET);
2234 Record.push_back(MacroOffsets.size());
2235 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2236 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2237 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002238}
2239
2240void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002241 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002242 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002243
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002244 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002245
Douglas Gregor92a96f52011-02-08 21:58:10 +00002246 // Enter the preprocessor block.
2247 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002248
Douglas Gregoraae92242010-03-19 21:51:54 +00002249 // If the preprocessor has a preprocessing record, emit it.
2250 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002251 using namespace llvm;
2252
2253 // Set up the abbreviation for
2254 unsigned InclusionAbbrev = 0;
2255 {
2256 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2257 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2263 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2264 }
2265
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002266 unsigned FirstPreprocessorEntityID
2267 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2268 + NUM_PREDEF_PP_ENTITY_IDS;
2269 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002270 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002271 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2272 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002273 E != EEnd;
2274 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002275 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002276
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002277 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2278 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002279
Douglas Gregor92a96f52011-02-08 21:58:10 +00002280 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002281 // Record this macro definition's ID.
2282 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002283
Douglas Gregor92a96f52011-02-08 21:58:10 +00002284 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002285 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2286 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002287 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002288
Chandler Carrutha88a22182011-07-14 08:20:46 +00002289 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002290 Record.push_back(ME->isBuiltinMacro());
2291 if (ME->isBuiltinMacro())
2292 AddIdentifierRef(ME->getName(), Record);
2293 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002294 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002295 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002296 continue;
2297 }
2298
2299 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2300 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002301 Record.push_back(ID->getFileName().size());
2302 Record.push_back(ID->wasInQuotes());
2303 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002304 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002305 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002306 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002307 // Check that the FileEntry is not null because it was not resolved and
2308 // we create a PCH even with compiler errors.
2309 if (ID->getFile())
2310 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002311 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2312 continue;
2313 }
2314
2315 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2316 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002317 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002318
Douglas Gregoraae92242010-03-19 21:51:54 +00002319 // Write the offsets table for the preprocessing record.
2320 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002321 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2322
Douglas Gregoraae92242010-03-19 21:51:54 +00002323 // Write the offsets table for identifier IDs.
2324 using namespace llvm;
2325 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002326 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002329 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002330
Douglas Gregoraae92242010-03-19 21:51:54 +00002331 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002332 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002333 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002334 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2335 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002336 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002337}
2338
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002339unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2340 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2341 if (Known != SubmoduleIDs.end())
2342 return Known->second;
2343
2344 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2345}
2346
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002347unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2348 if (!Mod)
2349 return 0;
2350
2351 llvm::DenseMap<Module *, unsigned>::const_iterator
2352 Known = SubmoduleIDs.find(Mod);
2353 if (Known != SubmoduleIDs.end())
2354 return Known->second;
2355
2356 return 0;
2357}
2358
Douglas Gregor253eefe2011-12-01 00:59:36 +00002359/// \brief Compute the number of modules within the given tree (including the
2360/// given module).
2361static unsigned getNumberOfModules(Module *Mod) {
2362 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002363 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2364 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002365 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002366 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002367
2368 return ChildModules + 1;
2369}
2370
Douglas Gregorde3ef502011-11-30 23:21:26 +00002371void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002372 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002373 // FIXME: This feels like it belongs somewhere else, but there are no
2374 // other consumers of this information.
2375 SourceManager &SrcMgr = PP->getSourceManager();
2376 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002377 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002378 if (Module *ImportedFrom
2379 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2380 SrcMgr))) {
2381 ImportedFrom->Imports.push_back(I->getImportedModule());
2382 }
2383 }
2384
Douglas Gregor69021972011-11-30 17:33:56 +00002385 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002386 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002387
2388 // Write the abbreviations needed for the submodules block.
2389 using namespace llvm;
2390 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2391 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2403 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2404
2405 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002406 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2408 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2409
2410 Abbrev = new BitCodeAbbrev();
2411 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2413 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002414
2415 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002416 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2418 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2419
2420 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002421 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2423 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2424
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002425 Abbrev = new BitCodeAbbrev();
2426 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002429 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2430
Douglas Gregor59527662012-10-15 06:28:11 +00002431 Abbrev = new BitCodeAbbrev();
2432 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2433 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2434 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2435
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002436 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002437 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2439 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2440
2441 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002442 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2444 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2445
2446 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002447 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2449 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2450
2451 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002452 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2455 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2456
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002457 Abbrev = new BitCodeAbbrev();
2458 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2460 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2461
Douglas Gregorfb912652013-03-20 21:10:35 +00002462 Abbrev = new BitCodeAbbrev();
2463 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2464 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2466 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2467
Douglas Gregor253eefe2011-12-01 00:59:36 +00002468 // Write the submodule metadata block.
2469 RecordData Record;
2470 Record.push_back(getNumberOfModules(WritingModule));
2471 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2472 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2473
Douglas Gregor69021972011-11-30 17:33:56 +00002474 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002475 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002476 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002477 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002478 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002479 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002480 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002481
2482 // Emit the definition of the block.
2483 Record.clear();
2484 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002485 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002486 if (Mod->Parent) {
2487 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2488 Record.push_back(SubmoduleIDs[Mod->Parent]);
2489 } else {
2490 Record.push_back(0);
2491 }
2492 Record.push_back(Mod->IsFramework);
2493 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002494 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002495 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002496 Record.push_back(Mod->InferSubmodules);
2497 Record.push_back(Mod->InferExplicitSubmodules);
2498 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002499 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002500 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2501
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002502 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002503 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002504 Record.clear();
2505 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002506 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002507 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002508 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002509 }
2510
Douglas Gregor69021972011-11-30 17:33:56 +00002511 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002512 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002513 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002514 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002515 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002516 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002517 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2518 Record.clear();
2519 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2520 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2521 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002522 }
Richard Smith306d8922014-10-22 23:50:56 +00002523
Douglas Gregor69021972011-11-30 17:33:56 +00002524 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002525 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002526 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002527 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002528 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002529 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002530 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2531 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2532 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002533 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002534 Module::HK_PrivateTextual},
2535 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002536 };
2537 for (auto &HL : HeaderLists) {
Douglas Gregor69021972011-11-30 17:33:56 +00002538 Record.clear();
Richard Smith3c1a41a2014-12-02 00:08:08 +00002539 Record.push_back(HL.RecordKind);
2540 for (auto &H : Mod->Headers[HL.HeaderKind])
2541 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2542 }
2543
2544 // Emit the top headers.
2545 {
2546 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2547 Record.clear();
2548 Record.push_back(SUBMODULE_TOPHEADER);
2549 for (auto *H : TopHeaders)
2550 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002551 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002552
2553 // Emit the imports.
2554 if (!Mod->Imports.empty()) {
2555 Record.clear();
2556 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002557 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00002558 assert(ImportedID && "Unknown submodule!");
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002559 Record.push_back(ImportedID);
2560 }
2561 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2562 }
2563
Douglas Gregor24bb9232011-12-02 18:58:38 +00002564 // Emit the exports.
2565 if (!Mod->Exports.empty()) {
2566 Record.clear();
2567 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002568 if (Module *Exported = Mod->Exports[I].getPointer()) {
2569 unsigned ExportedID = SubmoduleIDs[Exported];
2570 assert(ExportedID > 0 && "Unknown submodule ID?");
2571 Record.push_back(ExportedID);
2572 } else {
2573 Record.push_back(0);
2574 }
2575
Douglas Gregor24bb9232011-12-02 18:58:38 +00002576 Record.push_back(Mod->Exports[I].getInt());
2577 }
2578 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2579 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002580
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002581 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2582 // Might be unnecessary as use declarations are only used to build the
2583 // module itself.
2584
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002585 // Emit the link libraries.
2586 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2587 Record.clear();
2588 Record.push_back(SUBMODULE_LINK_LIBRARY);
2589 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2590 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2591 Mod->LinkLibraries[I].Library);
2592 }
2593
Douglas Gregorfb912652013-03-20 21:10:35 +00002594 // Emit the conflicts.
2595 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2596 Record.clear();
2597 Record.push_back(SUBMODULE_CONFLICT);
2598 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2599 assert(OtherID && "Unknown submodule!");
2600 Record.push_back(OtherID);
2601 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2602 Mod->Conflicts[I].Message);
2603 }
2604
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002605 // Emit the configuration macros.
2606 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2607 Record.clear();
2608 Record.push_back(SUBMODULE_CONFIG_MACRO);
2609 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2610 Mod->ConfigMacros[I]);
2611 }
2612
Douglas Gregor69021972011-11-30 17:33:56 +00002613 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002614 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2615 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002616 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002617 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002618 }
2619
2620 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002621
2622 assert((NextSubmoduleID - FirstSubmoduleID
2623 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002624}
2625
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002626serialization::SubmoduleID
2627ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002628 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002629 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002630
2631 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002632 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002633 Module *OwningMod
2634 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002635 if (!OwningMod)
2636 return 0;
2637
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002638 // Check whether this submodule is part of our own module.
2639 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002640 return 0;
2641
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002642 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002643}
2644
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002645void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2646 bool isModule) {
2647 // Make sure set diagnostic pragmas don't affect the translation unit that
2648 // imports the module.
2649 // FIXME: Make diagnostic pragma sections work properly with modules.
2650 if (isModule)
2651 return;
2652
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002653 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2654 DiagStateIDMap;
2655 unsigned CurrID = 0;
2656 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002657 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002658 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002659 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2660 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002661 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002662 if (point.Loc.isInvalid())
2663 continue;
2664
2665 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002666 unsigned &DiagStateID = DiagStateIDMap[point.State];
2667 Record.push_back(DiagStateID);
2668
2669 if (DiagStateID == 0) {
2670 DiagStateID = ++CurrID;
2671 for (DiagnosticsEngine::DiagState::const_iterator
2672 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2673 if (I->second.isPragma()) {
2674 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002675 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002676 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002677 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002678 Record.push_back(-1); // mark the end of the diag/map pairs for this
2679 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002680 }
2681 }
2682
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002683 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002684 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002685}
2686
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002687void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2688 if (CXXBaseSpecifiersOffsets.empty())
2689 return;
2690
2691 RecordData Record;
2692
2693 // Create a blob abbreviation for the C++ base specifiers offsets.
2694 using namespace llvm;
2695
2696 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2697 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2698 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2700 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2701
Douglas Gregorc27b2872011-08-04 00:01:48 +00002702 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002703 Record.clear();
2704 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2705 Record.push_back(CXXBaseSpecifiersOffsets.size());
2706 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002707 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002708}
2709
Douglas Gregorc5046832009-04-27 18:38:38 +00002710//===----------------------------------------------------------------------===//
2711// Type Serialization
2712//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002713
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002714/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002715void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002716 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002717 if (Idx.getIndex() == 0) // we haven't seen this type before.
2718 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002719
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002720 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002721
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002722 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002723 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002724 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002725 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002726 else if (TypeOffsets.size() < Index) {
2727 TypeOffsets.resize(Index + 1);
2728 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002729 }
2730
2731 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002732
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002733 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002734 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002735 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002736
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002737 if (T.hasLocalNonFastQualifiers()) {
2738 Qualifiers Qs = T.getLocalQualifiers();
2739 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002740 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002741 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002742 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002743 } else {
2744 switch (T->getTypeClass()) {
2745 // For all of the concrete, non-dependent types, call the
2746 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002747#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002748 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002749#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002750#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002751 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002752 }
2753
2754 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002755 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002756
2757 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002758 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002759}
2760
Douglas Gregorc5046832009-04-27 18:38:38 +00002761//===----------------------------------------------------------------------===//
2762// Declaration Serialization
2763//===----------------------------------------------------------------------===//
2764
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002765/// \brief Write the block containing all of the declaration IDs
2766/// lexically declared within the given DeclContext.
2767///
2768/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2769/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002770uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002771 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002772 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002773 return 0;
2774
Douglas Gregor8f45df52009-04-16 22:23:12 +00002775 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002776 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002777 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002778 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002779 for (const auto *D : DC->decls())
2780 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002781
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002782 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002783 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002784 return Offset;
2785}
2786
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002787void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002788 using namespace llvm;
2789 RecordData Record;
2790
2791 // Write the type offsets array
2792 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002793 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2797 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2798 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002799 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002800 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002801 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002802 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002803
2804 // Write the declaration offsets array
2805 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002806 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2810 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2811 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002812 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002813 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002814 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002815 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002816}
2817
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002818void ASTWriter::WriteFileDeclIDsMap() {
2819 using namespace llvm;
2820 RecordData Record;
2821
2822 // Join the vectors of DeclIDs from all files.
2823 SmallVector<DeclID, 256> FileSortedIDs;
2824 for (FileDeclIDsTy::iterator
2825 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2826 DeclIDInFileInfo &Info = *FI->second;
2827 Info.FirstDeclIndex = FileSortedIDs.size();
2828 for (LocDeclIDsTy::iterator
2829 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2830 FileSortedIDs.push_back(DI->second);
2831 }
2832
2833 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2834 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002835 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2837 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2838 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002839 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002840 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2841}
2842
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002843void ASTWriter::WriteComments() {
2844 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002845 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002846 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002847 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2848 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002849 I != E; ++I) {
2850 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002851 AddSourceRange((*I)->getSourceRange(), Record);
2852 Record.push_back((*I)->getKind());
2853 Record.push_back((*I)->isTrailingComment());
2854 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002855 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2856 }
2857 Stream.ExitBlock();
2858}
2859
Douglas Gregorc5046832009-04-27 18:38:38 +00002860//===----------------------------------------------------------------------===//
2861// Global Method Pool and Selector Serialization
2862//===----------------------------------------------------------------------===//
2863
Douglas Gregore84a9da2009-04-20 20:36:09 +00002864namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002865// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002866class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002867 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002868
2869public:
2870 typedef Selector key_type;
2871 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002872
Sebastian Redl834bb972010-08-04 17:20:04 +00002873 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002874 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002875 ObjCMethodList Instance, Factory;
2876 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002877 typedef const data_type& data_type_ref;
2878
Justin Bogner25463f12014-04-18 20:27:24 +00002879 typedef unsigned hash_value_type;
2880 typedef unsigned offset_type;
2881
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002882 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002883
Justin Bogner25463f12014-04-18 20:27:24 +00002884 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002885 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002886 }
Mike Stump11289f42009-09-09 15:08:12 +00002887
2888 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002889 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002890 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002891 using namespace llvm::support;
2892 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002893 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002894 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002895 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2896 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002897 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002898 if (Method->Method)
2899 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002900 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002901 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002902 if (Method->Method)
2903 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002904 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002905 return std::make_pair(KeyLen, DataLen);
2906 }
Mike Stump11289f42009-09-09 15:08:12 +00002907
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002908 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002909 using namespace llvm::support;
2910 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002911 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002912 assert((Start >> 32) == 0 && "Selector key offset too large");
2913 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002914 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002915 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002916 if (N == 0)
2917 N = 1;
2918 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002919 LE.write<uint32_t>(
2920 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002921 }
Mike Stump11289f42009-09-09 15:08:12 +00002922
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002923 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002924 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002925 using namespace llvm::support;
2926 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002927 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002928 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002929 unsigned NumInstanceMethods = 0;
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)
2933 ++NumInstanceMethods;
2934
2935 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002936 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002937 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002938 if (Method->Method)
2939 ++NumFactoryMethods;
2940
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002941 unsigned InstanceBits = Methods.Instance.getBits();
2942 assert(InstanceBits < 4);
2943 unsigned NumInstanceMethodsAndBits =
2944 (NumInstanceMethods << 2) | InstanceBits;
2945 unsigned FactoryBits = Methods.Factory.getBits();
2946 assert(FactoryBits < 4);
2947 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Justin Bognere1c147c2014-03-28 22:03:19 +00002948 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2949 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002950 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002951 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002952 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002953 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002954 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002955 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002956 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002957 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002958
2959 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002960 }
2961};
2962} // end anonymous namespace
2963
Sebastian Redla19a67f2010-08-03 21:58:15 +00002964/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002965///
2966/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002967/// in an on-disk hash table indexed by the selector. The hash table also
2968/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002969void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002970 using namespace llvm;
2971
Sebastian Redla19a67f2010-08-03 21:58:15 +00002972 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002973 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002974 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002975 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002976 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002977 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002978 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002979 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002980
Sebastian Redla19a67f2010-08-03 21:58:15 +00002981 // Create the on-disk hash table representation. We walk through every
2982 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002983 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002984 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002985 I = SelectorIDs.begin(), E = SelectorIDs.end();
2986 I != E; ++I) {
2987 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002988 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002989 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002990 I->second,
2991 ObjCMethodList(),
2992 ObjCMethodList()
2993 };
2994 if (F != SemaRef.MethodPool.end()) {
2995 Data.Instance = F->second.first;
2996 Data.Factory = F->second.second;
2997 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002998 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002999 // changed.
3000 if (Chain && I->second < FirstSelectorID) {
3001 // Selector already exists. Did it change?
3002 bool changed = false;
3003 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003004 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003005 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003006 changed = true;
3007 }
3008 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003009 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003010 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003011 changed = true;
3012 }
3013 if (!changed)
3014 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003015 } else if (Data.Instance.Method || Data.Factory.Method) {
3016 // A new method pool entry.
3017 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003018 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003019 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003020 }
3021
Douglas Gregorc78d3462009-04-24 21:10:55 +00003022 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003023 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003024 uint32_t BucketOffset;
3025 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003026 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003027 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003028 llvm::raw_svector_ostream Out(MethodPool);
3029 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003030 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003031 BucketOffset = Generator.Emit(Out, Trait);
3032 }
3033
3034 // Create a blob abbreviation
3035 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003036 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3040 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3041
Douglas Gregor95c13f52009-04-25 17:48:32 +00003042 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003043 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003044 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003045 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003046 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003047 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00003048
3049 // Create a blob abbreviation for the selector table offsets.
3050 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003051 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003052 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3055 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3056
3057 // Write the selector offsets table.
3058 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003059 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003060 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003061 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003062 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003063 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003064 }
3065}
3066
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003067/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003068void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003069 using namespace llvm;
3070 if (SemaRef.ReferencedSelectors.empty())
3071 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003072
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003073 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003074
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003075 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003076 // very tricky to fix, and given that @selector shouldn't really appear in
3077 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003078 for (DenseMap<Selector, SourceLocation>::iterator S =
3079 SemaRef.ReferencedSelectors.begin(),
3080 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3081 Selector Sel = (*S).first;
3082 SourceLocation Loc = (*S).second;
3083 AddSelectorRef(Sel, Record);
3084 AddSourceLocation(Loc, Record);
3085 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003086 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003087}
3088
Douglas Gregorc5046832009-04-27 18:38:38 +00003089//===----------------------------------------------------------------------===//
3090// Identifier Table Serialization
3091//===----------------------------------------------------------------------===//
3092
Douglas Gregorc78d3462009-04-24 21:10:55 +00003093namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003094class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003095 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00003096 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003097 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003098 bool IsModule;
3099
Douglas Gregor1d583f22009-04-28 21:18:29 +00003100 /// \brief Determines whether this is an "interesting" identifier
3101 /// that needs a full IdentifierInfo structure written into the hash
3102 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003103 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003104 if (II->isPoisoned() ||
3105 II->isExtensionToken() ||
3106 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003107 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003108 II->getFETokenInfo<void>())
3109 return true;
3110
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003111 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00003112 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003113
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003114 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003115 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003116 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003117
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003118 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3119 if (!IsModule)
3120 return !shouldIgnoreMacro(Macro, IsModule, PP);
Richard Smithdaa69e02014-07-25 04:40:03 +00003121
3122 MacroState State;
3123 if (getFirstPublicSubmoduleMacro(Macro, State))
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003124 return true;
3125 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003126
3127 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003128 }
3129
Richard Smithdaa69e02014-07-25 04:40:03 +00003130 enum class SubmoduleMacroState {
3131 /// We've seen nothing about this macro.
3132 None,
3133 /// We've seen a public visibility directive.
3134 Public,
3135 /// We've either exported a macro for this module or found that the
3136 /// module's definition of this macro is private.
3137 Done
3138 };
3139 typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
Richard Smith49f906a2014-03-01 00:08:04 +00003140
3141 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003142 getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3143 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3144 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003145 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003146 }
3147
Richard Smith49f906a2014-03-01 00:08:04 +00003148 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003149 getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
Richard Smith49f906a2014-03-01 00:08:04 +00003150 if (MacroDirective *NextMD =
Richard Smithdaa69e02014-07-25 04:40:03 +00003151 getPublicSubmoduleMacro(MD->getPrevious(), State))
3152 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003153 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003154 }
3155
Richard Smithdaa69e02014-07-25 04:40:03 +00003156 /// \brief Traverses the macro directives history and returns the next
3157 /// public macro definition or undefinition that has not been found so far.
3158 ///
Richard Smith49f906a2014-03-01 00:08:04 +00003159 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003160 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00003161 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
Richard Smithdaa69e02014-07-25 04:40:03 +00003162 MacroState &State) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003163 if (!MD)
Craig Toppera13603a2014-05-22 05:54:18 +00003164 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003165
Richard Smith49f906a2014-03-01 00:08:04 +00003166 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003167 for (; MD; MD = MD->getPrevious()) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003168 // Once we hit an ignored macro, we're done: the rest of the chain
3169 // will all be ignored macros.
3170 if (shouldIgnoreMacro(MD, IsModule, PP))
3171 break;
Richard Smith57721ac2014-07-21 04:10:40 +00003172
Richard Smithdaa69e02014-07-25 04:40:03 +00003173 // If this macro was imported, re-export it.
3174 if (MD->isImported())
3175 return MD;
Richard Smith57721ac2014-07-21 04:10:40 +00003176
Richard Smithdaa69e02014-07-25 04:40:03 +00003177 SubmoduleID ModID = getSubmoduleID(MD);
3178 auto &S = State[ModID];
3179 assert(ModID && "found macro in no submodule");
Richard Smith49f906a2014-03-01 00:08:04 +00003180
Richard Smithdaa69e02014-07-25 04:40:03 +00003181 if (S == SubmoduleMacroState::Done)
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003182 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003183
Richard Smithdaa69e02014-07-25 04:40:03 +00003184 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3185 // The latest visibility directive for a name in a submodule affects all
3186 // the directives that come before it.
3187 if (S == SubmoduleMacroState::None)
3188 S = VisMD->isPublic() ? SubmoduleMacroState::Public
3189 : SubmoduleMacroState::Done;
3190 } else {
3191 S = SubmoduleMacroState::Done;
Richard Smith49f906a2014-03-01 00:08:04 +00003192 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003193 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003194 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003195
Craig Toppera13603a2014-05-22 05:54:18 +00003196 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003197 }
3198
Richard Smithdaa69e02014-07-25 04:40:03 +00003199 ArrayRef<SubmoduleID>
3200 getOverriddenSubmodules(MacroDirective *MD,
3201 SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3202 assert(!isa<VisibilityMacroDirective>(MD) &&
3203 "only #define and #undef can override");
3204 if (MD->isImported())
3205 return MD->getOverriddenModules();
3206
3207 ScratchSpace.clear();
3208 SubmoduleID ModID = getSubmoduleID(MD);
3209 for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3210 if (shouldIgnoreMacro(MD, IsModule, PP))
3211 break;
3212
3213 // If this is a definition from a submodule import, that submodule's
3214 // definition is overridden by the definition or undefinition that we
3215 // started with.
3216 if (MD->isImported()) {
3217 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3218 SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3219 assert(DefModuleID && "imported macro has no owning module");
3220 ScratchSpace.push_back(DefModuleID);
3221 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3222 // If we override a #undef, we override anything that #undef overrides.
3223 // We don't need to override it, since an active #undef doesn't affect
3224 // the meaning of a macro.
3225 auto Overrides = UndefMD->getOverriddenModules();
3226 ScratchSpace.insert(ScratchSpace.end(),
3227 Overrides.begin(), Overrides.end());
3228 }
3229 }
3230
3231 // Stop once we leave the original macro's submodule.
3232 //
3233 // Either this submodule #included another submodule of the same
3234 // module or it just happened to be built after the other module.
3235 // In the former case, we override the submodule's macro.
3236 //
3237 // FIXME: In the latter case, we shouldn't do so, but we can't tell
3238 // these cases apart.
3239 //
3240 // FIXME: We can leave this submodule and re-enter it if it #includes a
3241 // header within a different submodule of the same module. In such cases
3242 // the overrides list will be incomplete.
3243 SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3244 if (DirectiveModuleID != ModID) {
3245 if (DirectiveModuleID && !MD->isImported())
3246 ScratchSpace.push_back(DirectiveModuleID);
3247 break;
3248 }
3249 }
3250
3251 std::sort(ScratchSpace.begin(), ScratchSpace.end());
3252 ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3253 ScratchSpace.end());
3254 return ScratchSpace;
3255 }
3256
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003257 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Richard Smith57721ac2014-07-21 04:10:40 +00003258 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003259 }
3260
Douglas Gregore84a9da2009-04-20 20:36:09 +00003261public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003262 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003263 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003264
Sebastian Redl539c5062010-08-18 23:57:32 +00003265 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003266 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003267
Justin Bogner25463f12014-04-18 20:27:24 +00003268 typedef unsigned hash_value_type;
3269 typedef unsigned offset_type;
3270
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003271 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3272 IdentifierResolver &IdResolver, bool IsModule)
3273 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003274
Justin Bogner25463f12014-04-18 20:27:24 +00003275 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003276 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003277 }
Mike Stump11289f42009-09-09 15:08:12 +00003278
3279 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003280 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003281 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003282 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Craig Toppera13603a2014-05-22 05:54:18 +00003283 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003284 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003285 DataLen += 2; // 2 bytes for builtin ID
3286 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003287 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003288 DataLen += 4; // MacroDirectives offset.
3289 if (IsModule) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003290 MacroState State;
3291 SmallVector<SubmoduleID, 16> Scratch;
3292 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3293 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003294 DataLen += 4; // MacroInfo ID or ModuleID.
Richard Smithdaa69e02014-07-25 04:40:03 +00003295 if (unsigned NumOverrides =
3296 getOverriddenSubmodules(MD, Scratch).size())
3297 DataLen += 4 * (1 + NumOverrides);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003298 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003299 DataLen += 4; // 0 terminator.
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003300 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003301 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003302
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003303 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3304 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003305 D != DEnd; ++D)
Richard Smithdaa69e02014-07-25 04:40:03 +00003306 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003307 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003308 using namespace llvm::support;
3309 endian::Writer<little> LE(Out);
3310
3311 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003312 // We emit the key length after the data length so that every
3313 // string is preceded by a 16-bit length. This matches the PTH
3314 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003315 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003316 return std::make_pair(KeyLen, DataLen);
3317 }
Mike Stump11289f42009-09-09 15:08:12 +00003318
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003319 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003320 unsigned KeyLen) {
3321 // Record the location of the key data. This is used when generating
3322 // the mapping from persistent IDs to strings.
3323 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003324 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003325 }
Mike Stump11289f42009-09-09 15:08:12 +00003326
Richard Smith49f906a2014-03-01 00:08:04 +00003327 static void emitMacroOverrides(raw_ostream &Out,
Craig Topper00bbdcf2014-06-28 23:22:23 +00003328 ArrayRef<SubmoduleID> Overridden) {
Richard Smith49f906a2014-03-01 00:08:04 +00003329 if (!Overridden.empty()) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003330 using namespace llvm::support;
3331 endian::Writer<little> LE(Out);
3332 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Richard Smithdaa69e02014-07-25 04:40:03 +00003333 for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3334 assert(Overridden[I] && "zero module ID for override");
Justin Bognere1c147c2014-03-28 22:03:19 +00003335 LE.write<uint32_t>(Overridden[I]);
Richard Smithdaa69e02014-07-25 04:40:03 +00003336 }
Richard Smith49f906a2014-03-01 00:08:04 +00003337 }
3338 }
3339
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003340 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003341 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003342 using namespace llvm::support;
3343 endian::Writer<little> LE(Out);
Craig Toppera13603a2014-05-22 05:54:18 +00003344 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003345 if (!isInterestingIdentifier(II, Macro)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003346 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003347 return;
3348 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003349
Justin Bognere1c147c2014-03-28 22:03:19 +00003350 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003351 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3352 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003353 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003354 Bits = 0;
3355 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003356 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003357 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003358 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3359 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003360 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003361 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003362 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003363
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003364 if (HadMacroDefinition) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003365 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003366 if (IsModule) {
3367 // Write the IDs of macros coming from different submodules.
Richard Smithdaa69e02014-07-25 04:40:03 +00003368 MacroState State;
3369 SmallVector<SubmoduleID, 16> Scratch;
3370 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3371 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003372 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003373 // FIXME: If this macro directive was created by #pragma pop_macros,
3374 // or if it was created implicitly by resolving conflicting macros,
3375 // it may be for a different submodule from the one in the MacroInfo
3376 // object. If so, we should write out its owning ModuleID.
3377 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Richard Smith49f906a2014-03-01 00:08:04 +00003378 assert(InfoID);
Justin Bognere1c147c2014-03-28 22:03:19 +00003379 LE.write<uint32_t>(InfoID << 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003380 } else {
Richard Smithdaa69e02014-07-25 04:40:03 +00003381 auto *UndefMD = cast<UndefMacroDirective>(MD);
3382 SubmoduleID Mod = UndefMD->isImported()
3383 ? UndefMD->getOwningModuleID()
3384 : getSubmoduleID(UndefMD);
3385 LE.write<uint32_t>((Mod << 1) | 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003386 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003387 emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003388 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003389 LE.write<uint32_t>(0xdeadbeef);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003390 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003391 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003392
Douglas Gregora868bbd2009-04-21 22:25:48 +00003393 // Emit the declaration IDs in reverse order, because the
3394 // IdentifierResolver provides the declarations as they would be
3395 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003396 // "stat"), but the ASTReader adds declarations to the end of the list
3397 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003398 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003399 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3400 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003401 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003402 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003403 D != DEnd; ++D)
Justin Bognere1c147c2014-03-28 22:03:19 +00003404 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003405 }
3406
3407 /// \brief Returns the most recent local decl or the given decl if there are
3408 /// no local ones. The given decl is assumed to be the most recent one.
3409 Decl *getMostRecentLocalDecl(Decl *Orig) {
3410 // The only way a "from AST file" decl would be more recent from a local one
3411 // is if it came from a module.
3412 if (!PP.getLangOpts().Modules)
3413 return Orig;
3414
3415 // Look for a local in the decl chain.
3416 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3417 if (!D->isFromASTFile())
3418 return D;
3419 // If we come up a decl from a (chained-)PCH stop since we won't find a
3420 // local one.
3421 if (D->getOwningModuleID() == 0)
3422 break;
3423 }
3424
3425 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003426 }
3427};
3428} // end anonymous namespace
3429
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003430/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003431///
3432/// The identifier table consists of a blob containing string data
3433/// (the actual identifiers themselves) and a separate "offsets" index
3434/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003435void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3436 IdentifierResolver &IdResolver,
3437 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003438 using namespace llvm;
3439
3440 // Create and write out the blob that contains the identifier
3441 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003442 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003443 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003444 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003445
Douglas Gregore6648fb2009-04-28 20:33:11 +00003446 // Look for any identifiers that were named while processing the
3447 // headers, but are otherwise not needed. We add these to the hash
3448 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003449 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003450 // file.
3451 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3452 IDEnd = PP.getIdentifierTable().end();
3453 ID != IDEnd; ++ID)
3454 getIdentifierRef(ID->second);
3455
Sebastian Redlff4a2952010-07-23 23:49:55 +00003456 // Create the on-disk hash table representation. We only store offsets
3457 // for identifiers that appear here for the first time.
3458 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003459 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003460 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3461 ID != IDEnd; ++ID) {
3462 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003463 if (!Chain || !ID->first->isFromAST() ||
3464 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003465 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003466 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003467 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003468
Douglas Gregore84a9da2009-04-20 20:36:09 +00003469 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003470 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003471 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003472 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003473 using namespace llvm::support;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003474 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003475 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003476 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003477 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003478 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003479 }
3480
3481 // Create a blob abbreviation
3482 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003483 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003484 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003486 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003487
3488 // Write the identifier table
3489 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003490 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003491 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003492 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003493 }
3494
3495 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003496 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003497 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3501 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3502
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003503#ifndef NDEBUG
3504 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3505 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3506#endif
3507
Douglas Gregor0e149972009-04-25 19:10:14 +00003508 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003509 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003510 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003511 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003512 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003513 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003514}
3515
Douglas Gregorc5046832009-04-27 18:38:38 +00003516//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003517// DeclContext's Name Lookup Table Serialization
3518//===----------------------------------------------------------------------===//
3519
Richard Smithbb853c72014-08-13 01:23:33 +00003520/// Determine the declaration that should be put into the name lookup table to
3521/// represent the given declaration in this module. This is usually D itself,
3522/// but if D was imported and merged into a local declaration, we want the most
3523/// recent local declaration instead. The chosen declaration will be the most
3524/// recent declaration in any module that imports this one.
3525static NamedDecl *getDeclForLocalLookup(NamedDecl *D) {
Richard Smith8c913ec2014-08-14 02:21:01 +00003526 if (!D->isFromASTFile())
3527 return D;
3528
3529 if (Decl *Redecl = D->getPreviousDecl()) {
3530 // For Redeclarable decls, a prior declaration might be local.
3531 for (; Redecl; Redecl = Redecl->getPreviousDecl())
3532 if (!Redecl->isFromASTFile())
3533 return cast<NamedDecl>(Redecl);
3534 } else if (Decl *First = D->getCanonicalDecl()) {
3535 // For Mergeable decls, the first decl might be local.
3536 if (!First->isFromASTFile())
3537 return cast<NamedDecl>(First);
3538 }
3539
3540 // All declarations are imported. Our most recent declaration will also be
3541 // the most recent one in anyone who imports us.
Richard Smithbb853c72014-08-13 01:23:33 +00003542 return D;
3543}
3544
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003545namespace {
3546// Trait used for the on-disk hash table used in the method pool.
3547class ASTDeclContextNameLookupTrait {
3548 ASTWriter &Writer;
3549
3550public:
3551 typedef DeclarationName key_type;
3552 typedef key_type key_type_ref;
3553
3554 typedef DeclContext::lookup_result data_type;
3555 typedef const data_type& data_type_ref;
3556
Justin Bogner25463f12014-04-18 20:27:24 +00003557 typedef unsigned hash_value_type;
3558 typedef unsigned offset_type;
3559
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003560 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3561
Justin Bogner25463f12014-04-18 20:27:24 +00003562 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003563 llvm::FoldingSetNodeID ID;
3564 ID.AddInteger(Name.getNameKind());
3565
3566 switch (Name.getNameKind()) {
3567 case DeclarationName::Identifier:
3568 ID.AddString(Name.getAsIdentifierInfo()->getName());
3569 break;
3570 case DeclarationName::ObjCZeroArgSelector:
3571 case DeclarationName::ObjCOneArgSelector:
3572 case DeclarationName::ObjCMultiArgSelector:
3573 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3574 break;
3575 case DeclarationName::CXXConstructorName:
3576 case DeclarationName::CXXDestructorName:
3577 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003578 break;
3579 case DeclarationName::CXXOperatorName:
3580 ID.AddInteger(Name.getCXXOverloadedOperator());
3581 break;
3582 case DeclarationName::CXXLiteralOperatorName:
3583 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3584 case DeclarationName::CXXUsingDirective:
3585 break;
3586 }
3587
3588 return ID.ComputeHash();
3589 }
3590
3591 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003592 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003593 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003594 using namespace llvm::support;
3595 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003596 unsigned KeyLen = 1;
3597 switch (Name.getNameKind()) {
3598 case DeclarationName::Identifier:
3599 case DeclarationName::ObjCZeroArgSelector:
3600 case DeclarationName::ObjCOneArgSelector:
3601 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003602 case DeclarationName::CXXLiteralOperatorName:
3603 KeyLen += 4;
3604 break;
3605 case DeclarationName::CXXOperatorName:
3606 KeyLen += 1;
3607 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003608 case DeclarationName::CXXConstructorName:
3609 case DeclarationName::CXXDestructorName:
3610 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003611 case DeclarationName::CXXUsingDirective:
3612 break;
3613 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003614 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003615
3616 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003617 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003618 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003619
3620 return std::make_pair(KeyLen, DataLen);
3621 }
3622
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003623 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003624 using namespace llvm::support;
3625 endian::Writer<little> LE(Out);
3626 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003627 switch (Name.getNameKind()) {
3628 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003629 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003630 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003631 case DeclarationName::ObjCZeroArgSelector:
3632 case DeclarationName::ObjCOneArgSelector:
3633 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003634 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003635 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003636 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003637 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3638 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003639 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003640 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003641 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003642 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003643 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003644 case DeclarationName::CXXConstructorName:
3645 case DeclarationName::CXXDestructorName:
3646 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003647 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003648 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003649 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003650
3651 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003652 }
3653
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003654 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003655 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003656 using namespace llvm::support;
3657 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003658 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003659 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003660 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3661 I != E; ++I)
Richard Smithbb853c72014-08-13 01:23:33 +00003662 LE.write<uint32_t>(Writer.GetDeclRef(getDeclForLocalLookup(*I)));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003663
3664 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3665 }
3666};
3667} // end anonymous namespace
3668
Richard Smithcd45dbc2014-04-19 03:48:30 +00003669template<typename Visitor>
3670static void visitLocalLookupResults(const DeclContext *ConstDC,
3671 bool NeedToReconcileExternalVisibleStorage,
3672 Visitor AddLookupResult) {
3673 // FIXME: We need to build the lookups table, which is logically const.
3674 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Richard Smith961eae52014-03-25 01:14:22 +00003675 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3676
Richard Smith961eae52014-03-25 01:14:22 +00003677 SmallVector<DeclarationName, 16> ExternalNames;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003678 for (auto &Lookup : *DC->buildLookup()) {
Richard Smith961eae52014-03-25 01:14:22 +00003679 if (Lookup.second.hasExternalDecls() ||
Richard Smithcd45dbc2014-04-19 03:48:30 +00003680 NeedToReconcileExternalVisibleStorage) {
Richard Smith961eae52014-03-25 01:14:22 +00003681 // We don't know for sure what declarations are found by this name,
3682 // because the external source might have a different set from the set
3683 // that are in the lookup map, and we can't update it now without
3684 // risking invalidating our lookup iterator. So add it to a queue to
3685 // deal with later.
3686 ExternalNames.push_back(Lookup.first);
3687 continue;
3688 }
3689
3690 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3691 }
3692
3693 // Add the names we needed to defer. Note, this shouldn't add any new decls
3694 // to the list we need to serialize: any new declarations we find here should
3695 // be imported from an external source.
3696 // FIXME: What if the external source isn't an ASTReader?
3697 for (const auto &Name : ExternalNames)
Richard Smithcd45dbc2014-04-19 03:48:30 +00003698 AddLookupResult(Name, DC->lookup(Name));
3699}
3700
3701void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
David Blaikie82e95a32014-11-19 07:49:47 +00003702 if (UpdatedDeclContexts.insert(DC).second && WritingAST) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003703 // Ensure we emit all the visible declarations.
3704 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3705 [&](DeclarationName Name,
3706 DeclContext::lookup_const_result Result) {
3707 for (auto *Decl : Result)
Richard Smithbb853c72014-08-13 01:23:33 +00003708 GetDeclRef(getDeclForLocalLookup(Decl));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003709 });
3710 }
3711}
3712
3713uint32_t
3714ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3715 llvm::SmallVectorImpl<char> &LookupTable) {
3716 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3717
3718 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3719 Generator;
3720 ASTDeclContextNameLookupTrait Trait(*this);
3721
3722 // Create the on-disk hash table representation.
3723 DeclarationName ConstructorName;
3724 DeclarationName ConversionName;
3725 SmallVector<NamedDecl *, 8> ConstructorDecls;
3726 SmallVector<NamedDecl *, 4> ConversionDecls;
3727
3728 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3729 [&](DeclarationName Name,
3730 DeclContext::lookup_result Result) {
3731 if (Result.empty())
3732 return;
3733
3734 // Different DeclarationName values of certain kinds are mapped to
3735 // identical serialized keys, because we don't want to use type
3736 // identifiers in the keys (since type ids are local to the module).
3737 switch (Name.getNameKind()) {
3738 case DeclarationName::CXXConstructorName:
3739 // There may be different CXXConstructorName DeclarationName values
3740 // in a DeclContext because a UsingDecl that inherits constructors
3741 // has the DeclarationName of the inherited constructors.
3742 if (!ConstructorName)
3743 ConstructorName = Name;
3744 ConstructorDecls.append(Result.begin(), Result.end());
3745 return;
3746
3747 case DeclarationName::CXXConversionFunctionName:
3748 if (!ConversionName)
3749 ConversionName = Name;
3750 ConversionDecls.append(Result.begin(), Result.end());
3751 return;
3752
3753 default:
3754 break;
3755 }
3756
3757 Generator.insert(Name, Result, Trait);
3758 });
Richard Smith961eae52014-03-25 01:14:22 +00003759
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003760 // Add the constructors.
3761 if (!ConstructorDecls.empty()) {
3762 Generator.insert(ConstructorName,
3763 DeclContext::lookup_result(ConstructorDecls.begin(),
3764 ConstructorDecls.end()),
3765 Trait);
3766 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003767
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003768 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003769 if (!ConversionDecls.empty()) {
3770 Generator.insert(ConversionName,
3771 DeclContext::lookup_result(ConversionDecls.begin(),
3772 ConversionDecls.end()),
3773 Trait);
3774 }
3775
3776 // Create the on-disk hash table in a buffer.
3777 llvm::raw_svector_ostream Out(LookupTable);
3778 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003779 using namespace llvm::support;
3780 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003781 return Generator.Emit(Out, Trait);
3782}
3783
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003784/// \brief Write the block containing all of the declaration IDs
3785/// visible from the given DeclContext.
3786///
3787/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003788/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003789uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3790 DeclContext *DC) {
3791 if (DC->getPrimaryContext() != DC)
3792 return 0;
3793
3794 // Since there is no name lookup into functions or methods, don't bother to
3795 // build a visible-declarations table for these entities.
3796 if (DC->isFunctionOrMethod())
3797 return 0;
3798
3799 // If not in C++, we perform name lookup for the translation unit via the
3800 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003801 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003802 return 0;
3803
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003804 // Serialize the contents of the mapping used for lookup. Note that,
3805 // although we have two very different code paths, the serialized
3806 // representation is the same for both cases: a declaration name,
3807 // followed by a size, followed by references to the visible
3808 // declarations that have that name.
3809 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003810 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003811 if (!Map || Map->empty())
3812 return 0;
3813
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003814 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003815 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003816 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003817
3818 // Write the lookup table
3819 RecordData Record;
3820 Record.push_back(DECL_CONTEXT_VISIBLE);
3821 Record.push_back(BucketOffset);
3822 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3823 LookupTable.str());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003824 ++NumVisibleDeclContexts;
3825 return Offset;
3826}
3827
Sebastian Redla4071b42010-08-24 00:50:09 +00003828/// \brief Write an UPDATE_VISIBLE block for the given context.
3829///
3830/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3831/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003832/// (in C++), for namespaces, and for classes with forward-declared unscoped
3833/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003834void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003835 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003836 if (!Map || Map->empty())
3837 return;
3838
Sebastian Redla4071b42010-08-24 00:50:09 +00003839 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003840 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003841 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003842
3843 // Write the lookup table
3844 RecordData Record;
3845 Record.push_back(UPDATE_VISIBLE);
3846 Record.push_back(getDeclID(cast<Decl>(DC)));
3847 Record.push_back(BucketOffset);
3848 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3849}
3850
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003851/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3852void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3853 RecordData Record;
3854 Record.push_back(Opts.fp_contract);
3855 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3856}
3857
3858/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3859void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003860 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003861 return;
3862
3863 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3864 RecordData Record;
3865#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3866#include "clang/Basic/OpenCLExtensions.def"
3867 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3868}
3869
Douglas Gregor358cd442012-01-15 16:58:34 +00003870void ASTWriter::WriteRedeclarations() {
3871 RecordData LocalRedeclChains;
3872 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3873
3874 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3875 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003876 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003877
3878 Decl *MostRecent = First->getMostRecentDecl();
3879
3880 // If we only have a single declaration, there is no point in storing
3881 // a redeclaration chain.
3882 if (First == MostRecent)
3883 continue;
3884
3885 unsigned Offset = LocalRedeclChains.size();
3886 unsigned Size = 0;
3887 LocalRedeclChains.push_back(0); // Placeholder for the size.
3888
3889 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003890 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003891 Prev = Prev->getPreviousDecl()) {
3892 if (!Prev->isFromASTFile()) {
3893 AddDeclRef(Prev, LocalRedeclChains);
3894 ++Size;
3895 }
3896 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003897
3898 if (!First->isFromASTFile() && Chain) {
3899 Decl *FirstFromAST = MostRecent;
3900 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3901 if (Prev->isFromASTFile())
3902 FirstFromAST = Prev;
3903 }
3904
Richard Smith2516ba22014-08-11 18:35:44 +00003905 // FIXME: Do we need to do this for the first declaration from each
3906 // redeclaration chain that was merged into this one?
Douglas Gregor6168bd22013-02-18 15:53:43 +00003907 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3908 }
3909
Douglas Gregor358cd442012-01-15 16:58:34 +00003910 LocalRedeclChains[Offset] = Size;
3911
3912 // Reverse the set of local redeclarations, so that we store them in
3913 // order (since we found them in reverse order).
3914 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3915
Douglas Gregor6168bd22013-02-18 15:53:43 +00003916 // Add the mapping from the first ID from the AST to the set of local
3917 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003918 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3919 LocalRedeclsMap.push_back(Info);
3920
3921 assert(N == Redeclarations.size() &&
3922 "Deserialized a declaration we shouldn't have");
3923 }
3924
3925 if (LocalRedeclChains.empty())
3926 return;
3927
3928 // Sort the local redeclarations map by the first declaration ID,
3929 // since the reader will be performing binary searches on this information.
3930 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3931
3932 // Emit the local redeclarations map.
3933 using namespace llvm;
3934 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3935 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3938 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3939
3940 RecordData Record;
3941 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3942 Record.push_back(LocalRedeclsMap.size());
3943 Stream.EmitRecordWithBlob(AbbrevID, Record,
3944 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3945 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3946
3947 // Emit the redeclaration chains.
3948 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3949}
3950
Douglas Gregor404cdde2012-01-27 01:47:08 +00003951void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003952 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003953 RecordData Categories;
3954
3955 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3956 unsigned Size = 0;
3957 unsigned StartIndex = Categories.size();
3958
3959 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3960
3961 // Allocate space for the size.
3962 Categories.push_back(0);
3963
3964 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003965 for (ObjCInterfaceDecl::known_categories_iterator
3966 Cat = Class->known_categories_begin(),
3967 CatEnd = Class->known_categories_end();
3968 Cat != CatEnd; ++Cat, ++Size) {
3969 assert(getDeclID(*Cat) != 0 && "Bogus category");
3970 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003971 }
3972
3973 // Update the size.
3974 Categories[StartIndex] = Size;
3975
3976 // Record this interface -> category map.
3977 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3978 CategoriesMap.push_back(CatInfo);
3979 }
3980
3981 // Sort the categories map by the definition ID, since the reader will be
3982 // performing binary searches on this information.
3983 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3984
3985 // Emit the categories map.
3986 using namespace llvm;
3987 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3988 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3991 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3992
3993 RecordData Record;
3994 Record.push_back(OBJC_CATEGORIES_MAP);
3995 Record.push_back(CategoriesMap.size());
3996 Stream.EmitRecordWithBlob(AbbrevID, Record,
3997 reinterpret_cast<char*>(CategoriesMap.data()),
3998 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3999
4000 // Emit the category lists.
4001 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4002}
4003
Douglas Gregor464b0ca2011-12-22 21:40:42 +00004004void ASTWriter::WriteMergedDecls() {
4005 if (!Chain || Chain->MergedDecls.empty())
4006 return;
4007
4008 RecordData Record;
4009 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
4010 IEnd = Chain->MergedDecls.end();
4011 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00004012 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Richard Smithcd45dbc2014-04-19 03:48:30 +00004013 : GetDeclRef(I->first);
Douglas Gregor464b0ca2011-12-22 21:40:42 +00004014 assert(CanonID && "Merged declaration not known?");
4015
4016 Record.push_back(CanonID);
4017 Record.push_back(I->second.size());
4018 Record.append(I->second.begin(), I->second.end());
4019 }
4020 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
4021}
4022
Richard Smithe40f2ba2013-08-07 21:41:30 +00004023void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4024 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4025
4026 if (LPTMap.empty())
4027 return;
4028
4029 RecordData Record;
4030 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4031 ItEnd = LPTMap.end();
4032 It != ItEnd; ++It) {
4033 LateParsedTemplate *LPT = It->second;
4034 AddDeclRef(It->first, Record);
4035 AddDeclRef(LPT->D, Record);
4036 Record.push_back(LPT->Toks.size());
4037
4038 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4039 TokEnd = LPT->Toks.end();
4040 TokIt != TokEnd; ++TokIt) {
4041 AddToken(*TokIt, Record);
4042 }
4043 }
4044 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4045}
4046
Dario Domizioli13a0a382014-05-23 12:13:25 +00004047/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4048void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4049 RecordData Record;
4050 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4051 AddSourceLocation(PragmaLoc, Record);
4052 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4053}
4054
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004055//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004056// General Serialization Routines
4057//===----------------------------------------------------------------------===//
4058
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004059/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004060void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4061 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004062 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004063 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4064 e = Attrs.end(); i != e; ++i){
4065 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004066 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004067 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004068
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004069#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004070
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004071 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004072}
4073
John McCallf413f5e2013-05-03 00:10:13 +00004074void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4075 AddSourceLocation(Tok.getLocation(), Record);
4076 Record.push_back(Tok.getLength());
4077
4078 // FIXME: When reading literal tokens, reconstruct the literal pointer
4079 // if it is needed.
4080 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4081 // FIXME: Should translate token kind to a stable encoding.
4082 Record.push_back(Tok.getKind());
4083 // FIXME: Should translate token flags to a stable encoding.
4084 Record.push_back(Tok.getFlags());
4085}
4086
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004087void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004088 Record.push_back(Str.size());
4089 Record.insert(Record.end(), Str.begin(), Str.end());
4090}
4091
Richard Smith7ed1bc92014-12-05 22:42:13 +00004092bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004093 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004094
Richard Smith54cc3c22014-12-11 20:50:24 +00004095 bool Changed =
4096 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004097
4098 // Remove a prefix to make the path relative, if relevant.
4099 const char *PathBegin = Path.data();
4100 const char *PathPtr =
4101 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4102 if (PathPtr != PathBegin) {
4103 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4104 Changed = true;
4105 }
4106
4107 return Changed;
4108}
4109
4110void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4111 SmallString<128> FilePath(Path);
4112 PreparePathForOutput(FilePath);
4113 AddString(FilePath, Record);
4114}
4115
4116void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
4117 StringRef Path) {
4118 SmallString<128> FilePath(Path);
4119 PreparePathForOutput(FilePath);
4120 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4121}
4122
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004123void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4124 RecordDataImpl &Record) {
4125 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004126 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004127 Record.push_back(*Minor + 1);
4128 else
4129 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004130 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004131 Record.push_back(*Subminor + 1);
4132 else
4133 Record.push_back(0);
4134}
4135
Douglas Gregore84a9da2009-04-20 20:36:09 +00004136/// \brief Note that the identifier II occurs at the given offset
4137/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004138void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004139 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004140 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004141 // up earlier in the chain and thus don't need an offset.
4142 if (ID >= FirstIdentID)
4143 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004144}
4145
Douglas Gregor95c13f52009-04-25 17:48:32 +00004146/// \brief Note that the selector Sel occurs at the given offset
4147/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004148void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004149 unsigned ID = SelectorIDs[Sel];
4150 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004151 // Don't record offsets for selectors that are also available in a different
4152 // file.
4153 if (ID < FirstSelectorID)
4154 return;
4155 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004156}
4157
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004158ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Richard Smith01b2cb42014-07-26 06:37:51 +00004159 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4160 WritingModule(nullptr), WritingAST(false),
4161 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4162 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4163 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4164 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4165 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4166 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4167 NextSubmoduleID(FirstSubmoduleID),
4168 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4169 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4170 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4171 NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4172 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4173 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004174 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004175 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004176 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4177 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4178 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004179
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004180ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004181 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004182}
4183
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004184void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004185 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004186 Module *WritingModule, StringRef isysroot,
4187 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004188 WritingAST = true;
4189
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004190 ASTHasCompilerErrors = hasErrors;
4191
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004192 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004193 Stream.Emit((unsigned)'C', 8);
4194 Stream.Emit((unsigned)'P', 8);
4195 Stream.Emit((unsigned)'C', 8);
4196 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004197
Chris Lattner28fa4e62009-04-26 22:26:21 +00004198 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004199
Douglas Gregoreda8e122011-08-09 15:13:55 +00004200 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004201 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004202 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004203 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004204 Context = nullptr;
4205 PP = nullptr;
4206 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004207 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004208
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004209 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004210}
4211
Douglas Gregora94a1542011-07-27 21:45:57 +00004212template<typename Vector>
4213static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4214 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004215 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4216 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004217 Writer.AddDeclRef(*I, Record);
4218 }
4219}
4220
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004221void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004222 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004223 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004224 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004225 using namespace llvm;
4226
Craig Toppera13603a2014-05-22 05:54:18 +00004227 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004228
Douglas Gregorcf68c582011-12-01 22:20:10 +00004229 // Make sure that the AST reader knows to finalize itself.
4230 if (Chain)
4231 Chain->finalizeForWriting();
4232
Sebastian Redl143413f2010-07-12 22:02:52 +00004233 ASTContext &Context = SemaRef.Context;
4234 Preprocessor &PP = SemaRef.PP;
4235
Douglas Gregordab42432011-08-12 00:15:20 +00004236 // Set up predefined declaration IDs.
4237 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00004238 if (Context.ObjCIdDecl)
4239 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00004240 if (Context.ObjCSelDecl)
4241 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00004242 if (Context.ObjCClassDecl)
4243 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00004244 if (Context.ObjCProtocolClassDecl)
4245 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00004246 if (Context.Int128Decl)
4247 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4248 if (Context.UInt128Decl)
4249 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00004250 if (Context.ObjCInstanceTypeDecl)
4251 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004252 if (Context.BuiltinVaListDecl)
4253 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4254
Douglas Gregor851443c2011-08-12 01:39:19 +00004255 if (!Chain) {
4256 // Make sure that we emit IdentifierInfos (and any attached
4257 // declarations) for builtins. We don't need to do this when we're
4258 // emitting chained PCH files, because all of the builtins will be
4259 // in the original PCH file.
4260 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004261 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004262 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00004263 if (!Context.getLangOpts().NoBuiltin) {
4264 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4265 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004266 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4267 getIdentifierRef(&Table.get(BuiltinNames[I]));
4268 }
4269
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004270 // If there are any out-of-date identifiers, bring them up to date.
4271 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00004272 // Find out-of-date identifiers.
4273 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004274 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4275 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00004276 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004277 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00004278 OutOfDate.push_back(ID->second);
4279 }
4280
4281 // Update the out-of-date identifiers.
4282 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4283 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4284 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004285 }
4286
Richard Smithcd45dbc2014-04-19 03:48:30 +00004287 // If we saw any DeclContext updates before we started writing the AST file,
4288 // make sure all visible decls in those DeclContexts are written out.
4289 if (!UpdatedDeclContexts.empty()) {
4290 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4291 UpdatedDeclContexts.clear();
4292 for (auto *DC : OldUpdatedDeclContexts)
4293 AddUpdatedDeclContext(DC);
4294 }
4295
Chris Lattner0c797362009-09-08 18:19:27 +00004296 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004297 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004298 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004299 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004300 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004301
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004302 // Build a record containing all of the file scoped decls in this file.
4303 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004304 if (!isModule)
4305 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4306 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004307
Douglas Gregor851443c2011-08-12 01:39:19 +00004308 // Build a record containing all of the delegating constructors we still need
4309 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004310 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004311 if (!isModule)
4312 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004313
Douglas Gregor851443c2011-08-12 01:39:19 +00004314 // Write the set of weak, undeclared identifiers. We always write the
4315 // entire table, since later PCH files in a PCH chain are only interested in
4316 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004317 RecordData WeakUndeclaredIdentifiers;
4318 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004319 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004320 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4321 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4322 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4323 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4324 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4325 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4326 }
4327 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004328
Richard Smith78165b52013-01-10 23:43:47 +00004329 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004330 // declarations in this header file. Generally, this record will be
4331 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00004332 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004333 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00004334 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00004335 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00004336 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4337 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00004338 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004339 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00004340 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00004341 }
4342
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004343 // Build a record containing all of the ext_vector declarations.
4344 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004345 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004346
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004347 // Build a record containing all of the VTable uses information.
4348 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004349 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004350 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4351 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4352 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4353 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4354 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004355 }
4356
Nico Weber72889432014-09-06 01:25:55 +00004357 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4358 RecordData UnusedLocalTypedefNameCandidates;
4359 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4360 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4361
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004362 // Build a record containing all of dynamic classes declarations.
4363 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004364 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004365
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004366 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004367 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004368 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004369 I = SemaRef.PendingInstantiations.begin(),
4370 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4371 AddDeclRef(I->first, PendingInstantiations);
4372 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004373 }
4374 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4375 "There are local ones at end of translation unit!");
4376
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004377 // Build a record containing some declaration references.
4378 RecordData SemaDeclRefs;
4379 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4380 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4381 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4382 }
4383
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004384 RecordData CUDASpecialDeclRefs;
4385 if (Context.getcudaConfigureCallDecl()) {
4386 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4387 }
4388
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004389 // Build a record containing all of the known namespaces.
4390 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004391 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004392 I = SemaRef.KnownNamespaces.begin(),
4393 IEnd = SemaRef.KnownNamespaces.end();
4394 I != IEnd; ++I) {
4395 if (!I->second)
4396 AddDeclRef(I->first, KnownNamespaces);
4397 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004398
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004399 // Build a record of all used, undefined objects that require definitions.
4400 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004401
4402 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004403 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004404 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4405 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004406 AddDeclRef(I->first, UndefinedButUsed);
4407 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004408 }
4409
Douglas Gregor112b9072012-10-18 05:31:06 +00004410 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004411 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004412
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004413 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004414 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004415 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004416
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004417 // This is so that older clang versions, before the introduction
4418 // of the control block, can read and reject the newer PCH format.
4419 Record.clear();
4420 Record.push_back(VERSION_MAJOR);
4421 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4422
Douglas Gregor851443c2011-08-12 01:39:19 +00004423 // Create a lexical update block containing all of the declarations in the
4424 // translation unit that do not come from other AST files.
4425 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4426 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004427 for (const auto *I : TU->noload_decls()) {
4428 if (!I->isFromASTFile())
4429 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004430 }
4431
4432 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4433 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4434 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4435 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4436 Record.clear();
4437 Record.push_back(TU_UPDATE_LEXICAL);
4438 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4439 data(NewGlobalDecls));
4440
4441 // And a visible updates block for the translation unit.
4442 Abv = new llvm::BitCodeAbbrev();
4443 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4444 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4445 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4446 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4447 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4448 WriteDeclContextVisibleUpdate(TU);
4449
4450 // If the translation unit has an anonymous namespace, and we don't already
4451 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004452 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004453 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4454 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004455 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004456 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004457 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004458
Richard Smith5652c0f2014-03-21 01:48:23 +00004459 // Add update records for all mangling numbers and static local numbers.
4460 // These aren't really update records, but this is a convenient way of
4461 // tagging this rare extra data onto the declarations.
4462 for (const auto &Number : Context.MangleNumbers)
4463 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004464 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4465 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004466 for (const auto &Number : Context.StaticLocalNumbers)
4467 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004468 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4469 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004470
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004471 // Make sure visible decls, added to DeclContexts previously loaded from
4472 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004473 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004474 I = UpdatingVisibleDecls.begin(),
4475 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4476 GetDeclRef(*I);
4477 }
4478
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004479 // Make sure all decls associated with an identifier are registered for
4480 // serialization.
4481 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4482 IDEnd = PP.getIdentifierTable().end();
4483 ID != IDEnd; ++ID) {
4484 const IdentifierInfo *II = ID->second;
4485 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4486 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4487 DEnd = SemaRef.IdResolver.end();
4488 D != DEnd; ++D) {
4489 GetDeclRef(*D);
4490 }
4491 }
4492 }
4493
Douglas Gregor5204bde2011-08-02 16:26:37 +00004494 // Form the record of special types.
4495 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004496 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004497 AddTypeRef(Context.getFILEType(), SpecialTypes);
4498 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4499 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4500 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4501 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004502 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004503 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004504
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004505 if (Chain) {
4506 // Write the mapping information describing our module dependencies and how
4507 // each of those modules were mapped into our own offset/ID space, so that
4508 // the reader can build the appropriate mapping to its own offset/ID space.
4509 // The map consists solely of a blob with the following format:
4510 // *(module-name-len:i16 module-name:len*i8
4511 // source-location-offset:i32
4512 // identifier-id:i32
4513 // preprocessed-entity-id:i32
4514 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004515 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004516 // selector-id:i32
4517 // declaration-id:i32
4518 // c++-base-specifiers-id:i32
4519 // type-id:i32)
4520 //
4521 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4522 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4523 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4524 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004525 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004526 {
4527 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004528 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004529 using namespace llvm::support;
4530 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004531 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004532 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004533 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004534
Ben Langmuir785180e2014-10-20 16:27:30 +00004535 // Note: if a base ID was uint max, it would not be possible to load
4536 // another module after it or have more than one entity inside it.
4537 uint32_t None = std::numeric_limits<uint32_t>::max();
4538
4539 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4540 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4541 if (ShouldWrite)
4542 LE.write<uint32_t>(BaseID);
4543 else
4544 LE.write<uint32_t>(None);
4545 };
4546
Ben Langmuirfe971d92014-08-16 04:54:18 +00004547 // These values should be unique within a chain, since they will be read
4548 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004549 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4550 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4551 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4552 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4553 M->NumPreprocessedEntities);
4554 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4555 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4556 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4557 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004558 }
4559 }
4560 Record.clear();
4561 Record.push_back(MODULE_OFFSET_MAP);
4562 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4563 Buffer.data(), Buffer.size());
4564 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004565
Richard Smithb9eab6d2014-03-20 19:44:17 +00004566 RecordData DeclUpdatesOffsetsRecord;
4567
Richard Smith59442b42014-03-20 20:07:19 +00004568 // Keep writing types, declarations, and declaration update records
4569 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004570 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4571 WriteTypeAbbrevs();
4572 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004573 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4574 E = DeclsToRewrite.end();
4575 I != E; ++I)
4576 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004577 do {
4578 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4579 while (!DeclTypesToEmit.empty()) {
4580 DeclOrType DOT = DeclTypesToEmit.front();
4581 DeclTypesToEmit.pop();
4582 if (DOT.isType())
4583 WriteType(DOT.getType());
4584 else
4585 WriteDecl(Context, DOT.getDecl());
4586 }
4587 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004588 Stream.ExitBlock();
4589
Richard Smithb9eab6d2014-03-20 19:44:17 +00004590 DoneWritingDeclsAndTypes = true;
4591
4592 // These things can only be done once we've written out decls and types.
4593 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004594 if (!DeclUpdatesOffsetsRecord.empty())
4595 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004596 WriteCXXBaseSpecifiersOffsets();
4597 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004598 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004599
4600 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004601 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004602 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004603 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004604 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004605 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004606 WriteFPPragmaOptions(SemaRef.getFPOptions());
4607 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004608 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004609
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004610 // If we're emitting a module, write out the submodule information.
4611 if (WritingModule)
4612 WriteSubmodules(WritingModule);
4613
Douglas Gregor5204bde2011-08-02 16:26:37 +00004614 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4615
Douglas Gregord4df8652009-04-22 22:02:47 +00004616 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004617 if (!EagerlyDeserializedDecls.empty())
4618 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004619
4620 // Write the record containing tentative definitions.
4621 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004622 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004623
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004624 // Write the record containing unused file scoped decls.
4625 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004626 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004627
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004628 // Write the record containing weak undeclared identifiers.
4629 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004630 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004631 WeakUndeclaredIdentifiers);
4632
Richard Smith78165b52013-01-10 23:43:47 +00004633 // Write the record containing locally-scoped extern "C" definitions.
4634 if (!LocallyScopedExternCDecls.empty())
4635 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4636 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004637
4638 // Write the record containing ext_vector type names.
4639 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004640 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004641
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004642 // Write the record containing VTable uses information.
4643 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004644 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004645
4646 // Write the record containing dynamic classes declarations.
4647 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004648 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004649
Nico Weber72889432014-09-06 01:25:55 +00004650 // Write the record containing potentially unused local typedefs.
4651 if (!UnusedLocalTypedefNameCandidates.empty())
4652 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4653 UnusedLocalTypedefNameCandidates);
4654
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004655 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004656 if (!PendingInstantiations.empty())
4657 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004658
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004659 // Write the record containing declaration references of Sema.
4660 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004661 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004662
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004663 // Write the record containing CUDA-specific declaration references.
4664 if (!CUDASpecialDeclRefs.empty())
4665 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004666
4667 // Write the delegating constructors.
4668 if (!DelegatingCtorDecls.empty())
4669 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004670
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004671 // Write the known namespaces.
4672 if (!KnownNamespaces.empty())
4673 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004674
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004675 // Write the undefined internal functions and variables, and inline functions.
4676 if (!UndefinedButUsed.empty())
4677 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004678
Douglas Gregor851443c2011-08-12 01:39:19 +00004679 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004680 for (auto *DC : UpdatedDeclContexts)
4681 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004682
Douglas Gregor959bb062011-12-03 01:15:29 +00004683 if (!WritingModule) {
4684 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004685 struct ModuleInfo {
4686 uint64_t ID;
4687 Module *M;
4688 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4689 };
Richard Smith56be7542014-03-21 00:33:59 +00004690 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004691 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004692 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004693 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4694 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004695 }
Richard Smith56be7542014-03-21 00:33:59 +00004696
4697 if (!Imports.empty()) {
4698 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4699 return A.ID < B.ID;
4700 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004701 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4702 return A.ID == B.ID;
4703 };
Richard Smith56be7542014-03-21 00:33:59 +00004704
4705 // Sort and deduplicate module IDs.
4706 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004707 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004708 Imports.end());
4709
4710 RecordData ImportedModules;
4711 for (const auto &Import : Imports) {
4712 ImportedModules.push_back(Import.ID);
4713 // FIXME: If the module has macros imported then later has declarations
4714 // imported, this location won't be the right one as a location for the
4715 // declaration imports.
4716 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4717 }
4718
Douglas Gregor959bb062011-12-03 01:15:29 +00004719 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4720 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004721 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004722
Douglas Gregor851443c2011-08-12 01:39:19 +00004723 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004724 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004725 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004726 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004727 WriteLateParsedTemplates(SemaRef);
Dario Domizioli13a0a382014-05-23 12:13:25 +00004728 if(!WritingModule)
4729 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004730
Douglas Gregor08f01292009-04-17 22:13:46 +00004731 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004732 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004733 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004734 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004735 Record.push_back(NumLexicalDeclContexts);
4736 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004737 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004738 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004739}
4740
Richard Smithb9eab6d2014-03-20 19:44:17 +00004741void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004742 if (DeclUpdates.empty())
4743 return;
4744
Richard Smith59442b42014-03-20 20:07:19 +00004745 DeclUpdateMap LocalUpdates;
4746 LocalUpdates.swap(DeclUpdates);
4747
Richard Smith6ef42932014-03-20 21:02:00 +00004748 for (auto &DeclUpdate : LocalUpdates) {
4749 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004750 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004751 continue; // The decl will be written completely,no need to store updates.
4752
Richard Smithd28ac5b2014-03-22 23:33:22 +00004753 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004754 RecordData Record;
4755 for (auto &Update : DeclUpdate.second) {
4756 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4757
4758 Record.push_back(Kind);
4759 switch (Kind) {
4760 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4761 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4762 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004763 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004764 Record.push_back(GetDeclRef(Update.getDecl()));
4765 break;
4766
Richard Smith4d235792014-08-07 18:53:08 +00004767 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004768 // An updated body is emitted last, so that the reader doesn't need
4769 // to skip over the lazy body to reach statements for other records.
4770 Record.pop_back();
4771 HasUpdatedBody = true;
4772 break;
4773
Richard Smith4d235792014-08-07 18:53:08 +00004774 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4775 AddSourceLocation(Update.getLoc(), Record);
4776 break;
4777
Richard Smithcd45dbc2014-04-19 03:48:30 +00004778 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4779 auto *RD = cast<CXXRecordDecl>(D);
4780 AddUpdatedDeclContext(RD->getPrimaryContext());
4781 AddCXXDefinitionData(RD, Record);
4782 Record.push_back(WriteDeclContextLexicalBlock(
4783 *Context, const_cast<CXXRecordDecl *>(RD)));
4784
4785 // This state is sometimes updated by template instantiation, when we
4786 // switch from the specialization referring to the template declaration
4787 // to it referring to the template definition.
4788 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4789 Record.push_back(MSInfo->getTemplateSpecializationKind());
4790 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4791 } else {
4792 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4793 Record.push_back(Spec->getTemplateSpecializationKind());
4794 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004795
4796 // The instantiation might have been resolved to a partial
4797 // specialization. If so, record which one.
4798 auto From = Spec->getInstantiatedFrom();
4799 if (auto PartialSpec =
4800 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4801 Record.push_back(true);
4802 AddDeclRef(PartialSpec, Record);
4803 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4804 Record);
4805 } else {
4806 Record.push_back(false);
4807 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004808 }
4809 Record.push_back(RD->getTagKind());
4810 AddSourceLocation(RD->getLocation(), Record);
4811 AddSourceLocation(RD->getLocStart(), Record);
4812 AddSourceLocation(RD->getRBraceLoc(), Record);
4813
4814 // Instantiation may change attributes; write them all out afresh.
4815 Record.push_back(D->hasAttrs());
4816 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004817 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4818 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004819
4820 // FIXME: Ensure we don't get here for explicit instantiations.
4821 break;
4822 }
4823
Richard Smith564417a2014-03-20 21:47:22 +00004824 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4825 addExceptionSpec(
4826 *this,
4827 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4828 Record);
4829 break;
4830
Richard Smith6ef42932014-03-20 21:02:00 +00004831 case UPD_CXX_DEDUCED_RETURN_TYPE:
4832 Record.push_back(GetOrCreateTypeID(Update.getType()));
4833 break;
4834
4835 case UPD_DECL_MARKED_USED:
4836 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004837
4838 case UPD_MANGLING_NUMBER:
4839 case UPD_STATIC_LOCAL_NUMBER:
4840 Record.push_back(Update.getNumber());
4841 break;
Alexey Bataev97720002014-11-11 04:05:39 +00004842 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4843 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4844 Record);
4845 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004846 }
4847 }
4848
Richard Smithd28ac5b2014-03-22 23:33:22 +00004849 if (HasUpdatedBody) {
4850 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004851 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004852 Record.push_back(Def->isInlined());
4853 AddSourceLocation(Def->getInnerLocStart(), Record);
4854 AddFunctionDefinition(Def, Record);
Richard Smith4d235792014-08-07 18:53:08 +00004855 if (auto *DD = dyn_cast<CXXDestructorDecl>(Def))
4856 Record.push_back(GetDeclRef(DD->getOperatorDelete()));
Richard Smithd28ac5b2014-03-22 23:33:22 +00004857 }
4858
Richard Smithcd45dbc2014-04-19 03:48:30 +00004859 OffsetsRecord.push_back(GetDeclRef(D));
4860 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4861
Richard Smith6ef42932014-03-20 21:02:00 +00004862 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004863
Richard Smithb9eab6d2014-03-20 19:44:17 +00004864 // Flush any statements that were written as part of this update record.
4865 FlushStmts();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004866
4867 // Flush C++ base specifiers, if there are any.
4868 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004869 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004870}
4871
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004872void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004873 if (ReplacedDecls.empty())
4874 return;
4875
4876 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004877 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4878 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004879 Record.push_back(I->ID);
4880 Record.push_back(I->Offset);
4881 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004882 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004883 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004884}
4885
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004886void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004887 Record.push_back(Loc.getRawEncoding());
4888}
4889
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004890void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004891 AddSourceLocation(Range.getBegin(), Record);
4892 AddSourceLocation(Range.getEnd(), Record);
4893}
4894
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004895void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004896 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004897 const uint64_t *Words = Value.getRawData();
4898 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004899}
4900
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004901void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004902 Record.push_back(Value.isUnsigned());
4903 AddAPInt(Value, Record);
4904}
4905
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004906void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004907 AddAPInt(Value.bitcastToAPInt(), Record);
4908}
4909
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004910void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004911 Record.push_back(getIdentifierRef(II));
4912}
4913
Sebastian Redl539c5062010-08-18 23:57:32 +00004914IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004915 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004916 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004917
Sebastian Redl539c5062010-08-18 23:57:32 +00004918 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004919 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004920 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004921 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004922}
4923
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004924MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004925 // Don't emit builtin macros like __LINE__ to the AST file unless they
4926 // have been redefined by the header (in which case they are not
4927 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004928 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004929 return 0;
4930
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004931 MacroID &ID = MacroIDs[MI];
4932 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004933 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004934 MacroInfoToEmitData Info = { Name, MI, ID };
4935 MacroInfosToEmit.push_back(Info);
4936 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004937 return ID;
4938}
4939
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004940MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004941 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004942 return 0;
4943
4944 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4945 return MacroIDs[MI];
4946}
4947
4948uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4949 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4950 return IdentMacroDirectivesOffsetMap[Name];
4951}
4952
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004953void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004954 Record.push_back(getSelectorRef(SelRef));
4955}
4956
Sebastian Redl539c5062010-08-18 23:57:32 +00004957SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004958 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004959 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004960 }
4961
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004962 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004963 if (SID == 0 && Chain) {
4964 // This might trigger a ReadSelector callback, which will set the ID for
4965 // this selector.
4966 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004967 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004968 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004969 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004970 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004971 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004972 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004973 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004974}
4975
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004976void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004977 AddDeclRef(Temp->getDestructor(), Record);
4978}
4979
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004980void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4981 CXXBaseSpecifier const *BasesEnd,
4982 RecordDataImpl &Record) {
4983 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4984 CXXBaseSpecifiersToWrite.push_back(
4985 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4986 Bases, BasesEnd));
4987 Record.push_back(NextCXXBaseSpecifiersID++);
4988}
4989
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004990void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004991 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004992 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004993 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004994 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004995 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004996 break;
4997 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004998 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004999 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005000 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00005001 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005002 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005003 break;
5004 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00005005 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005006 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00005007 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005008 break;
John McCall0ad16662009-10-29 08:12:44 +00005009 case TemplateArgument::Null:
5010 case TemplateArgument::Integral:
5011 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005012 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005013 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005014 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005015 break;
5016 }
5017}
5018
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005019void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005020 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005021 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005022
5023 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5024 bool InfoHasSameExpr
5025 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5026 Record.push_back(InfoHasSameExpr);
5027 if (InfoHasSameExpr)
5028 return; // Avoid storing the same expr twice.
5029 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005030 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
5031 Record);
5032}
5033
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005034void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
5035 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00005036 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00005037 AddTypeRef(QualType(), Record);
5038 return;
5039 }
5040
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005041 AddTypeLoc(TInfo->getTypeLoc(), Record);
5042}
5043
5044void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
5045 AddTypeRef(TL.getType(), Record);
5046
John McCall8f115c62009-10-16 21:56:05 +00005047 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005048 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005049 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005050}
5051
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005052void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005053 Record.push_back(GetOrCreateTypeID(T));
5054}
5055
Douglas Gregoreda8e122011-08-09 15:13:55 +00005056TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005057 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00005058 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005059 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
5060}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005061
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005062TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005063 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00005064 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005065 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005066}
5067
5068TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
5069 if (T.isNull())
5070 return TypeIdx();
5071 assert(!T.getLocalFastQualifiers());
5072
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00005073 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005074 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005075 if (DoneWritingDeclsAndTypes) {
5076 assert(0 && "New type seen after serializing all the types to emit!");
5077 return TypeIdx();
5078 }
5079
Douglas Gregor1970d882009-04-26 03:49:13 +00005080 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00005081 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005082 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00005083 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00005084 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005085 return Idx;
5086}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005087
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005088TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005089 if (T.isNull())
5090 return TypeIdx();
5091 assert(!T.getLocalFastQualifiers());
5092
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005093 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5094 assert(I != TypeIdxs.end() && "Type not emitted!");
5095 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005096}
5097
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005098void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005099 Record.push_back(GetDeclRef(D));
5100}
5101
Sebastian Redl539c5062010-08-18 23:57:32 +00005102DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005103 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005104
5105 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005106 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005107 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005108
5109 // If D comes from an AST file, its declaration ID is already known and
5110 // fixed.
5111 if (D->isFromASTFile())
5112 return D->getGlobalID();
5113
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005114 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005115 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005116 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005117 if (DoneWritingDeclsAndTypes) {
5118 assert(0 && "New decl seen after serializing all the decls to emit!");
5119 return 0;
5120 }
5121
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005122 // We haven't seen this declaration before. Give it a new ID and
5123 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005124 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005125 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005126 }
5127
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005128 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005129}
5130
Sebastian Redl539c5062010-08-18 23:57:32 +00005131DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005132 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005133 return 0;
5134
Douglas Gregorb3163e52012-01-05 22:33:30 +00005135 // If D comes from an AST file, its declaration ID is already known and
5136 // fixed.
5137 if (D->isFromASTFile())
5138 return D->getGlobalID();
5139
Douglas Gregore84a9da2009-04-20 20:36:09 +00005140 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5141 return DeclIDs[D];
5142}
5143
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005144void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005145 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005146 assert(D);
5147
5148 SourceLocation Loc = D->getLocation();
5149 if (Loc.isInvalid())
5150 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005151
5152 // We only keep track of the file-level declarations of each file.
5153 if (!D->getLexicalDeclContext()->isFileContext())
5154 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005155 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5156 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005157 if (isa<ParmVarDecl>(D))
5158 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005159
5160 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005161 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005162 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005163 FileID FID;
5164 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005165 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005166 if (FID.isInvalid())
5167 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005168 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005169
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005170 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005171 if (!Info)
5172 Info = new DeclIDInFileInfo();
5173
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005174 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005175 LocDeclIDsTy &Decls = Info->DeclIDs;
5176
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005177 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005178 Decls.push_back(LocDecl);
5179 return;
5180 }
5181
Benjamin Kramer45025c02013-08-24 13:22:59 +00005182 LocDeclIDsTy::iterator I =
5183 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005184
5185 Decls.insert(I, LocDecl);
5186}
5187
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005188void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005189 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005190 Record.push_back(Name.getNameKind());
5191 switch (Name.getNameKind()) {
5192 case DeclarationName::Identifier:
5193 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5194 break;
5195
5196 case DeclarationName::ObjCZeroArgSelector:
5197 case DeclarationName::ObjCOneArgSelector:
5198 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005199 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005200 break;
5201
5202 case DeclarationName::CXXConstructorName:
5203 case DeclarationName::CXXDestructorName:
5204 case DeclarationName::CXXConversionFunctionName:
5205 AddTypeRef(Name.getCXXNameType(), Record);
5206 break;
5207
5208 case DeclarationName::CXXOperatorName:
5209 Record.push_back(Name.getCXXOverloadedOperator());
5210 break;
5211
Alexis Hunt3d221f22009-11-29 07:34:05 +00005212 case DeclarationName::CXXLiteralOperatorName:
5213 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5214 break;
5215
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005216 case DeclarationName::CXXUsingDirective:
5217 // No extra data to emit
5218 break;
5219 }
5220}
Chris Lattnerca025db2010-05-07 21:43:38 +00005221
Richard Smithd08aeb62014-08-28 01:33:39 +00005222unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5223 assert(needsAnonymousDeclarationNumber(D) &&
5224 "expected an anonymous declaration");
5225
5226 // Number the anonymous declarations within this context, if we've not
5227 // already done so.
5228 auto It = AnonymousDeclarationNumbers.find(D);
5229 if (It == AnonymousDeclarationNumbers.end()) {
5230 unsigned Index = 0;
5231 for (Decl *LexicalD : D->getLexicalDeclContext()->decls()) {
5232 auto *ND = dyn_cast<NamedDecl>(LexicalD);
5233 if (!ND || !needsAnonymousDeclarationNumber(ND))
5234 continue;
5235 AnonymousDeclarationNumbers[ND] = Index++;
5236 }
5237
5238 It = AnonymousDeclarationNumbers.find(D);
5239 assert(It != AnonymousDeclarationNumbers.end() &&
5240 "declaration not found within its lexical context");
5241 }
5242
5243 return It->second;
5244}
5245
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005246void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005247 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005248 switch (Name.getNameKind()) {
5249 case DeclarationName::CXXConstructorName:
5250 case DeclarationName::CXXDestructorName:
5251 case DeclarationName::CXXConversionFunctionName:
5252 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5253 break;
5254
5255 case DeclarationName::CXXOperatorName:
5256 AddSourceLocation(
5257 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5258 Record);
5259 AddSourceLocation(
5260 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5261 Record);
5262 break;
5263
5264 case DeclarationName::CXXLiteralOperatorName:
5265 AddSourceLocation(
5266 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5267 Record);
5268 break;
5269
5270 case DeclarationName::Identifier:
5271 case DeclarationName::ObjCZeroArgSelector:
5272 case DeclarationName::ObjCOneArgSelector:
5273 case DeclarationName::ObjCMultiArgSelector:
5274 case DeclarationName::CXXUsingDirective:
5275 break;
5276 }
5277}
5278
5279void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005280 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005281 AddDeclarationName(NameInfo.getName(), Record);
5282 AddSourceLocation(NameInfo.getLoc(), Record);
5283 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5284}
5285
5286void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005287 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005288 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005289 Record.push_back(Info.NumTemplParamLists);
5290 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5291 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5292}
5293
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005294void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005295 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005296 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005297 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005298 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005299
5300 // Push each of the NNS's onto a stack for serialization in reverse order.
5301 while (NNS) {
5302 NestedNames.push_back(NNS);
5303 NNS = NNS->getPrefix();
5304 }
5305
5306 Record.push_back(NestedNames.size());
5307 while(!NestedNames.empty()) {
5308 NNS = NestedNames.pop_back_val();
5309 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5310 Record.push_back(Kind);
5311 switch (Kind) {
5312 case NestedNameSpecifier::Identifier:
5313 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5314 break;
5315
5316 case NestedNameSpecifier::Namespace:
5317 AddDeclRef(NNS->getAsNamespace(), Record);
5318 break;
5319
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005320 case NestedNameSpecifier::NamespaceAlias:
5321 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5322 break;
5323
Chris Lattnerca025db2010-05-07 21:43:38 +00005324 case NestedNameSpecifier::TypeSpec:
5325 case NestedNameSpecifier::TypeSpecWithTemplate:
5326 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5327 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5328 break;
5329
5330 case NestedNameSpecifier::Global:
5331 // Don't need to write an associated value.
5332 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005333
5334 case NestedNameSpecifier::Super:
5335 AddDeclRef(NNS->getAsRecordDecl(), Record);
5336 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005337 }
5338 }
5339}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005340
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005341void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5342 RecordDataImpl &Record) {
5343 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005344 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005345 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005346
5347 // Push each of the nested-name-specifiers's onto a stack for
5348 // serialization in reverse order.
5349 while (NNS) {
5350 NestedNames.push_back(NNS);
5351 NNS = NNS.getPrefix();
5352 }
5353
5354 Record.push_back(NestedNames.size());
5355 while(!NestedNames.empty()) {
5356 NNS = NestedNames.pop_back_val();
5357 NestedNameSpecifier::SpecifierKind Kind
5358 = NNS.getNestedNameSpecifier()->getKind();
5359 Record.push_back(Kind);
5360 switch (Kind) {
5361 case NestedNameSpecifier::Identifier:
5362 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5363 AddSourceRange(NNS.getLocalSourceRange(), Record);
5364 break;
5365
5366 case NestedNameSpecifier::Namespace:
5367 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5368 AddSourceRange(NNS.getLocalSourceRange(), Record);
5369 break;
5370
5371 case NestedNameSpecifier::NamespaceAlias:
5372 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5373 AddSourceRange(NNS.getLocalSourceRange(), Record);
5374 break;
5375
5376 case NestedNameSpecifier::TypeSpec:
5377 case NestedNameSpecifier::TypeSpecWithTemplate:
5378 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5379 AddTypeLoc(NNS.getTypeLoc(), Record);
5380 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5381 break;
5382
5383 case NestedNameSpecifier::Global:
5384 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5385 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005386
5387 case NestedNameSpecifier::Super:
5388 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5389 AddSourceRange(NNS.getLocalSourceRange(), Record);
5390 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005391 }
5392 }
5393}
5394
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005395void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005396 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005397 Record.push_back(Kind);
5398 switch (Kind) {
5399 case TemplateName::Template:
5400 AddDeclRef(Name.getAsTemplateDecl(), Record);
5401 break;
5402
5403 case TemplateName::OverloadedTemplate: {
5404 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5405 Record.push_back(OvT->size());
5406 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5407 I != E; ++I)
5408 AddDeclRef(*I, Record);
5409 break;
5410 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005411
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005412 case TemplateName::QualifiedTemplate: {
5413 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5414 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5415 Record.push_back(QualT->hasTemplateKeyword());
5416 AddDeclRef(QualT->getTemplateDecl(), Record);
5417 break;
5418 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005419
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005420 case TemplateName::DependentTemplate: {
5421 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5422 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5423 Record.push_back(DepT->isIdentifier());
5424 if (DepT->isIdentifier())
5425 AddIdentifierRef(DepT->getIdentifier(), Record);
5426 else
5427 Record.push_back(DepT->getOperator());
5428 break;
5429 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005430
5431 case TemplateName::SubstTemplateTemplateParm: {
5432 SubstTemplateTemplateParmStorage *subst
5433 = Name.getAsSubstTemplateTemplateParm();
5434 AddDeclRef(subst->getParameter(), Record);
5435 AddTemplateName(subst->getReplacement(), Record);
5436 break;
5437 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005438
5439 case TemplateName::SubstTemplateTemplateParmPack: {
5440 SubstTemplateTemplateParmPackStorage *SubstPack
5441 = Name.getAsSubstTemplateTemplateParmPack();
5442 AddDeclRef(SubstPack->getParameterPack(), Record);
5443 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5444 break;
5445 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005446 }
5447}
5448
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005449void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005450 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005451 Record.push_back(Arg.getKind());
5452 switch (Arg.getKind()) {
5453 case TemplateArgument::Null:
5454 break;
5455 case TemplateArgument::Type:
5456 AddTypeRef(Arg.getAsType(), Record);
5457 break;
5458 case TemplateArgument::Declaration:
5459 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005460 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005461 break;
5462 case TemplateArgument::NullPtr:
5463 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005464 break;
5465 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005466 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005467 AddTypeRef(Arg.getIntegralType(), Record);
5468 break;
5469 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005470 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5471 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005472 case TemplateArgument::TemplateExpansion:
5473 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005474 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005475 Record.push_back(*NumExpansions + 1);
5476 else
5477 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005478 break;
5479 case TemplateArgument::Expression:
5480 AddStmt(Arg.getAsExpr());
5481 break;
5482 case TemplateArgument::Pack:
5483 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005484 for (const auto &P : Arg.pack_elements())
5485 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005486 break;
5487 }
5488}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005489
5490void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005491ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005492 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005493 assert(TemplateParams && "No TemplateParams!");
5494 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5495 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5496 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5497 Record.push_back(TemplateParams->size());
5498 for (TemplateParameterList::const_iterator
5499 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5500 P != PEnd; ++P)
5501 AddDeclRef(*P, Record);
5502}
5503
5504/// \brief Emit a template argument list.
5505void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005506ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005507 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005508 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005509 Record.push_back(TemplateArgs->size());
5510 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005511 AddTemplateArgument(TemplateArgs->get(i), Record);
5512}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005513
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005514void
5515ASTWriter::AddASTTemplateArgumentListInfo
5516(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5517 assert(ASTTemplArgList && "No ASTTemplArgList!");
5518 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5519 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5520 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5521 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5522 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5523 AddTemplateArgumentLoc(TemplArgs[i], Record);
5524}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005525
5526void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005527ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005528 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005529 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005530 I = Set.begin(), E = Set.end(); I != E; ++I) {
5531 AddDeclRef(I.getDecl(), Record);
5532 Record.push_back(I.getAccess());
5533 }
5534}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005535
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005536void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005537 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005538 Record.push_back(Base.isVirtual());
5539 Record.push_back(Base.isBaseOfClass());
5540 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005541 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005542 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005543 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005544 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5545 : SourceLocation(),
5546 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005547}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005548
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005549void ASTWriter::FlushCXXBaseSpecifiers() {
5550 RecordData Record;
5551 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5552 Record.clear();
5553
5554 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005555 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005556 if (Index == CXXBaseSpecifiersOffsets.size())
5557 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5558 else {
5559 if (Index > CXXBaseSpecifiersOffsets.size())
5560 CXXBaseSpecifiersOffsets.resize(Index + 1);
5561 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5562 }
5563
5564 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5565 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5566 Record.push_back(BEnd - B);
5567 for (; B != BEnd; ++B)
5568 AddCXXBaseSpecifier(*B, Record);
5569 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005570
5571 // Flush any expressions that were written as part of the base specifiers.
5572 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005573 }
5574
5575 CXXBaseSpecifiersToWrite.clear();
5576}
5577
Alexis Hunt1d792652011-01-08 20:30:50 +00005578void ASTWriter::AddCXXCtorInitializers(
5579 const CXXCtorInitializer * const *CtorInitializers,
5580 unsigned NumCtorInitializers,
5581 RecordDataImpl &Record) {
5582 Record.push_back(NumCtorInitializers);
5583 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5584 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005585
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005586 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005587 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005588 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005589 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005590 } else if (Init->isDelegatingInitializer()) {
5591 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005592 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005593 } else if (Init->isMemberInitializer()){
5594 Record.push_back(CTOR_INITIALIZER_MEMBER);
5595 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005596 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005597 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5598 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005599 }
Francois Pichetd583da02010-12-04 09:14:42 +00005600
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005601 AddSourceLocation(Init->getMemberLocation(), Record);
5602 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005603 AddSourceLocation(Init->getLParenLoc(), Record);
5604 AddSourceLocation(Init->getRParenLoc(), Record);
5605 Record.push_back(Init->isWritten());
5606 if (Init->isWritten()) {
5607 Record.push_back(Init->getSourceOrder());
5608 } else {
5609 Record.push_back(Init->getNumArrayIndices());
5610 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5611 AddDeclRef(Init->getArrayIndex(i), Record);
5612 }
5613 }
5614}
5615
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005616void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005617 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005618 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005619 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005620 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005621 Record.push_back(Data.Aggregate);
5622 Record.push_back(Data.PlainOldData);
5623 Record.push_back(Data.Empty);
5624 Record.push_back(Data.Polymorphic);
5625 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005626 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005627 Record.push_back(Data.HasNoNonEmptyBases);
5628 Record.push_back(Data.HasPrivateFields);
5629 Record.push_back(Data.HasProtectedFields);
5630 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005631 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005632 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005633 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005634 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005635 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005636 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5637 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5638 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5639 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5640 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5641 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005642 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005643 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005644 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005645 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005646 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005647 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005648 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005649 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005650 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005651 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005652 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5653 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5654 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5655 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005656 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005657
5658 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005659 if (Data.NumBases > 0)
5660 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5661 Record);
5662
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005663 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5664 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005665 if (Data.NumVBases > 0)
5666 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5667 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005668
Richard Smitha4ba74c2013-08-30 04:46:40 +00005669 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5670 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005671 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005672 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005673
5674 // Add lambda-specific data.
5675 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005676 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005677 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005678 Record.push_back(Lambda.IsGenericLambda);
5679 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005680 Record.push_back(Lambda.NumCaptures);
5681 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005682 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005683 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005684 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005685 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005686 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005687 AddSourceLocation(Capture.getLocation(), Record);
5688 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005689 Record.push_back(Capture.getCaptureKind());
5690 switch (Capture.getCaptureKind()) {
5691 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005692 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005693 break;
5694 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005695 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005696 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005697 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005698 AddDeclRef(Var, Record);
5699 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5700 : SourceLocation(),
5701 Record);
5702 break;
5703 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005704 }
5705 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005706}
5707
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005708void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005709 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005710 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005711 assert(FirstDeclID == NextDeclID &&
5712 FirstTypeID == NextTypeID &&
5713 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005714 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005715 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005716 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005717 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005718
Sebastian Redl07a89a82010-07-30 00:29:29 +00005719 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005720
Douglas Gregordf0c1512011-08-18 04:12:04 +00005721 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5722 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5723 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005724 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005725 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005726 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005727 NextDeclID = FirstDeclID;
5728 NextTypeID = FirstTypeID;
5729 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005730 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005731 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005732 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005733}
5734
Sebastian Redl539c5062010-08-18 23:57:32 +00005735void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005736 // Always keep the highest ID. See \p TypeRead() for more information.
5737 IdentID &StoredID = IdentifierIDs[II];
5738 if (ID > StoredID)
5739 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005740}
5741
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005742void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005743 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005744 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005745 if (ID > StoredID)
5746 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005747}
5748
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005749void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005750 // Always take the highest-numbered type index. This copes with an interesting
5751 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005752 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005753 // keep the higher-numbered entry so that we can properly write it out to
5754 // the AST file.
5755 TypeIdx &StoredIdx = TypeIdxs[T];
5756 if (Idx.getIndex() >= StoredIdx.getIndex())
5757 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005758}
5759
Sebastian Redl539c5062010-08-18 23:57:32 +00005760void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005761 // Always keep the highest ID. See \p TypeRead() for more information.
5762 SelectorID &StoredID = SelectorIDs[S];
5763 if (ID > StoredID)
5764 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005765}
Douglas Gregor91096292010-10-02 19:29:26 +00005766
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005767void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005768 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005769 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005770 MacroDefinitions[MD] = ID;
5771}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005772
Douglas Gregore37a85a2011-12-02 17:30:13 +00005773void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5774 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5775 SubmoduleIDs[Mod] = ID;
5776}
5777
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005778void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005779 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005780 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005781 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5782 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005783 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005784 // A forward reference was mutated into a definition. Rewrite it.
5785 // FIXME: This happens during template instantiation, should we
5786 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005787 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5788 "completed a tag from another module but not by instantiation?");
5789 DeclUpdates[RD].push_back(
5790 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005791 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005792 }
5793}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005794
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005795void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5796 // TU and namespaces are handled elsewhere.
5797 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5798 return;
5799
Douglas Gregorb3722e22011-09-09 23:01:35 +00005800 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005801 return; // Not a source decl added to a DeclContext from PCH.
5802
Douglas Gregor9f782892013-01-21 15:25:38 +00005803 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005804 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005805 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005806 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005807}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005808
5809void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5810 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005811 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005812 return; // Not a source member added to a class from PCH.
5813 if (!isa<CXXMethodDecl>(D))
5814 return; // We are interested in lazily declared implicit methods.
5815
5816 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005817 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005818 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005819 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005820}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005821
5822void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5823 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005824 // The specializations set is kept in the canonical template.
5825 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005826 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005827 return; // Not a source specialization added to a template from PCH.
5828
Richard Smithe9a8bc32014-09-30 00:45:29 +00005829 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005830 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5831 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005832}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005833
Larisse Voufo39a1e502013-08-06 01:03:05 +00005834void ASTWriter::AddedCXXTemplateSpecialization(
5835 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5836 // The specializations set is kept in the canonical template.
Larisse Voufo39a1e502013-08-06 01:03:05 +00005837 TD = TD->getCanonicalDecl();
5838 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5839 return; // Not a source specialization added to a template from PCH.
5840
Richard Smithe9a8bc32014-09-30 00:45:29 +00005841 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005842 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5843 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005844}
5845
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005846void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5847 const FunctionDecl *D) {
5848 // The specializations set is kept in the canonical template.
5849 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005850 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005851 return; // Not a source specialization added to a template from PCH.
5852
Richard Smithe9a8bc32014-09-30 00:45:29 +00005853 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005854 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5855 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005856}
5857
Richard Smith564417a2014-03-20 21:47:22 +00005858void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5859 assert(!WritingAST && "Already writing the AST!");
5860 FD = FD->getCanonicalDecl();
5861 if (!FD->isFromASTFile())
5862 return; // Not a function declared in PCH and defined outside.
5863
5864 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5865}
5866
Richard Smith1fa5d642013-05-11 05:45:24 +00005867void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5868 assert(!WritingAST && "Already writing the AST!");
5869 FD = FD->getCanonicalDecl();
5870 if (!FD->isFromASTFile())
5871 return; // Not a function declared in PCH and defined outside.
5872
Aaron Ballman4f45b712014-03-21 15:22:56 +00005873 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith1fa5d642013-05-11 05:45:24 +00005874}
5875
Sebastian Redlab238a72011-04-24 16:28:06 +00005876void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005877 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005878 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005879 return; // Declaration not imported from PCH.
5880
Richard Smith4d235792014-08-07 18:53:08 +00005881 // Implicit function decl from a PCH was defined.
5882 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005883}
5884
Richard Smithd28ac5b2014-03-22 23:33:22 +00005885void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5886 assert(!WritingAST && "Already writing the AST!");
5887 if (!D->isFromASTFile())
5888 return;
5889
Richard Smithd28ac5b2014-03-22 23:33:22 +00005890 DeclUpdates[D].push_back(
Richard Smith4d235792014-08-07 18:53:08 +00005891 DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005892}
5893
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005894void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005895 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005896 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005897 return;
5898
5899 // Since the actual instantiation is delayed, this really means that we need
5900 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005901 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005902 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5903 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005904}
5905
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005906void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5907 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005908 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005909 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005910 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005911
5912 assert(IFD->getDefinition() && "Category on a class without a definition?");
5913 ObjCClassesWithCategories.insert(
5914 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005915}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005916
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005917
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005918void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5919 const ObjCPropertyDecl *OrigProp,
5920 const ObjCCategoryDecl *ClassExt) {
5921 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5922 if (!D)
5923 return;
5924
5925 assert(!WritingAST && "Already writing the AST!");
5926 if (!D->isFromASTFile())
5927 return; // Declaration not imported from PCH.
5928
5929 RewriteDecl(D);
5930}
Eli Friedman276dd182013-09-05 00:02:25 +00005931
5932void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5933 assert(!WritingAST && "Already writing the AST!");
5934 if (!D->isFromASTFile())
5935 return;
5936
Aaron Ballman4f45b712014-03-21 15:22:56 +00005937 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005938}
Alexey Bataev97720002014-11-11 04:05:39 +00005939
5940void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5941 assert(!WritingAST && "Already writing the AST!");
5942 if (!D->isFromASTFile())
5943 return;
5944
5945 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5946}