blob: cb25fe6f886f0a7c467159420009a4a3d315be86 [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"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000027#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000048#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000049#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000050#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000051#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000052#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000053#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000054#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000055#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000056#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000058using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000059
Sebastian Redl3df5a082010-07-30 17:03:48 +000060template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000061static StringRef data(const std::vector<T, Allocator> &v) {
62 if (v.empty()) return StringRef();
63 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000065}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000066
67template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000068static StringRef data(const SmallVectorImpl<T> &v) {
69 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000070 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000071}
72
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073//===----------------------------------------------------------------------===//
74// Type serialization
75//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000076
Douglas Gregoref84c4b2009-04-09 22:27:44 +000077namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000078 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000079 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000080 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000081
82 public:
83 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000084 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000085
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000086 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000087 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088
89 void VisitArrayType(const ArrayType *T);
90 void VisitFunctionType(const FunctionType *T);
91 void VisitTagType(const TagType *T);
92
93#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
94#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095#include "clang/AST/TypeNodes.def"
96 };
97}
98
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000099void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000100 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101}
102
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000105 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106}
107
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000110 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111}
112
Reid Kleckner8a365022013-06-24 17:51:48 +0000113void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
114 Writer.AddTypeRef(T->getOriginalType(), Record);
115 Code = TYPE_DECAYED;
116}
117
Reid Kleckner0503a872013-12-05 01:23:43 +0000118void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
119 Writer.AddTypeRef(T->getOriginalType(), Record);
120 Writer.AddTypeRef(T->getAdjustedType(), Record);
121 Code = TYPE_ADJUSTED;
122}
123
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000126 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000127}
128
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000130 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
131 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000132 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133}
134
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000136 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000137 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000138}
139
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000141 Writer.AddTypeRef(T->getPointeeType(), Record);
142 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 Writer.AddTypeRef(T->getElementType(), Record);
148 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000149 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150}
151
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000152void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153 VisitArrayType(T);
154 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000155 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156}
157
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000160 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161}
162
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000165 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
166 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000167 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000168 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169}
170
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000172 Writer.AddTypeRef(T->getElementType(), Record);
173 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000174 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000175 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176}
177
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000180 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181}
182
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000184 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000185 FunctionType::ExtInfo C = T->getExtInfo();
186 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000187 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000188 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000189 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000190 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000191 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192}
193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Richard Smith564417a2014-03-20 21:47:22 +0000199static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
200 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000201 Record.push_back(T->getExceptionSpecType());
202 if (T->getExceptionSpecType() == EST_Dynamic) {
203 Record.push_back(T->getNumExceptions());
204 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
205 Writer.AddTypeRef(T->getExceptionType(I), Record);
206 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
207 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000208 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
209 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
210 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000211 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
212 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000213 }
Richard Smith564417a2014-03-20 21:47:22 +0000214}
215
216void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
217 VisitFunctionType(T);
218 Record.push_back(T->getNumParams());
219 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
220 Writer.AddTypeRef(T->getParamType(I), Record);
221 Record.push_back(T->isVariadic());
222 Record.push_back(T->hasTrailingReturn());
223 Record.push_back(T->getTypeQuals());
224 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
225 addExceptionSpec(Writer, T, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000226 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227}
228
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000230 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000231 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000232}
John McCallb96ec562009-12-04 22:46:56 +0000233
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000236 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
237 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000238 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239}
240
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000242 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000243 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244}
245
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000246void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000247 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000248 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249}
250
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000251void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000252 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000253 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000254 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000255}
256
Alexis Hunte852b102011-05-24 22:41:36 +0000257void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
258 Writer.AddTypeRef(T->getBaseType(), Record);
259 Writer.AddTypeRef(T->getUnderlyingType(), Record);
260 Record.push_back(T->getUTTKind());
261 Code = TYPE_UNARY_TRANSFORM;
262}
263
Richard Smith30482bc2011-02-20 03:19:35 +0000264void ASTTypeWriter::VisitAutoType(const AutoType *T) {
265 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000266 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000267 if (T->getDeducedType().isNull())
268 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000269 Code = TYPE_AUTO;
270}
271
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000273 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000274 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000275 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000276 "Cannot serialize in the middle of a type definition");
277}
278
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000279void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000280 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000281 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000282}
283
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000284void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000285 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000286 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000287}
288
John McCall81904512011-01-06 01:58:22 +0000289void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
290 Writer.AddTypeRef(T->getModifiedType(), Record);
291 Writer.AddTypeRef(T->getEquivalentType(), Record);
292 Record.push_back(T->getAttrKind());
293 Code = TYPE_ATTRIBUTED;
294}
295
Mike Stump11289f42009-09-09 15:08:12 +0000296void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000297ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000298 const SubstTemplateTypeParmType *T) {
299 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
300 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000301 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000302}
303
304void
Douglas Gregorada4b792011-01-14 02:55:32 +0000305ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
306 const SubstTemplateTypeParmPackType *T) {
307 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
308 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
309 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
310}
311
312void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000313ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000314 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000315 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000316 Writer.AddTemplateName(T->getTemplateName(), Record);
317 Record.push_back(T->getNumArgs());
318 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
319 ArgI != ArgE; ++ArgI)
320 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000321 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
322 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000323 : T->getCanonicalTypeInternal(),
324 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000325 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000326}
327
328void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000329ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000330 VisitArrayType(T);
331 Writer.AddStmt(T->getSizeExpr());
332 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000333 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000334}
335
336void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000337ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000338 const DependentSizedExtVectorType *T) {
339 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000340 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000341}
342
343void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000344ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000345 Record.push_back(T->getDepth());
346 Record.push_back(T->getIndex());
347 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000348 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000349 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000350}
351
352void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000353ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000354 Record.push_back(T->getKeyword());
355 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
356 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000357 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
358 : T->getCanonicalTypeInternal(),
359 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000360 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000361}
362
363void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000364ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000365 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000366 Record.push_back(T->getKeyword());
367 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
368 Writer.AddIdentifierRef(T->getIdentifier(), Record);
369 Record.push_back(T->getNumArgs());
370 for (DependentTemplateSpecializationType::iterator
371 I = T->begin(), E = T->end(); I != E; ++I)
372 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000373 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000374}
375
Douglas Gregord2fa7662010-12-20 02:24:11 +0000376void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
377 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000378 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000379 Record.push_back(*NumExpansions + 1);
380 else
381 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000382 Code = TYPE_PACK_EXPANSION;
383}
384
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000385void ASTTypeWriter::VisitParenType(const ParenType *T) {
386 Writer.AddTypeRef(T->getInnerType(), Record);
387 Code = TYPE_PAREN;
388}
389
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000390void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000391 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000392 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
393 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000394 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000395}
396
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000397void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000398 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000399 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000400 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000401}
402
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000403void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000404 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000405 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000406}
407
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000408void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000409 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000410 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000411 for (const auto *I : T->quals())
412 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000413 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000414}
415
Steve Narofffb4330f2009-06-17 22:40:22 +0000416void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000417ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000418 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000419 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000420}
421
Eli Friedman0dfb8892011-10-06 23:00:33 +0000422void
423ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
424 Writer.AddTypeRef(T->getValueType(), Record);
425 Code = TYPE_ATOMIC;
426}
427
John McCall8f115c62009-10-16 21:56:05 +0000428namespace {
429
430class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000431 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000432 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000433
434public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000435 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000436 : Writer(Writer), Record(Record) { }
437
John McCall17001972009-10-18 01:05:36 +0000438#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000439#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000440 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000441#include "clang/AST/TypeLocNodes.def"
442
John McCall17001972009-10-18 01:05:36 +0000443 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
444 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000445};
446
447}
448
John McCall17001972009-10-18 01:05:36 +0000449void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
450 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000451}
John McCall17001972009-10-18 01:05:36 +0000452void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000453 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
454 if (TL.needsExtraLocalData()) {
455 Record.push_back(TL.getWrittenTypeSpec());
456 Record.push_back(TL.getWrittenSignSpec());
457 Record.push_back(TL.getWrittenWidthSpec());
458 Record.push_back(TL.hasModeAttr());
459 }
John McCall8f115c62009-10-16 21:56:05 +0000460}
John McCall17001972009-10-18 01:05:36 +0000461void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
462 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000463}
John McCall17001972009-10-18 01:05:36 +0000464void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
465 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000466}
Reid Kleckner8a365022013-06-24 17:51:48 +0000467void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
468 // nothing to do
469}
Reid Kleckner0503a872013-12-05 01:23:43 +0000470void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
471 // nothing to do
472}
John McCall17001972009-10-18 01:05:36 +0000473void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000475}
John McCall17001972009-10-18 01:05:36 +0000476void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000478}
John McCall17001972009-10-18 01:05:36 +0000479void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000481}
John McCall17001972009-10-18 01:05:36 +0000482void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000484 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000485}
John McCall17001972009-10-18 01:05:36 +0000486void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
488 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
489 Record.push_back(TL.getSizeExpr() ? 1 : 0);
490 if (TL.getSizeExpr())
491 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000492}
John McCall17001972009-10-18 01:05:36 +0000493void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
494 VisitArrayTypeLoc(TL);
495}
496void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
497 VisitArrayTypeLoc(TL);
498}
499void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
500 VisitArrayTypeLoc(TL);
501}
502void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
503 DependentSizedArrayTypeLoc TL) {
504 VisitArrayTypeLoc(TL);
505}
506void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
507 DependentSizedExtVectorTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
511 Writer.AddSourceLocation(TL.getNameLoc(), Record);
512}
513void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
516void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000517 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000518 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
519 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000520 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000521 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
522 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000523}
524void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
525 VisitFunctionTypeLoc(TL);
526}
527void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
528 VisitFunctionTypeLoc(TL);
529}
John McCallb96ec562009-12-04 22:46:56 +0000530void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
John McCall17001972009-10-18 01:05:36 +0000533void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
536void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000537 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
538 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
539 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000540}
541void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000542 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
543 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
544 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
545 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000546}
547void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
Alexis Hunte852b102011-05-24 22:41:36 +0000550void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getKWLoc(), Record);
552 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
553 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
554 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
555}
Richard Smith30482bc2011-02-20 03:19:35 +0000556void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getNameLoc(), Record);
558}
John McCall17001972009-10-18 01:05:36 +0000559void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561}
562void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getNameLoc(), Record);
564}
John McCall81904512011-01-06 01:58:22 +0000565void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
567 if (TL.hasAttrOperand()) {
568 SourceRange range = TL.getAttrOperandParensRange();
569 Writer.AddSourceLocation(range.getBegin(), Record);
570 Writer.AddSourceLocation(range.getEnd(), Record);
571 }
572 if (TL.hasAttrExprOperand()) {
573 Expr *operand = TL.getAttrExprOperand();
574 Record.push_back(operand ? 1 : 0);
575 if (operand) Writer.AddStmt(operand);
576 } else if (TL.hasAttrEnumOperand()) {
577 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
578 }
579}
John McCall17001972009-10-18 01:05:36 +0000580void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
John McCallcebee162009-10-18 09:09:24 +0000583void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
584 SubstTemplateTypeParmTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getNameLoc(), Record);
586}
Douglas Gregorada4b792011-01-14 02:55:32 +0000587void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
588 SubstTemplateTypeParmPackTypeLoc TL) {
589 Writer.AddSourceLocation(TL.getNameLoc(), Record);
590}
John McCall17001972009-10-18 01:05:36 +0000591void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
592 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000594 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
595 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
596 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
597 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000598 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
599 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000600}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000601void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
602 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
603 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
604}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000605void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000606 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000607 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000608}
John McCalle78aac42010-03-10 03:28:59 +0000609void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
610 Writer.AddSourceLocation(TL.getNameLoc(), Record);
611}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000612void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000613 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000614 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000615 Writer.AddSourceLocation(TL.getNameLoc(), Record);
616}
John McCallc392f372010-06-11 00:33:02 +0000617void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
618 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000619 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000620 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000621 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000622 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000623 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
624 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
625 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000626 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
627 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000628}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000629void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
630 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
631}
John McCall17001972009-10-18 01:05:36 +0000632void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
633 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000634}
635void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
636 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000637 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
638 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
639 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
640 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000641}
John McCallfc93cf92009-10-22 22:37:11 +0000642void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
643 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000644}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000645void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
646 Writer.AddSourceLocation(TL.getKWLoc(), Record);
647 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
648 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
649}
John McCall8f115c62009-10-16 21:56:05 +0000650
Chris Lattner19cea4e2009-04-22 05:57:30 +0000651//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000652// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000653//===----------------------------------------------------------------------===//
654
Chris Lattner28fa4e62009-04-26 22:26:21 +0000655static void EmitBlockID(unsigned ID, const char *Name,
656 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000657 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000658 Record.clear();
659 Record.push_back(ID);
660 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
661
662 // Emit the block name if present.
663 if (Name == 0 || Name[0] == 0) return;
664 Record.clear();
665 while (*Name)
666 Record.push_back(*Name++);
667 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
668}
669
670static void EmitRecordID(unsigned ID, const char *Name,
671 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000672 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000673 Record.clear();
674 Record.push_back(ID);
675 while (*Name)
676 Record.push_back(*Name++);
677 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000678}
679
680static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000681 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000682#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000683 RECORD(STMT_STOP);
684 RECORD(STMT_NULL_PTR);
685 RECORD(STMT_NULL);
686 RECORD(STMT_COMPOUND);
687 RECORD(STMT_CASE);
688 RECORD(STMT_DEFAULT);
689 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000690 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000691 RECORD(STMT_IF);
692 RECORD(STMT_SWITCH);
693 RECORD(STMT_WHILE);
694 RECORD(STMT_DO);
695 RECORD(STMT_FOR);
696 RECORD(STMT_GOTO);
697 RECORD(STMT_INDIRECT_GOTO);
698 RECORD(STMT_CONTINUE);
699 RECORD(STMT_BREAK);
700 RECORD(STMT_RETURN);
701 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000702 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000703 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000704 RECORD(EXPR_PREDEFINED);
705 RECORD(EXPR_DECL_REF);
706 RECORD(EXPR_INTEGER_LITERAL);
707 RECORD(EXPR_FLOATING_LITERAL);
708 RECORD(EXPR_IMAGINARY_LITERAL);
709 RECORD(EXPR_STRING_LITERAL);
710 RECORD(EXPR_CHARACTER_LITERAL);
711 RECORD(EXPR_PAREN);
712 RECORD(EXPR_UNARY_OPERATOR);
713 RECORD(EXPR_SIZEOF_ALIGN_OF);
714 RECORD(EXPR_ARRAY_SUBSCRIPT);
715 RECORD(EXPR_CALL);
716 RECORD(EXPR_MEMBER);
717 RECORD(EXPR_BINARY_OPERATOR);
718 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
719 RECORD(EXPR_CONDITIONAL_OPERATOR);
720 RECORD(EXPR_IMPLICIT_CAST);
721 RECORD(EXPR_CSTYLE_CAST);
722 RECORD(EXPR_COMPOUND_LITERAL);
723 RECORD(EXPR_EXT_VECTOR_ELEMENT);
724 RECORD(EXPR_INIT_LIST);
725 RECORD(EXPR_DESIGNATED_INIT);
726 RECORD(EXPR_IMPLICIT_VALUE_INIT);
727 RECORD(EXPR_VA_ARG);
728 RECORD(EXPR_ADDR_LABEL);
729 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000730 RECORD(EXPR_CHOOSE);
731 RECORD(EXPR_GNU_NULL);
732 RECORD(EXPR_SHUFFLE_VECTOR);
733 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000734 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000735 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000736 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000737 RECORD(EXPR_OBJC_ARRAY_LITERAL);
738 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000739 RECORD(EXPR_OBJC_ENCODE);
740 RECORD(EXPR_OBJC_SELECTOR_EXPR);
741 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
742 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
743 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
744 RECORD(EXPR_OBJC_KVC_REF_EXPR);
745 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000746 RECORD(STMT_OBJC_FOR_COLLECTION);
747 RECORD(STMT_OBJC_CATCH);
748 RECORD(STMT_OBJC_FINALLY);
749 RECORD(STMT_OBJC_AT_TRY);
750 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
751 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000752 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000753 RECORD(EXPR_CXX_OPERATOR_CALL);
754 RECORD(EXPR_CXX_CONSTRUCT);
755 RECORD(EXPR_CXX_STATIC_CAST);
756 RECORD(EXPR_CXX_DYNAMIC_CAST);
757 RECORD(EXPR_CXX_REINTERPRET_CAST);
758 RECORD(EXPR_CXX_CONST_CAST);
759 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000760 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000761 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000762 RECORD(EXPR_CXX_BOOL_LITERAL);
763 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000764 RECORD(EXPR_CXX_TYPEID_EXPR);
765 RECORD(EXPR_CXX_TYPEID_TYPE);
766 RECORD(EXPR_CXX_UUIDOF_EXPR);
767 RECORD(EXPR_CXX_UUIDOF_TYPE);
768 RECORD(EXPR_CXX_THIS);
769 RECORD(EXPR_CXX_THROW);
770 RECORD(EXPR_CXX_DEFAULT_ARG);
771 RECORD(EXPR_CXX_BIND_TEMPORARY);
772 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
773 RECORD(EXPR_CXX_NEW);
774 RECORD(EXPR_CXX_DELETE);
775 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
776 RECORD(EXPR_EXPR_WITH_CLEANUPS);
777 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
778 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
779 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
780 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
781 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000782 RECORD(EXPR_CXX_NOEXCEPT);
783 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000784 RECORD(EXPR_PACK_EXPANSION);
785 RECORD(EXPR_SIZEOF_PACK);
786 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000787 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000788#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000789}
Mike Stump11289f42009-09-09 15:08:12 +0000790
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000791void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000792 RecordData Record;
793 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000794
Sebastian Redl539c5062010-08-18 23:57:32 +0000795#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
796#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000797
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000798 // Control Block.
799 BLOCK(CONTROL_BLOCK);
800 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000801 RECORD(MODULE_NAME);
802 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000803 RECORD(IMPORTS);
804 RECORD(LANGUAGE_OPTIONS);
805 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000806 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000807 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000808 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000809 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000810 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000811 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000812 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000813 RECORD(PREPROCESSOR_OPTIONS);
814
Douglas Gregor108cb222012-10-19 00:45:00 +0000815 BLOCK(INPUT_FILES_BLOCK);
816 RECORD(INPUT_FILE);
817
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000818 // AST Top-Level Block.
819 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000820 RECORD(TYPE_OFFSET);
821 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000822 RECORD(IDENTIFIER_OFFSET);
823 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000824 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000825 RECORD(SPECIAL_TYPES);
826 RECORD(STATISTICS);
827 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000828 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000829 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000830 RECORD(SELECTOR_OFFSETS);
831 RECORD(METHOD_POOL);
832 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000833 RECORD(SOURCE_LOCATION_OFFSETS);
834 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000835 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000836 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000837 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000838 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000839 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000840 RECORD(SEMA_DECL_REFS);
841 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
842 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
843 RECORD(DECL_REPLACEMENTS);
844 RECORD(UPDATE_VISIBLE);
845 RECORD(DECL_UPDATE_OFFSETS);
846 RECORD(DECL_UPDATES);
847 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
848 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000849 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000850 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000851 RECORD(FP_PRAGMA_OPTIONS);
852 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000853 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000854 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000855 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000856 RECORD(MODULE_OFFSET_MAP);
857 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000858 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000859 RECORD(FILE_SORTED_DECLS);
860 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000861 RECORD(MERGED_DECLARATIONS);
862 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000863 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000864 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000865 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000866 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000867
Chris Lattner28fa4e62009-04-26 22:26:21 +0000868 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000869 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000870 RECORD(SM_SLOC_FILE_ENTRY);
871 RECORD(SM_SLOC_BUFFER_ENTRY);
872 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000873 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000874
Chris Lattner28fa4e62009-04-26 22:26:21 +0000875 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000876 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000877 RECORD(PP_MACRO_OBJECT_LIKE);
878 RECORD(PP_MACRO_FUNCTION_LIKE);
879 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000880
Douglas Gregor12bfa382009-10-17 00:13:19 +0000881 // Decls and Types block.
882 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000883 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000884 RECORD(TYPE_COMPLEX);
885 RECORD(TYPE_POINTER);
886 RECORD(TYPE_BLOCK_POINTER);
887 RECORD(TYPE_LVALUE_REFERENCE);
888 RECORD(TYPE_RVALUE_REFERENCE);
889 RECORD(TYPE_MEMBER_POINTER);
890 RECORD(TYPE_CONSTANT_ARRAY);
891 RECORD(TYPE_INCOMPLETE_ARRAY);
892 RECORD(TYPE_VARIABLE_ARRAY);
893 RECORD(TYPE_VECTOR);
894 RECORD(TYPE_EXT_VECTOR);
895 RECORD(TYPE_FUNCTION_PROTO);
896 RECORD(TYPE_FUNCTION_NO_PROTO);
897 RECORD(TYPE_TYPEDEF);
898 RECORD(TYPE_TYPEOF_EXPR);
899 RECORD(TYPE_TYPEOF);
900 RECORD(TYPE_RECORD);
901 RECORD(TYPE_ENUM);
902 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000903 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000904 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000905 RECORD(TYPE_DECLTYPE);
906 RECORD(TYPE_ELABORATED);
907 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
908 RECORD(TYPE_UNRESOLVED_USING);
909 RECORD(TYPE_INJECTED_CLASS_NAME);
910 RECORD(TYPE_OBJC_OBJECT);
911 RECORD(TYPE_TEMPLATE_TYPE_PARM);
912 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
913 RECORD(TYPE_DEPENDENT_NAME);
914 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
915 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
916 RECORD(TYPE_PAREN);
917 RECORD(TYPE_PACK_EXPANSION);
918 RECORD(TYPE_ATTRIBUTED);
919 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000920 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000921 RECORD(DECL_TYPEDEF);
922 RECORD(DECL_ENUM);
923 RECORD(DECL_RECORD);
924 RECORD(DECL_ENUM_CONSTANT);
925 RECORD(DECL_FUNCTION);
926 RECORD(DECL_OBJC_METHOD);
927 RECORD(DECL_OBJC_INTERFACE);
928 RECORD(DECL_OBJC_PROTOCOL);
929 RECORD(DECL_OBJC_IVAR);
930 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000931 RECORD(DECL_OBJC_CATEGORY);
932 RECORD(DECL_OBJC_CATEGORY_IMPL);
933 RECORD(DECL_OBJC_IMPLEMENTATION);
934 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
935 RECORD(DECL_OBJC_PROPERTY);
936 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000937 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000938 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000939 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000940 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000941 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000942 RECORD(DECL_FILE_SCOPE_ASM);
943 RECORD(DECL_BLOCK);
944 RECORD(DECL_CONTEXT_LEXICAL);
945 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000946 RECORD(DECL_NAMESPACE);
947 RECORD(DECL_NAMESPACE_ALIAS);
948 RECORD(DECL_USING);
949 RECORD(DECL_USING_SHADOW);
950 RECORD(DECL_USING_DIRECTIVE);
951 RECORD(DECL_UNRESOLVED_USING_VALUE);
952 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
953 RECORD(DECL_LINKAGE_SPEC);
954 RECORD(DECL_CXX_RECORD);
955 RECORD(DECL_CXX_METHOD);
956 RECORD(DECL_CXX_CONSTRUCTOR);
957 RECORD(DECL_CXX_DESTRUCTOR);
958 RECORD(DECL_CXX_CONVERSION);
959 RECORD(DECL_ACCESS_SPEC);
960 RECORD(DECL_FRIEND);
961 RECORD(DECL_FRIEND_TEMPLATE);
962 RECORD(DECL_CLASS_TEMPLATE);
963 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
964 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000965 RECORD(DECL_VAR_TEMPLATE);
966 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
967 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000968 RECORD(DECL_FUNCTION_TEMPLATE);
969 RECORD(DECL_TEMPLATE_TYPE_PARM);
970 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
971 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
972 RECORD(DECL_STATIC_ASSERT);
973 RECORD(DECL_CXX_BASE_SPECIFIERS);
974 RECORD(DECL_INDIRECTFIELD);
975 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
976
Douglas Gregor03412ba2011-06-03 02:27:19 +0000977 // Statements and Exprs can occur in the Decls and Types block.
978 AddStmtsExprs(Stream, Record);
979
Douglas Gregor92a96f52011-02-08 21:58:10 +0000980 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000981 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000982 RECORD(PPD_MACRO_DEFINITION);
983 RECORD(PPD_INCLUSION_DIRECTIVE);
984
Chris Lattner28fa4e62009-04-26 22:26:21 +0000985#undef RECORD
986#undef BLOCK
987 Stream.ExitBlock();
988}
989
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000990/// \brief Adjusts the given filename to only write out the portion of the
991/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000992///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000993/// \param Filename the file name to adjust.
994///
995/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
996/// the returned filename will be adjusted by this system root.
997///
998/// \returns either the original filename (if it needs no adjustment) or the
999/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001000static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +00001001adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001002 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001003
Douglas Gregorc567ba22011-07-22 16:35:34 +00001004 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001005 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001006
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001007 // Verify that the filename and the system root have the same prefix.
1008 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001009 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001010 if (Filename[Pos] != isysroot[Pos])
1011 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001012
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001013 // We hit the end of the filename before we hit the end of the system root.
1014 if (!Filename[Pos])
1015 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001016
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001017 // If the file name has a '/' at the current position, skip over the '/'.
1018 // We distinguish sysroot-based includes from absolute includes by the
1019 // absence of '/' at the beginning of sysroot-based includes.
1020 if (Filename[Pos] == '/')
1021 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001022
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001023 return Filename + Pos;
1024}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001025
Douglas Gregor112b9072012-10-18 05:31:06 +00001026/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001027void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1028 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001029 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001030 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001031 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1032 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001033
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001034 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001035 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1036 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1037 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1038 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1039 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1040 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1041 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1042 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1043 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1044 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1045 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001046 Record.push_back(VERSION_MAJOR);
1047 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001048 Record.push_back(CLANG_VERSION_MAJOR);
1049 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001050 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001051 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001052 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1053 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001054
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001055 // Module name
1056 if (WritingModule) {
1057 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1058 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1060 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1061 RecordData Record;
1062 Record.push_back(MODULE_NAME);
1063 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1064 }
1065
1066 // Module map file
1067 if (WritingModule) {
1068 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1069 Abbrev->Add(BitCodeAbbrevOp(MODULE_MAP_FILE));
1070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Filename
1071 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1072
1073 assert(WritingModule->ModuleMap && "missing module map");
1074 SmallString<128> ModuleMap(WritingModule->ModuleMap->getName());
1075 llvm::sys::fs::make_absolute(ModuleMap);
1076 RecordData Record;
1077 Record.push_back(MODULE_MAP_FILE);
1078 Stream.EmitRecordWithBlob(AbbrevCode, Record, ModuleMap.str());
1079 }
1080
Douglas Gregor112b9072012-10-18 05:31:06 +00001081 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001082 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001083 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001084 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001085
1086 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1087 M != MEnd; ++M) {
1088 // Skip modules that weren't directly imported.
1089 if (!(*M)->isDirectlyImported())
1090 continue;
1091
1092 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001093 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001094 Record.push_back((*M)->File->getSize());
1095 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001096 const std::string &FileName = (*M)->FileName;
1097 Record.push_back(FileName.size());
1098 Record.append(FileName.begin(), FileName.end());
1099 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001100 Stream.EmitRecord(IMPORTS, Record);
1101 }
Mike Stump11289f42009-09-09 15:08:12 +00001102
Douglas Gregor112b9072012-10-18 05:31:06 +00001103 // Language options.
1104 Record.clear();
1105 const LangOptions &LangOpts = Context.getLangOpts();
1106#define LANGOPT(Name, Bits, Default, Description) \
1107 Record.push_back(LangOpts.Name);
1108#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1109 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1110#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001111#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1112#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001113
1114 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1115 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1116
1117 Record.push_back(LangOpts.CurrentModule.size());
1118 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001119
1120 // Comment options.
1121 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1122 for (CommentOptions::BlockCommandNamesTy::const_iterator
1123 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1124 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1125 I != IEnd; ++I) {
1126 AddString(*I, Record);
1127 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001128 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001129
Douglas Gregor112b9072012-10-18 05:31:06 +00001130 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1131
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001132 // Target options.
1133 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001134 const TargetInfo &Target = Context.getTargetInfo();
1135 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001136 AddString(TargetOpts.Triple, Record);
1137 AddString(TargetOpts.CPU, Record);
1138 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001139 AddString(TargetOpts.LinkerVersion, Record);
1140 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1141 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1142 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1143 }
1144 Record.push_back(TargetOpts.Features.size());
1145 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1146 AddString(TargetOpts.Features[I], Record);
1147 }
1148 Stream.EmitRecord(TARGET_OPTIONS, Record);
1149
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001150 // Diagnostic options.
1151 Record.clear();
1152 const DiagnosticOptions &DiagOpts
1153 = Context.getDiagnostics().getDiagnosticOptions();
1154#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1155#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1156 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1157#include "clang/Basic/DiagnosticOptions.def"
1158 Record.push_back(DiagOpts.Warnings.size());
1159 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1160 AddString(DiagOpts.Warnings[I], Record);
1161 // Note: we don't serialize the log or serialization file names, because they
1162 // are generally transient files and will almost always be overridden.
1163 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1164
Douglas Gregorc6317db2012-10-24 15:49:58 +00001165 // File system options.
1166 Record.clear();
1167 const FileSystemOptions &FSOpts
1168 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1169 AddString(FSOpts.WorkingDir, Record);
1170 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1171
Douglas Gregor2d302362012-10-24 16:50:34 +00001172 // Header search options.
1173 Record.clear();
1174 const HeaderSearchOptions &HSOpts
1175 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1176 AddString(HSOpts.Sysroot, Record);
1177
1178 // Include entries.
1179 Record.push_back(HSOpts.UserEntries.size());
1180 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1181 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1182 AddString(Entry.Path, Record);
1183 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001184 Record.push_back(Entry.IsFramework);
1185 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001186 }
1187
1188 // System header prefixes.
1189 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1190 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1191 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1192 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1193 }
1194
1195 AddString(HSOpts.ResourceDir, Record);
1196 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001197 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001198 Record.push_back(HSOpts.DisableModuleHash);
1199 Record.push_back(HSOpts.UseBuiltinIncludes);
1200 Record.push_back(HSOpts.UseStandardSystemIncludes);
1201 Record.push_back(HSOpts.UseStandardCXXIncludes);
1202 Record.push_back(HSOpts.UseLibcxx);
1203 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1204
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001205 // Preprocessor options.
1206 Record.clear();
1207 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1208
1209 // Macro definitions.
1210 Record.push_back(PPOpts.Macros.size());
1211 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1212 AddString(PPOpts.Macros[I].first, Record);
1213 Record.push_back(PPOpts.Macros[I].second);
1214 }
1215
1216 // Includes
1217 Record.push_back(PPOpts.Includes.size());
1218 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1219 AddString(PPOpts.Includes[I], Record);
1220
1221 // Macro includes
1222 Record.push_back(PPOpts.MacroIncludes.size());
1223 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1224 AddString(PPOpts.MacroIncludes[I], Record);
1225
Douglas Gregorb6368752012-10-24 23:41:50 +00001226 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001227 // Detailed record is important since it is used for the module cache hash.
1228 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001229 AddString(PPOpts.ImplicitPCHInclude, Record);
1230 AddString(PPOpts.ImplicitPTHInclude, Record);
1231 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1232 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1233
Douglas Gregora3b20262011-05-06 21:43:30 +00001234 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001235 SourceManager &SM = Context.getSourceManager();
1236 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1237 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001238 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1239 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001240 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1241 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1242
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001243 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001244
Michael J. Spencer740857f2010-12-21 16:45:57 +00001245 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001246
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001247 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001248 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001249 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001250 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001251 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001252 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001253 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001254 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001255
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001256 Record.clear();
1257 Record.push_back(SM.getMainFileID().getOpaqueValue());
1258 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1259
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001260 // Original PCH directory
1261 if (!OutputFile.empty() && OutputFile != "-") {
1262 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1263 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1265 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1266
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001267 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001268
1269 llvm::sys::fs::make_absolute(OutputPath);
1270 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1271
1272 RecordData Record;
1273 Record.push_back(ORIGINAL_PCH_DIR);
1274 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1275 }
1276
Douglas Gregor49491f72013-03-15 22:15:07 +00001277 WriteInputFiles(Context.SourceMgr,
1278 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001279 isysroot,
1280 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001281 Stream.ExitBlock();
1282}
1283
Douglas Gregor49491f72013-03-15 22:15:07 +00001284namespace {
1285 /// \brief An input file.
1286 struct InputFileEntry {
1287 const FileEntry *File;
1288 bool IsSystemFile;
1289 bool BufferOverridden;
1290 };
1291}
1292
1293void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1294 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001295 StringRef isysroot,
1296 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001297 using namespace llvm;
1298 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1299 RecordData Record;
1300
1301 // Create input-file abbreviation.
1302 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1303 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001304 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001305 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1306 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001307 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001308 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1309 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1310
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001311 // Get all ContentCache objects for files, sorted by whether the file is a
1312 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001313 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001314 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1315 // Get this source location entry.
1316 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001317 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001318
1319 // We only care about file entries that were not overridden.
1320 if (!SLoc->isFile())
1321 continue;
1322 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001323 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001324 continue;
1325
Douglas Gregor49491f72013-03-15 22:15:07 +00001326 InputFileEntry Entry;
1327 Entry.File = Cache->OrigEntry;
1328 Entry.IsSystemFile = Cache->IsSystemFile;
1329 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001330 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001331 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001332 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001333 SortedFiles.push_front(Entry);
1334 }
1335
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001336 unsigned UserFilesNum = 0;
1337 // Write out all of the input files.
1338 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001339 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001340 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001341 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001342
Douglas Gregor49491f72013-03-15 22:15:07 +00001343 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001344 if (InputFileID != 0)
1345 continue; // already recorded this file.
1346
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001347 // Record this entry's offset.
1348 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001349
1350 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001351
Douglas Gregor49491f72013-03-15 22:15:07 +00001352 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001353 ++UserFilesNum;
1354
Douglas Gregor72be3902012-10-19 00:38:02 +00001355 Record.clear();
1356 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001357 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001358
1359 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001360 Record.push_back(Entry.File->getSize());
1361 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001362
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001363 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001364 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001365
Douglas Gregor72be3902012-10-19 00:38:02 +00001366 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001367 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001368 SmallString<128> FilePath(Filename);
1369
1370 // Ask the file manager to fixup the relative path for us. This will
1371 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001372 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001373
1374 // FIXME: This call to make_absolute shouldn't be necessary, the
1375 // call to FixupRelativePath should always return an absolute path.
1376 llvm::sys::fs::make_absolute(FilePath);
1377 Filename = FilePath.c_str();
1378
1379 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1380
1381 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1382 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001383
Douglas Gregor112b9072012-10-18 05:31:06 +00001384 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001385
1386 // Create input file offsets abbreviation.
1387 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1388 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1389 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001390 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1391 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001392 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1393 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1394
1395 // Write input file offsets.
1396 Record.clear();
1397 Record.push_back(INPUT_FILE_OFFSETS);
1398 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001399 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001400 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001401}
1402
Douglas Gregora7f71a92009-04-10 03:52:48 +00001403//===----------------------------------------------------------------------===//
1404// Source Manager Serialization
1405//===----------------------------------------------------------------------===//
1406
1407/// \brief Create an abbreviation for the SLocEntry that refers to a
1408/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001409static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001410 using namespace llvm;
1411 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001412 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001417 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1421 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001422 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001423}
1424
1425/// \brief Create an abbreviation for the SLocEntry that refers to a
1426/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001427static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001428 using namespace llvm;
1429 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001430 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1433 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1434 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1435 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001436 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001437}
1438
1439/// \brief Create an abbreviation for the SLocEntry that refers to a
1440/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001441static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001442 using namespace llvm;
1443 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001444 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001446 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001447}
1448
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001449/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1450/// expansion.
1451static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001452 using namespace llvm;
1453 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001454 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001460 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001461}
1462
Douglas Gregor09b69892011-02-10 17:09:37 +00001463namespace {
1464 // Trait used for the on-disk hash table of header search information.
1465 class HeaderFileInfoTrait {
1466 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001467 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001468
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001469 // Keep track of the framework names we've used during serialization.
1470 SmallVector<char, 128> FrameworkStringData;
1471 llvm::StringMap<unsigned> FrameworkNameOffset;
1472
Douglas Gregor09b69892011-02-10 17:09:37 +00001473 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001474 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1475 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001476
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001477 struct key_type {
1478 const FileEntry *FE;
1479 const char *Filename;
1480 };
1481 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001482
1483 typedef HeaderFileInfo data_type;
1484 typedef const data_type &data_type_ref;
1485
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001486 static unsigned ComputeHash(key_type_ref key) {
1487 // The hash is based only on size/time of the file, so that the reader can
1488 // match even when symlinking or excess path elements ("foo/../", "../")
1489 // change the form of the name. However, complete path is still the key.
1490 return llvm::hash_combine(key.FE->getSize(),
1491 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001492 }
1493
1494 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001495 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001496 using namespace llvm::support;
1497 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001498 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001499 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001500 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001501 if (Data.isModuleHeader)
1502 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001503 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001504 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001505 }
1506
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001507 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001508 using namespace llvm::support;
1509 endian::Writer<little> LE(Out);
1510 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001511 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001512 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001513 KeyLen -= 8;
1514 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001515 }
1516
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001517 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001518 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001519 using namespace llvm::support;
1520 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001521 uint64_t Start = Out.tell(); (void)Start;
1522
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001523 unsigned char Flags = (Data.HeaderRole << 6)
1524 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001525 | (Data.isPragmaOnce << 4)
1526 | (Data.DirInfo << 2)
1527 | (Data.Resolved << 1)
1528 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001529 LE.write<uint8_t>(Flags);
1530 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001531
1532 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001533 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001534 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001535 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001536
1537 unsigned Offset = 0;
1538 if (!Data.Framework.empty()) {
1539 // If this header refers into a framework, save the framework name.
1540 llvm::StringMap<unsigned>::iterator Pos
1541 = FrameworkNameOffset.find(Data.Framework);
1542 if (Pos == FrameworkNameOffset.end()) {
1543 Offset = FrameworkStringData.size() + 1;
1544 FrameworkStringData.append(Data.Framework.begin(),
1545 Data.Framework.end());
1546 FrameworkStringData.push_back(0);
1547
1548 FrameworkNameOffset[Data.Framework] = Offset;
1549 } else
1550 Offset = Pos->second;
1551 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001552 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001553
1554 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001555 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001556 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001557 }
1558
Douglas Gregor09b69892011-02-10 17:09:37 +00001559 assert(Out.tell() - Start == DataLen && "Wrong data length");
1560 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001561
1562 const char *strings_begin() const { return FrameworkStringData.begin(); }
1563 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001564 };
1565} // end anonymous namespace
1566
1567/// \brief Write the header search block for the list of files that
1568///
1569/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001570void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001571 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001572 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1573
1574 if (FilesByUID.size() > HS.header_file_size())
1575 FilesByUID.resize(HS.header_file_size());
1576
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001577 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001578 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001579 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001580 unsigned NumHeaderSearchEntries = 0;
1581 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1582 const FileEntry *File = FilesByUID[UID];
1583 if (!File)
1584 continue;
1585
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001586 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1587 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001588 HeaderFileInfo HFI;
1589 if (!HS.tryGetFileInfo(File, HFI) ||
1590 (HFI.External && Chain) ||
1591 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001592 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001593
1594 // Turn the file name into an absolute path, if it isn't already.
1595 const char *Filename = File->getName();
1596 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1597
1598 // If we performed any translation on the file name at all, we need to
1599 // save this string, since the generator will refer to it later.
1600 if (Filename != File->getName()) {
1601 Filename = strdup(Filename);
1602 SavedStrings.push_back(Filename);
1603 }
1604
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001605 HeaderFileInfoTrait::key_type key = { File, Filename };
1606 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001607 ++NumHeaderSearchEntries;
1608 }
1609
1610 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001611 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001612 uint32_t BucketOffset;
1613 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001614 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001615 llvm::raw_svector_ostream Out(TableData);
1616 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001617 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001618 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1619 }
1620
1621 // Create a blob abbreviation
1622 using namespace llvm;
1623 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1624 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1629 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1630
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001631 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001632 RecordData Record;
1633 Record.push_back(HEADER_SEARCH_TABLE);
1634 Record.push_back(BucketOffset);
1635 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001636 Record.push_back(TableData.size());
1637 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001638 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1639
1640 // Free all of the strings we had to duplicate.
1641 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001642 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001643}
1644
Douglas Gregora7f71a92009-04-10 03:52:48 +00001645/// \brief Writes the block containing the serialized form of the
1646/// source manager.
1647///
1648/// TODO: We should probably use an on-disk hash table (stored in a
1649/// blob), indexed based on the file name, so that we only create
1650/// entries for files that we actually need. In the common case (no
1651/// errors), we probably won't have to create file entries for any of
1652/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001653void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001654 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001655 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001656 RecordData Record;
1657
Chris Lattner0910e3b2009-04-10 17:16:57 +00001658 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001659 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001660
1661 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001662 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1663 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1664 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001665 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001666
Douglas Gregor258ae542009-04-27 06:38:32 +00001667 // Write out the source location entry table. We skip the first
1668 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001669 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001670 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001671 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1672 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001673 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001674 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001675 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001676 FileID FID = FileID::get(I);
1677 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001678
Douglas Gregor258ae542009-04-27 06:38:32 +00001679 // Record the offset of this source-location entry.
1680 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1681
1682 // Figure out which record code to use.
1683 unsigned Code;
1684 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001685 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1686 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001687 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001688 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001689 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001690 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001691 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001692 Record.clear();
1693 Record.push_back(Code);
1694
Douglas Gregor925296b2011-07-19 16:10:42 +00001695 // Starting offset of this entry within this module, so skip the dummy.
1696 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001697 if (SLoc->isFile()) {
1698 const SrcMgr::FileInfo &File = SLoc->getFile();
1699 Record.push_back(File.getIncludeLoc().getRawEncoding());
1700 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1701 Record.push_back(File.hasLineDirectives());
1702
1703 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001704 if (Content->OrigEntry) {
1705 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001706 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001707
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001708 // The source location entry is a file. Emit input file ID.
1709 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1710 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001711
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001712 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001713
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001714 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001715 if (FDI != FileDeclIDs.end()) {
1716 Record.push_back(FDI->second->FirstDeclIndex);
1717 Record.push_back(FDI->second->DeclIDs.size());
1718 } else {
1719 Record.push_back(0);
1720 Record.push_back(0);
1721 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001722
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001723 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001724
1725 if (Content->BufferOverridden) {
1726 Record.clear();
1727 Record.push_back(SM_SLOC_BUFFER_BLOB);
1728 const llvm::MemoryBuffer *Buffer
1729 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1730 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1731 StringRef(Buffer->getBufferStart(),
1732 Buffer->getBufferSize() + 1));
1733 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001734 } else {
1735 // The source location entry is a buffer. The blob associated
1736 // with this entry contains the contents of the buffer.
1737
1738 // We add one to the size so that we capture the trailing NULL
1739 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1740 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001741 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001742 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001743 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001744 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001745 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001746 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001747 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001748 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001749 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001750 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001751
Douglas Gregor925296b2011-07-19 16:10:42 +00001752 if (strcmp(Name, "<built-in>") == 0) {
1753 PreloadSLocs.push_back(SLocEntryOffsets.size());
1754 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001755 }
1756 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001757 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001758 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001759 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1760 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001761 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1762 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001763
1764 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001765 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001766 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001767 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001768 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001769 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001770 }
1771 }
1772
Douglas Gregor8f45df52009-04-16 22:23:12 +00001773 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001774
1775 if (SLocEntryOffsets.empty())
1776 return;
1777
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001778 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001779 // table is used for lazily loading source-location information.
1780 using namespace llvm;
1781 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001782 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001784 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001785 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1786 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001787
Douglas Gregor258ae542009-04-27 06:38:32 +00001788 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001789 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001790 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001791 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001792 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001793
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001794 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001795 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001796 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001797
1798 // Write the line table. It depends on remapping working, so it must come
1799 // after the source location offsets.
1800 if (SourceMgr.hasLineTable()) {
1801 LineTableInfo &LineTable = SourceMgr.getLineTable();
1802
1803 Record.clear();
1804 // Emit the file names
1805 Record.push_back(LineTable.getNumFilenames());
1806 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1807 // Emit the file name
1808 const char *Filename = LineTable.getFilename(I);
1809 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1810 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1811 Record.push_back(FilenameLen);
1812 if (FilenameLen)
1813 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1814 }
1815
1816 // Emit the line entries
1817 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1818 L != LEnd; ++L) {
1819 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001820 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001821 continue;
1822
1823 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001824 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001825
1826 // Emit the line entries
1827 Record.push_back(L->second.size());
1828 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1829 LEEnd = L->second.end();
1830 LE != LEEnd; ++LE) {
1831 Record.push_back(LE->FileOffset);
1832 Record.push_back(LE->LineNo);
1833 Record.push_back(LE->FilenameID);
1834 Record.push_back((unsigned)LE->FileKind);
1835 Record.push_back(LE->IncludeOffset);
1836 }
1837 }
1838 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1839 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001840}
1841
Douglas Gregorc5046832009-04-27 18:38:38 +00001842//===----------------------------------------------------------------------===//
1843// Preprocessor Serialization
1844//===----------------------------------------------------------------------===//
1845
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001846namespace {
1847class ASTMacroTableTrait {
1848public:
1849 typedef IdentID key_type;
1850 typedef key_type key_type_ref;
1851
1852 struct Data {
1853 uint32_t MacroDirectivesOffset;
1854 };
1855
1856 typedef Data data_type;
1857 typedef const data_type &data_type_ref;
1858
1859 static unsigned ComputeHash(IdentID IdID) {
1860 return llvm::hash_value(IdID);
1861 }
1862
1863 std::pair<unsigned,unsigned>
1864 static EmitKeyDataLength(raw_ostream& Out,
1865 key_type_ref Key, data_type_ref Data) {
1866 unsigned KeyLen = 4; // IdentID.
1867 unsigned DataLen = 4; // MacroDirectivesOffset.
1868 return std::make_pair(KeyLen, DataLen);
1869 }
1870
1871 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001872 using namespace llvm::support;
1873 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001874 }
1875
1876 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1877 unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001878 using namespace llvm::support;
1879 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001880 }
1881};
1882} // end anonymous namespace
1883
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001884static int compareMacroDirectives(
1885 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1886 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1887 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001888}
1889
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001890static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1891 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001892 if (MacroInfo *MI = MD->getMacroInfo())
1893 if (MI->isBuiltinMacro())
1894 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001895
1896 if (IsModule) {
1897 SourceLocation Loc = MD->getLocation();
1898 if (Loc.isInvalid())
1899 return true;
1900 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1901 return true;
1902 }
1903
1904 return false;
1905}
1906
Chris Lattnereeffaef2009-04-10 17:15:23 +00001907/// \brief Writes the block containing the serialized form of the
1908/// preprocessor.
1909///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001910void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001911 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1912 if (PPRec)
1913 WritePreprocessorDetail(*PPRec);
1914
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001915 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001916
Chris Lattner0af3ba12009-04-13 01:29:17 +00001917 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1918 if (PP.getCounterValue() != 0) {
1919 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001920 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001921 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001922 }
1923
1924 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001925 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001926
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001927 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001928 // FIXME: use diagnostics subsystem for localization etc.
1929 if (PP.SawDateOrTime())
1930 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001931
Douglas Gregor796d76a2010-10-20 22:00:55 +00001932
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001933 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001934 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001935
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001936 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001937 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001938 MacroDirectives;
1939 for (Preprocessor::macro_iterator
1940 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1941 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001942 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001943 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001944 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001945
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001946 // Sort the set of macro definitions that need to be serialized by the
1947 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001948 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1949 &compareMacroDirectives);
1950
Justin Bognerbb094f02014-04-18 19:57:06 +00001951 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001952
1953 // Emit the macro directives as a list and associate the offset with the
1954 // identifier they belong to.
1955 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1956 const IdentifierInfo *Name = MacroDirectives[I].first;
1957 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1958 MacroDirective *MD = MacroDirectives[I].second;
1959
1960 // If the macro or identifier need no updates, don't write the macro history
1961 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001962 // FIXME: Chain the macro history instead of re-writing it.
1963 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001964 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1965 continue;
1966
1967 // Emit the macro directives in reverse source order.
1968 for (; MD; MD = MD->getPrevious()) {
1969 if (shouldIgnoreMacro(MD, IsModule, PP))
1970 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001971
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001972 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001973 Record.push_back(MD->getKind());
1974 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1975 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1976 Record.push_back(InfoID);
1977 Record.push_back(DefMD->isImported());
1978 Record.push_back(DefMD->isAmbiguous());
1979
1980 } else if (VisibilityMacroDirective *
1981 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1982 Record.push_back(VisMD->isPublic());
1983 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001984 }
1985 if (Record.empty())
1986 continue;
1987
1988 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1989 Record.clear();
1990
1991 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1992
1993 IdentID NameID = getIdentifierRef(Name);
1994 ASTMacroTableTrait::Data data;
1995 data.MacroDirectivesOffset = MacroDirectiveOffset;
1996 Generator.insert(NameID, data);
1997 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001998
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001999 /// \brief Offsets of each of the macros into the bitstream, indexed by
2000 /// the local macro ID
2001 ///
2002 /// For each identifier that is associated with a macro, this map
2003 /// provides the offset into the bitstream where that macro is
2004 /// defined.
2005 std::vector<uint32_t> MacroOffsets;
2006
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002007 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2008 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2009 MacroInfo *MI = MacroInfosToEmit[I].MI;
2010 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002011
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002012 if (ID < FirstMacroID) {
2013 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2014 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002015 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002016
2017 // Record the local offset of this macro.
2018 unsigned Index = ID - FirstMacroID;
2019 if (Index == MacroOffsets.size())
2020 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2021 else {
2022 if (Index > MacroOffsets.size())
2023 MacroOffsets.resize(Index + 1);
2024
2025 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2026 }
2027
2028 AddIdentifierRef(Name, Record);
2029 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2030 AddSourceLocation(MI->getDefinitionLoc(), Record);
2031 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2032 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002033 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002034 unsigned Code;
2035 if (MI->isObjectLike()) {
2036 Code = PP_MACRO_OBJECT_LIKE;
2037 } else {
2038 Code = PP_MACRO_FUNCTION_LIKE;
2039
2040 Record.push_back(MI->isC99Varargs());
2041 Record.push_back(MI->isGNUVarargs());
2042 Record.push_back(MI->hasCommaPasting());
2043 Record.push_back(MI->getNumArgs());
2044 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2045 I != E; ++I)
2046 AddIdentifierRef(*I, Record);
2047 }
2048
2049 // If we have a detailed preprocessing record, record the macro definition
2050 // ID that corresponds to this macro.
2051 if (PPRec)
2052 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2053
2054 Stream.EmitRecord(Code, Record);
2055 Record.clear();
2056
2057 // Emit the tokens array.
2058 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2059 // Note that we know that the preprocessor does not have any annotation
2060 // tokens in it because they are created by the parser, and thus can't
2061 // be in a macro definition.
2062 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002063 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002064 Stream.EmitRecord(PP_TOKEN, Record);
2065 Record.clear();
2066 }
2067 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002068 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002069
Douglas Gregor92a96f52011-02-08 21:58:10 +00002070 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002071
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002072 // Create the on-disk hash table in a buffer.
2073 SmallString<4096> MacroTable;
2074 uint32_t BucketOffset;
2075 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002076 using namespace llvm::support;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002077 llvm::raw_svector_ostream Out(MacroTable);
2078 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002079 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002080 BucketOffset = Generator.Emit(Out);
2081 }
2082
2083 // Write the macro table
2084 using namespace llvm;
2085 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2086 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2089 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2090
2091 Record.push_back(MACRO_TABLE);
2092 Record.push_back(BucketOffset);
2093 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2094 Record.clear();
2095
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002096 // Write the offsets table for macro IDs.
2097 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002098 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002099 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2102 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2103
2104 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2105 Record.clear();
2106 Record.push_back(MACRO_OFFSET);
2107 Record.push_back(MacroOffsets.size());
2108 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2109 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2110 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002111}
2112
2113void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002114 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002115 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002116
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002117 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002118
Douglas Gregor92a96f52011-02-08 21:58:10 +00002119 // Enter the preprocessor block.
2120 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002121
Douglas Gregoraae92242010-03-19 21:51:54 +00002122 // If the preprocessor has a preprocessing record, emit it.
2123 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002124 using namespace llvm;
2125
2126 // Set up the abbreviation for
2127 unsigned InclusionAbbrev = 0;
2128 {
2129 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2130 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002134 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2136 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2137 }
2138
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002139 unsigned FirstPreprocessorEntityID
2140 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2141 + NUM_PREDEF_PP_ENTITY_IDS;
2142 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002143 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002144 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2145 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002146 E != EEnd;
2147 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002148 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002149
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002150 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2151 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002152
Douglas Gregor92a96f52011-02-08 21:58:10 +00002153 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002154 // Record this macro definition's ID.
2155 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002156
Douglas Gregor92a96f52011-02-08 21:58:10 +00002157 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002158 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2159 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002160 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002161
Chandler Carrutha88a22182011-07-14 08:20:46 +00002162 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002163 Record.push_back(ME->isBuiltinMacro());
2164 if (ME->isBuiltinMacro())
2165 AddIdentifierRef(ME->getName(), Record);
2166 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002167 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002168 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002169 continue;
2170 }
2171
2172 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2173 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002174 Record.push_back(ID->getFileName().size());
2175 Record.push_back(ID->wasInQuotes());
2176 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002177 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002178 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002179 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002180 // Check that the FileEntry is not null because it was not resolved and
2181 // we create a PCH even with compiler errors.
2182 if (ID->getFile())
2183 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002184 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2185 continue;
2186 }
2187
2188 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2189 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002190 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002191
Douglas Gregoraae92242010-03-19 21:51:54 +00002192 // Write the offsets table for the preprocessing record.
2193 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002194 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2195
Douglas Gregoraae92242010-03-19 21:51:54 +00002196 // Write the offsets table for identifier IDs.
2197 using namespace llvm;
2198 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002199 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002202 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002203
Douglas Gregoraae92242010-03-19 21:51:54 +00002204 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002205 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002206 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002207 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2208 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002209 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002210}
2211
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002212unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2213 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2214 if (Known != SubmoduleIDs.end())
2215 return Known->second;
2216
2217 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2218}
2219
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002220unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2221 if (!Mod)
2222 return 0;
2223
2224 llvm::DenseMap<Module *, unsigned>::const_iterator
2225 Known = SubmoduleIDs.find(Mod);
2226 if (Known != SubmoduleIDs.end())
2227 return Known->second;
2228
2229 return 0;
2230}
2231
Douglas Gregor253eefe2011-12-01 00:59:36 +00002232/// \brief Compute the number of modules within the given tree (including the
2233/// given module).
2234static unsigned getNumberOfModules(Module *Mod) {
2235 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002236 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2237 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002238 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002239 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002240
2241 return ChildModules + 1;
2242}
2243
Douglas Gregorde3ef502011-11-30 23:21:26 +00002244void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002245 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002246 // FIXME: This feels like it belongs somewhere else, but there are no
2247 // other consumers of this information.
2248 SourceManager &SrcMgr = PP->getSourceManager();
2249 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002250 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002251 if (Module *ImportedFrom
2252 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2253 SrcMgr))) {
2254 ImportedFrom->Imports.push_back(I->getImportedModule());
2255 }
2256 }
2257
Douglas Gregor69021972011-11-30 17:33:56 +00002258 // Enter the submodule description block.
2259 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2260
2261 // Write the abbreviations needed for the submodules block.
2262 using namespace llvm;
2263 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2264 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2276 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2277
2278 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002279 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2281 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2282
2283 Abbrev = new BitCodeAbbrev();
2284 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2286 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002287
2288 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002289 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2291 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2292
2293 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002294 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2296 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2297
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002298 Abbrev = new BitCodeAbbrev();
2299 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002302 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2303
Douglas Gregor59527662012-10-15 06:28:11 +00002304 Abbrev = new BitCodeAbbrev();
2305 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2307 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2308
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002309 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002310 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2312 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2313
2314 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002315 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2318 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2319
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002320 Abbrev = new BitCodeAbbrev();
2321 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2323 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2324
Douglas Gregorfb912652013-03-20 21:10:35 +00002325 Abbrev = new BitCodeAbbrev();
2326 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2329 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2330
Douglas Gregor253eefe2011-12-01 00:59:36 +00002331 // Write the submodule metadata block.
2332 RecordData Record;
2333 Record.push_back(getNumberOfModules(WritingModule));
2334 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2335 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2336
Douglas Gregor69021972011-11-30 17:33:56 +00002337 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002338 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002339 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002340 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002341 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002342 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002343 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002344
2345 // Emit the definition of the block.
2346 Record.clear();
2347 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002348 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002349 if (Mod->Parent) {
2350 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2351 Record.push_back(SubmoduleIDs[Mod->Parent]);
2352 } else {
2353 Record.push_back(0);
2354 }
2355 Record.push_back(Mod->IsFramework);
2356 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002357 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002358 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002359 Record.push_back(Mod->InferSubmodules);
2360 Record.push_back(Mod->InferExplicitSubmodules);
2361 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002362 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002363 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2364
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002365 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002366 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002367 Record.clear();
2368 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002369 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002370 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002371 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002372 }
2373
Douglas Gregor69021972011-11-30 17:33:56 +00002374 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002375 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002376 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002377 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002378 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002379 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002380 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2381 Record.clear();
2382 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2383 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2384 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002385 }
2386
2387 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002388 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002389 Record.clear();
2390 Record.push_back(SUBMODULE_HEADER);
2391 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002392 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002393 }
Douglas Gregor59527662012-10-15 06:28:11 +00002394 // Emit the excluded headers.
2395 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2396 Record.clear();
2397 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2398 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2399 Mod->ExcludedHeaders[I]->getName());
2400 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002401 // Emit the private headers.
2402 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2403 Record.clear();
2404 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2405 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2406 Mod->PrivateHeaders[I]->getName());
2407 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002408 ArrayRef<const FileEntry *>
2409 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2410 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002411 Record.clear();
2412 Record.push_back(SUBMODULE_TOPHEADER);
2413 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002414 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002415 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002416
2417 // Emit the imports.
2418 if (!Mod->Imports.empty()) {
2419 Record.clear();
2420 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002421 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002422 assert(ImportedID && "Unknown submodule!");
2423 Record.push_back(ImportedID);
2424 }
2425 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2426 }
2427
Douglas Gregor24bb9232011-12-02 18:58:38 +00002428 // Emit the exports.
2429 if (!Mod->Exports.empty()) {
2430 Record.clear();
2431 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002432 if (Module *Exported = Mod->Exports[I].getPointer()) {
2433 unsigned ExportedID = SubmoduleIDs[Exported];
2434 assert(ExportedID > 0 && "Unknown submodule ID?");
2435 Record.push_back(ExportedID);
2436 } else {
2437 Record.push_back(0);
2438 }
2439
Douglas Gregor24bb9232011-12-02 18:58:38 +00002440 Record.push_back(Mod->Exports[I].getInt());
2441 }
2442 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2443 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002444
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002445 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2446 // Might be unnecessary as use declarations are only used to build the
2447 // module itself.
2448
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002449 // Emit the link libraries.
2450 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2451 Record.clear();
2452 Record.push_back(SUBMODULE_LINK_LIBRARY);
2453 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2454 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2455 Mod->LinkLibraries[I].Library);
2456 }
2457
Douglas Gregorfb912652013-03-20 21:10:35 +00002458 // Emit the conflicts.
2459 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2460 Record.clear();
2461 Record.push_back(SUBMODULE_CONFLICT);
2462 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2463 assert(OtherID && "Unknown submodule!");
2464 Record.push_back(OtherID);
2465 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2466 Mod->Conflicts[I].Message);
2467 }
2468
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002469 // Emit the configuration macros.
2470 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2471 Record.clear();
2472 Record.push_back(SUBMODULE_CONFIG_MACRO);
2473 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2474 Mod->ConfigMacros[I]);
2475 }
2476
Douglas Gregor69021972011-11-30 17:33:56 +00002477 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002478 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2479 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002480 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002481 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002482 }
2483
2484 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002485
2486 assert((NextSubmoduleID - FirstSubmoduleID
2487 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002488}
2489
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002490serialization::SubmoduleID
2491ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002492 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002493 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002494
2495 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002496 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002497 Module *OwningMod
2498 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002499 if (!OwningMod)
2500 return 0;
2501
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002502 // Check whether this submodule is part of our own module.
2503 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002504 return 0;
2505
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002506 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002507}
2508
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002509void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2510 bool isModule) {
2511 // Make sure set diagnostic pragmas don't affect the translation unit that
2512 // imports the module.
2513 // FIXME: Make diagnostic pragma sections work properly with modules.
2514 if (isModule)
2515 return;
2516
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002517 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2518 DiagStateIDMap;
2519 unsigned CurrID = 0;
2520 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002521 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002522 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002523 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2524 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002525 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002526 if (point.Loc.isInvalid())
2527 continue;
2528
2529 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002530 unsigned &DiagStateID = DiagStateIDMap[point.State];
2531 Record.push_back(DiagStateID);
2532
2533 if (DiagStateID == 0) {
2534 DiagStateID = ++CurrID;
2535 for (DiagnosticsEngine::DiagState::const_iterator
2536 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2537 if (I->second.isPragma()) {
2538 Record.push_back(I->first);
2539 Record.push_back(I->second.getMapping());
2540 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002541 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002542 Record.push_back(-1); // mark the end of the diag/map pairs for this
2543 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002544 }
2545 }
2546
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002547 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002548 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002549}
2550
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002551void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2552 if (CXXBaseSpecifiersOffsets.empty())
2553 return;
2554
2555 RecordData Record;
2556
2557 // Create a blob abbreviation for the C++ base specifiers offsets.
2558 using namespace llvm;
2559
2560 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2561 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2564 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2565
Douglas Gregorc27b2872011-08-04 00:01:48 +00002566 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002567 Record.clear();
2568 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2569 Record.push_back(CXXBaseSpecifiersOffsets.size());
2570 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002571 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002572}
2573
Douglas Gregorc5046832009-04-27 18:38:38 +00002574//===----------------------------------------------------------------------===//
2575// Type Serialization
2576//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002577
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002578/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002579void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002580 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002581 if (Idx.getIndex() == 0) // we haven't seen this type before.
2582 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002583
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002584 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002585
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002586 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002587 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002588 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002589 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002590 else if (TypeOffsets.size() < Index) {
2591 TypeOffsets.resize(Index + 1);
2592 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002593 }
2594
2595 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002596
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002597 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002598 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002599
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002600 if (T.hasLocalNonFastQualifiers()) {
2601 Qualifiers Qs = T.getLocalQualifiers();
2602 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002603 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002604 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002605 } else {
2606 switch (T->getTypeClass()) {
2607 // For all of the concrete, non-dependent types, call the
2608 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002609#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002610 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002611#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002612#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002613 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002614 }
2615
2616 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002617 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002618
2619 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002620 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002621}
2622
Douglas Gregorc5046832009-04-27 18:38:38 +00002623//===----------------------------------------------------------------------===//
2624// Declaration Serialization
2625//===----------------------------------------------------------------------===//
2626
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002627/// \brief Write the block containing all of the declaration IDs
2628/// lexically declared within the given DeclContext.
2629///
2630/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2631/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002632uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002633 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002634 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002635 return 0;
2636
Douglas Gregor8f45df52009-04-16 22:23:12 +00002637 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002638 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002639 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002640 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002641 for (const auto *D : DC->decls())
2642 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002643
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002644 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002645 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002646 return Offset;
2647}
2648
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002649void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002650 using namespace llvm;
2651 RecordData Record;
2652
2653 // Write the type offsets array
2654 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002655 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002657 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002658 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2659 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2660 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002661 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002662 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002663 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002664 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002665
2666 // Write the declaration offsets array
2667 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002668 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002670 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002671 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2672 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2673 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002674 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002675 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002676 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002677 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002678}
2679
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002680void ASTWriter::WriteFileDeclIDsMap() {
2681 using namespace llvm;
2682 RecordData Record;
2683
2684 // Join the vectors of DeclIDs from all files.
2685 SmallVector<DeclID, 256> FileSortedIDs;
2686 for (FileDeclIDsTy::iterator
2687 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2688 DeclIDInFileInfo &Info = *FI->second;
2689 Info.FirstDeclIndex = FileSortedIDs.size();
2690 for (LocDeclIDsTy::iterator
2691 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2692 FileSortedIDs.push_back(DI->second);
2693 }
2694
2695 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2696 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002697 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002698 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2699 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2700 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002701 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002702 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2703}
2704
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002705void ASTWriter::WriteComments() {
2706 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002707 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002708 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002709 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2710 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002711 I != E; ++I) {
2712 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002713 AddSourceRange((*I)->getSourceRange(), Record);
2714 Record.push_back((*I)->getKind());
2715 Record.push_back((*I)->isTrailingComment());
2716 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002717 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2718 }
2719 Stream.ExitBlock();
2720}
2721
Douglas Gregorc5046832009-04-27 18:38:38 +00002722//===----------------------------------------------------------------------===//
2723// Global Method Pool and Selector Serialization
2724//===----------------------------------------------------------------------===//
2725
Douglas Gregore84a9da2009-04-20 20:36:09 +00002726namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002727// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002728class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002729 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002730
2731public:
2732 typedef Selector key_type;
2733 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002734
Sebastian Redl834bb972010-08-04 17:20:04 +00002735 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002736 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002737 ObjCMethodList Instance, Factory;
2738 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002739 typedef const data_type& data_type_ref;
2740
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002741 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002742
Douglas Gregorc78d3462009-04-24 21:10:55 +00002743 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002744 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002745 }
Mike Stump11289f42009-09-09 15:08:12 +00002746
2747 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002748 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002749 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002750 using namespace llvm::support;
2751 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002752 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002753 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002754 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2755 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002756 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002757 if (Method->Method)
2758 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002759 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002760 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002761 if (Method->Method)
2762 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002763 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002764 return std::make_pair(KeyLen, DataLen);
2765 }
Mike Stump11289f42009-09-09 15:08:12 +00002766
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002767 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002768 using namespace llvm::support;
2769 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002770 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002771 assert((Start >> 32) == 0 && "Selector key offset too large");
2772 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002773 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002774 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002775 if (N == 0)
2776 N = 1;
2777 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002778 LE.write<uint32_t>(
2779 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002780 }
Mike Stump11289f42009-09-09 15:08:12 +00002781
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002782 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002783 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002784 using namespace llvm::support;
2785 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002786 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002787 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002788 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002789 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002790 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002791 if (Method->Method)
2792 ++NumInstanceMethods;
2793
2794 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002795 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002796 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002797 if (Method->Method)
2798 ++NumFactoryMethods;
2799
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002800 unsigned InstanceBits = Methods.Instance.getBits();
2801 assert(InstanceBits < 4);
2802 unsigned NumInstanceMethodsAndBits =
2803 (NumInstanceMethods << 2) | InstanceBits;
2804 unsigned FactoryBits = Methods.Factory.getBits();
2805 assert(FactoryBits < 4);
2806 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Justin Bognere1c147c2014-03-28 22:03:19 +00002807 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2808 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002809 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002810 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002811 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002812 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002813 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002814 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002815 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002816 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002817
2818 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002819 }
2820};
2821} // end anonymous namespace
2822
Sebastian Redla19a67f2010-08-03 21:58:15 +00002823/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002824///
2825/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002826/// in an on-disk hash table indexed by the selector. The hash table also
2827/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002828void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002829 using namespace llvm;
2830
Sebastian Redla19a67f2010-08-03 21:58:15 +00002831 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002832 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002833 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002834 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002835 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002836 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002837 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002838 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002839
Sebastian Redla19a67f2010-08-03 21:58:15 +00002840 // Create the on-disk hash table representation. We walk through every
2841 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002842 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002843 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002844 I = SelectorIDs.begin(), E = SelectorIDs.end();
2845 I != E; ++I) {
2846 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002847 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002848 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002849 I->second,
2850 ObjCMethodList(),
2851 ObjCMethodList()
2852 };
2853 if (F != SemaRef.MethodPool.end()) {
2854 Data.Instance = F->second.first;
2855 Data.Factory = F->second.second;
2856 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002857 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002858 // changed.
2859 if (Chain && I->second < FirstSelectorID) {
2860 // Selector already exists. Did it change?
2861 bool changed = false;
2862 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002863 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002864 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002865 changed = true;
2866 }
2867 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002868 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002869 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002870 changed = true;
2871 }
2872 if (!changed)
2873 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002874 } else if (Data.Instance.Method || Data.Factory.Method) {
2875 // A new method pool entry.
2876 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002877 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002878 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002879 }
2880
Douglas Gregorc78d3462009-04-24 21:10:55 +00002881 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002882 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002883 uint32_t BucketOffset;
2884 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002885 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002886 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002887 llvm::raw_svector_ostream Out(MethodPool);
2888 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002889 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002890 BucketOffset = Generator.Emit(Out, Trait);
2891 }
2892
2893 // Create a blob abbreviation
2894 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002895 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2899 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2900
Douglas Gregor95c13f52009-04-25 17:48:32 +00002901 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002902 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002903 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002904 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002905 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002906 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002907
2908 // Create a blob abbreviation for the selector table offsets.
2909 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002910 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2914 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2915
2916 // Write the selector offsets table.
2917 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002918 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002919 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002920 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002921 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002922 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002923 }
2924}
2925
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002926/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002927void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002928 using namespace llvm;
2929 if (SemaRef.ReferencedSelectors.empty())
2930 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002931
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002932 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002933
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002934 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002935 // very tricky to fix, and given that @selector shouldn't really appear in
2936 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002937 for (DenseMap<Selector, SourceLocation>::iterator S =
2938 SemaRef.ReferencedSelectors.begin(),
2939 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2940 Selector Sel = (*S).first;
2941 SourceLocation Loc = (*S).second;
2942 AddSelectorRef(Sel, Record);
2943 AddSourceLocation(Loc, Record);
2944 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002945 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002946}
2947
Douglas Gregorc5046832009-04-27 18:38:38 +00002948//===----------------------------------------------------------------------===//
2949// Identifier Table Serialization
2950//===----------------------------------------------------------------------===//
2951
Douglas Gregorc78d3462009-04-24 21:10:55 +00002952namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002953class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002954 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002955 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002956 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002957 bool IsModule;
2958
Douglas Gregor1d583f22009-04-28 21:18:29 +00002959 /// \brief Determines whether this is an "interesting" identifier
2960 /// that needs a full IdentifierInfo structure written into the hash
2961 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002962 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002963 if (II->isPoisoned() ||
2964 II->isExtensionToken() ||
2965 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002966 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002967 II->getFETokenInfo<void>())
2968 return true;
2969
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002970 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002971 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002972
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002973 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002974 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002975 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002976
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002977 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2978 if (!IsModule)
2979 return !shouldIgnoreMacro(Macro, IsModule, PP);
2980 SubmoduleID ModID;
2981 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2982 return true;
2983 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002984
2985 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002986 }
2987
Richard Smith49f906a2014-03-01 00:08:04 +00002988 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2989
2990 MacroDirective *
2991 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002992 ModID = 0;
Richard Smith49f906a2014-03-01 00:08:04 +00002993 llvm::SmallVector<SubmoduleID, 1> Overridden;
2994 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
2995 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2996 return NextMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002997 return 0;
2998 }
2999
Richard Smith49f906a2014-03-01 00:08:04 +00003000 MacroDirective *
3001 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
3002 OverriddenList &Overridden) {
3003 if (MacroDirective *NextMD =
3004 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
3005 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
3006 return NextMD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003007 return 0;
3008 }
3009
3010 /// \brief Traverses the macro directives history and returns the latest
Richard Smith49f906a2014-03-01 00:08:04 +00003011 /// public macro definition or undefinition that is not in ModID.
3012 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003013 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00003014 /// ModID is updated to the module containing the returned directive.
3015 ///
3016 /// FIXME: This process breaks down if a module defines a macro, imports
3017 /// another submodule that changes the macro, then changes the
3018 /// macro again itself.
3019 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
3020 SubmoduleID &ModID,
3021 OverriddenList &Overridden) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003022 if (!MD)
3023 return 0;
3024
Richard Smith49f906a2014-03-01 00:08:04 +00003025 Overridden.clear();
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003026 SubmoduleID OrigModID = ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003027 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003028 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003029 SubmoduleID ThisModID = getSubmoduleID(MD);
3030 if (ThisModID == 0) {
Richard Smith49f906a2014-03-01 00:08:04 +00003031 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003032 continue;
3033 }
Richard Smith49f906a2014-03-01 00:08:04 +00003034 if (ThisModID != ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003035 ModID = ThisModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003036 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003037 }
Richard Smith49f906a2014-03-01 00:08:04 +00003038
3039 // If this is a definition from a submodule import, that submodule's
3040 // definition is overridden by the definition or undefinition that we
3041 // started with.
3042 // FIXME: This should only apply to macros defined in OrigModID.
3043 // We can't do that currently, because a #include of a different submodule
3044 // of the same module just leaks through macros instead of providing new
3045 // DefMacroDirectives for them.
Richard Smith9d100862014-03-06 03:16:27 +00003046 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3047 // Figure out which submodule the macro was originally defined within.
3048 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
3049 if (!SourceID) {
3050 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3051 if (DefLoc == MD->getLocation())
3052 SourceID = ThisModID;
3053 else
3054 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3055 }
3056 if (SourceID != OrigModID)
Richard Smith49f906a2014-03-01 00:08:04 +00003057 Overridden.push_back(SourceID);
Richard Smith9d100862014-03-06 03:16:27 +00003058 }
Richard Smith49f906a2014-03-01 00:08:04 +00003059
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003060 // We are looking for a definition in a different submodule than the one
3061 // that we started with. If a submodule has re-definitions of the same
3062 // macro, only the last definition will be used as the "exported" one.
3063 if (ModID == OrigModID)
3064 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003065
Richard Smith49f906a2014-03-01 00:08:04 +00003066 // The latest visibility directive for a name in a submodule affects all
3067 // the directives that come before it.
3068 if (VisibilityMacroDirective *VisMD =
3069 dyn_cast<VisibilityMacroDirective>(MD)) {
3070 if (!IsPublic.hasValue())
3071 IsPublic = VisMD->isPublic();
3072 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3073 // FIXME: If we find an imported macro, we should include its list of
3074 // overrides in our export.
3075 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003076 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003077 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003078
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003079 return 0;
3080 }
3081
3082 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003083 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003084 }
3085
Douglas Gregore84a9da2009-04-20 20:36:09 +00003086public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003087 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003088 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003089
Sebastian Redl539c5062010-08-18 23:57:32 +00003090 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003091 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003092
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003093 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3094 IdentifierResolver &IdResolver, bool IsModule)
3095 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003096
3097 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003098 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003099 }
Mike Stump11289f42009-09-09 15:08:12 +00003100
3101 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003102 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003103 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003104 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003105 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003106 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003107 DataLen += 2; // 2 bytes for builtin ID
3108 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003109 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003110 DataLen += 4; // MacroDirectives offset.
3111 if (IsModule) {
3112 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003113 llvm::SmallVector<SubmoduleID, 4> Overridden;
3114 for (MacroDirective *
3115 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3116 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3117 // Previous macro's overrides.
3118 if (!Overridden.empty())
3119 DataLen += 4 * (1 + Overridden.size());
3120 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003121 }
Richard Smith49f906a2014-03-01 00:08:04 +00003122 // Previous macro's overrides.
3123 if (!Overridden.empty())
3124 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003125 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003126 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003127 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003128
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003129 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3130 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003131 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003132 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003133 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003134 using namespace llvm::support;
3135 endian::Writer<little> LE(Out);
3136
3137 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003138 // We emit the key length after the data length so that every
3139 // string is preceded by a 16-bit length. This matches the PTH
3140 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003141 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003142 return std::make_pair(KeyLen, DataLen);
3143 }
Mike Stump11289f42009-09-09 15:08:12 +00003144
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003145 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003146 unsigned KeyLen) {
3147 // Record the location of the key data. This is used when generating
3148 // the mapping from persistent IDs to strings.
3149 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003150 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003151 }
Mike Stump11289f42009-09-09 15:08:12 +00003152
Richard Smith49f906a2014-03-01 00:08:04 +00003153 static void emitMacroOverrides(raw_ostream &Out,
3154 llvm::ArrayRef<SubmoduleID> Overridden) {
3155 if (!Overridden.empty()) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003156 using namespace llvm::support;
3157 endian::Writer<little> LE(Out);
3158 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Richard Smith49f906a2014-03-01 00:08:04 +00003159 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003160 LE.write<uint32_t>(Overridden[I]);
Richard Smith49f906a2014-03-01 00:08:04 +00003161 }
3162 }
3163
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003164 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003165 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003166 using namespace llvm::support;
3167 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003168 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003169 if (!isInterestingIdentifier(II, Macro)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003170 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003171 return;
3172 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003173
Justin Bognere1c147c2014-03-28 22:03:19 +00003174 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003175 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3176 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003177 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003178 Bits = 0;
3179 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003180 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003181 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003182 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3183 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003184 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003185 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003186 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003187
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003188 if (HadMacroDefinition) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003189 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003190 if (IsModule) {
3191 // Write the IDs of macros coming from different submodules.
3192 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003193 llvm::SmallVector<SubmoduleID, 4> Overridden;
3194 for (MacroDirective *
3195 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3196 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3197 MacroID InfoID = 0;
3198 emitMacroOverrides(Out, Overridden);
3199 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3200 InfoID = Writer.getMacroID(DefMD->getInfo());
3201 assert(InfoID);
Justin Bognere1c147c2014-03-28 22:03:19 +00003202 LE.write<uint32_t>(InfoID << 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003203 } else {
3204 assert(isa<UndefMacroDirective>(MD));
Justin Bognere1c147c2014-03-28 22:03:19 +00003205 LE.write<uint32_t>((ModID << 1) | 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003206 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003207 }
Richard Smith49f906a2014-03-01 00:08:04 +00003208 emitMacroOverrides(Out, Overridden);
Justin Bognere1c147c2014-03-28 22:03:19 +00003209 LE.write<uint32_t>(0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003210 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003211 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003212
Douglas Gregora868bbd2009-04-21 22:25:48 +00003213 // Emit the declaration IDs in reverse order, because the
3214 // IdentifierResolver provides the declarations as they would be
3215 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003216 // "stat"), but the ASTReader adds declarations to the end of the list
3217 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003218 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003219 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3220 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003221 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003222 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003223 D != DEnd; ++D)
Justin Bognere1c147c2014-03-28 22:03:19 +00003224 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003225 }
3226
3227 /// \brief Returns the most recent local decl or the given decl if there are
3228 /// no local ones. The given decl is assumed to be the most recent one.
3229 Decl *getMostRecentLocalDecl(Decl *Orig) {
3230 // The only way a "from AST file" decl would be more recent from a local one
3231 // is if it came from a module.
3232 if (!PP.getLangOpts().Modules)
3233 return Orig;
3234
3235 // Look for a local in the decl chain.
3236 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3237 if (!D->isFromASTFile())
3238 return D;
3239 // If we come up a decl from a (chained-)PCH stop since we won't find a
3240 // local one.
3241 if (D->getOwningModuleID() == 0)
3242 break;
3243 }
3244
3245 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003246 }
3247};
3248} // end anonymous namespace
3249
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003250/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003251///
3252/// The identifier table consists of a blob containing string data
3253/// (the actual identifiers themselves) and a separate "offsets" index
3254/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003255void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3256 IdentifierResolver &IdResolver,
3257 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003258 using namespace llvm;
3259
3260 // Create and write out the blob that contains the identifier
3261 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003262 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003263 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003264 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003265
Douglas Gregore6648fb2009-04-28 20:33:11 +00003266 // Look for any identifiers that were named while processing the
3267 // headers, but are otherwise not needed. We add these to the hash
3268 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003269 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003270 // file.
3271 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3272 IDEnd = PP.getIdentifierTable().end();
3273 ID != IDEnd; ++ID)
3274 getIdentifierRef(ID->second);
3275
Sebastian Redlff4a2952010-07-23 23:49:55 +00003276 // Create the on-disk hash table representation. We only store offsets
3277 // for identifiers that appear here for the first time.
3278 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003279 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003280 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3281 ID != IDEnd; ++ID) {
3282 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003283 if (!Chain || !ID->first->isFromAST() ||
3284 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003285 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003286 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003287 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003288
Douglas Gregore84a9da2009-04-20 20:36:09 +00003289 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003290 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003291 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003292 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003293 using namespace llvm::support;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003294 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003295 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003296 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003297 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003298 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003299 }
3300
3301 // Create a blob abbreviation
3302 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003303 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003304 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003306 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003307
3308 // Write the identifier table
3309 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003310 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003311 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003312 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003313 }
3314
3315 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003316 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003317 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003319 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3321 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3322
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003323#ifndef NDEBUG
3324 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3325 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3326#endif
3327
Douglas Gregor0e149972009-04-25 19:10:14 +00003328 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003329 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003330 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003331 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003332 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003333 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003334}
3335
Douglas Gregorc5046832009-04-27 18:38:38 +00003336//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003337// DeclContext's Name Lookup Table Serialization
3338//===----------------------------------------------------------------------===//
3339
3340namespace {
3341// Trait used for the on-disk hash table used in the method pool.
3342class ASTDeclContextNameLookupTrait {
3343 ASTWriter &Writer;
3344
3345public:
3346 typedef DeclarationName key_type;
3347 typedef key_type key_type_ref;
3348
3349 typedef DeclContext::lookup_result data_type;
3350 typedef const data_type& data_type_ref;
3351
3352 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3353
3354 unsigned ComputeHash(DeclarationName Name) {
3355 llvm::FoldingSetNodeID ID;
3356 ID.AddInteger(Name.getNameKind());
3357
3358 switch (Name.getNameKind()) {
3359 case DeclarationName::Identifier:
3360 ID.AddString(Name.getAsIdentifierInfo()->getName());
3361 break;
3362 case DeclarationName::ObjCZeroArgSelector:
3363 case DeclarationName::ObjCOneArgSelector:
3364 case DeclarationName::ObjCMultiArgSelector:
3365 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3366 break;
3367 case DeclarationName::CXXConstructorName:
3368 case DeclarationName::CXXDestructorName:
3369 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003370 break;
3371 case DeclarationName::CXXOperatorName:
3372 ID.AddInteger(Name.getCXXOverloadedOperator());
3373 break;
3374 case DeclarationName::CXXLiteralOperatorName:
3375 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3376 case DeclarationName::CXXUsingDirective:
3377 break;
3378 }
3379
3380 return ID.ComputeHash();
3381 }
3382
3383 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003384 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003385 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003386 using namespace llvm::support;
3387 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003388 unsigned KeyLen = 1;
3389 switch (Name.getNameKind()) {
3390 case DeclarationName::Identifier:
3391 case DeclarationName::ObjCZeroArgSelector:
3392 case DeclarationName::ObjCOneArgSelector:
3393 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003394 case DeclarationName::CXXLiteralOperatorName:
3395 KeyLen += 4;
3396 break;
3397 case DeclarationName::CXXOperatorName:
3398 KeyLen += 1;
3399 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003400 case DeclarationName::CXXConstructorName:
3401 case DeclarationName::CXXDestructorName:
3402 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003403 case DeclarationName::CXXUsingDirective:
3404 break;
3405 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003406 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003407
3408 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003409 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003410 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003411
3412 return std::make_pair(KeyLen, DataLen);
3413 }
3414
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003415 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003416 using namespace llvm::support;
3417 endian::Writer<little> LE(Out);
3418 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003419 switch (Name.getNameKind()) {
3420 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003421 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003422 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003423 case DeclarationName::ObjCZeroArgSelector:
3424 case DeclarationName::ObjCOneArgSelector:
3425 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003426 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003427 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003428 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003429 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3430 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003431 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003432 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003433 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003434 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003435 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003436 case DeclarationName::CXXConstructorName:
3437 case DeclarationName::CXXDestructorName:
3438 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003439 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003440 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003441 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003442
3443 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003444 }
3445
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003446 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003447 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003448 using namespace llvm::support;
3449 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003450 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003451 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003452 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3453 I != E; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003454 LE.write<uint32_t>(Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003455
3456 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3457 }
3458};
3459} // end anonymous namespace
3460
Richard Smith961eae52014-03-25 01:14:22 +00003461uint32_t
3462ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3463 llvm::SmallVectorImpl<char> &LookupTable) {
3464 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3465 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3466
Justin Bognerbb094f02014-04-18 19:57:06 +00003467 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3468 Generator;
Richard Smith961eae52014-03-25 01:14:22 +00003469 ASTDeclContextNameLookupTrait Trait(*this);
3470
3471 // Create the on-disk hash table representation.
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003472 DeclarationName ConstructorName;
Richard Smith961eae52014-03-25 01:14:22 +00003473 DeclarationName ConversionName;
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003474 SmallVector<NamedDecl *, 8> ConstructorDecls;
Richard Smith961eae52014-03-25 01:14:22 +00003475 SmallVector<NamedDecl *, 4> ConversionDecls;
3476
3477 auto AddLookupResult = [&](DeclarationName Name,
3478 DeclContext::lookup_result Result) {
3479 if (Result.empty())
3480 return;
3481
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003482 // Different DeclarationName values of certain kinds are mapped to
3483 // identical serialized keys, because we don't want to use type
3484 // identifiers in the keys (since type ids are local to the module).
3485 switch (Name.getNameKind()) {
3486 case DeclarationName::CXXConstructorName:
3487 // There may be different CXXConstructorName DeclarationName values
3488 // in a DeclContext because a UsingDecl that inherits constructors
3489 // has the DeclarationName of the inherited constructors.
3490 if (!ConstructorName)
3491 ConstructorName = Name;
3492 ConstructorDecls.append(Result.begin(), Result.end());
3493 return;
3494 case DeclarationName::CXXConversionFunctionName:
Richard Smith961eae52014-03-25 01:14:22 +00003495 if (!ConversionName)
3496 ConversionName = Name;
3497 ConversionDecls.append(Result.begin(), Result.end());
3498 return;
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003499 default:
3500 break;
Richard Smith961eae52014-03-25 01:14:22 +00003501 }
3502
3503 Generator.insert(Name, Result, Trait);
3504 };
3505
3506 SmallVector<DeclarationName, 16> ExternalNames;
3507 for (auto &Lookup : *DC->getLookupPtr()) {
3508 if (Lookup.second.hasExternalDecls() ||
3509 DC->NeedToReconcileExternalVisibleStorage) {
3510 // We don't know for sure what declarations are found by this name,
3511 // because the external source might have a different set from the set
3512 // that are in the lookup map, and we can't update it now without
3513 // risking invalidating our lookup iterator. So add it to a queue to
3514 // deal with later.
3515 ExternalNames.push_back(Lookup.first);
3516 continue;
3517 }
3518
3519 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3520 }
3521
3522 // Add the names we needed to defer. Note, this shouldn't add any new decls
3523 // to the list we need to serialize: any new declarations we find here should
3524 // be imported from an external source.
3525 // FIXME: What if the external source isn't an ASTReader?
3526 for (const auto &Name : ExternalNames)
3527 // FIXME: const_cast since OnDiskHashTable wants a non-const lookup result.
3528 AddLookupResult(Name, const_cast<DeclContext*>(DC)->lookup(Name));
3529
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003530 // Add the constructors.
3531 if (!ConstructorDecls.empty()) {
3532 Generator.insert(ConstructorName,
3533 DeclContext::lookup_result(ConstructorDecls.begin(),
3534 ConstructorDecls.end()),
3535 Trait);
3536 }
3537 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003538 if (!ConversionDecls.empty()) {
3539 Generator.insert(ConversionName,
3540 DeclContext::lookup_result(ConversionDecls.begin(),
3541 ConversionDecls.end()),
3542 Trait);
3543 }
3544
3545 // Create the on-disk hash table in a buffer.
3546 llvm::raw_svector_ostream Out(LookupTable);
3547 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003548 using namespace llvm::support;
3549 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003550 return Generator.Emit(Out, Trait);
3551}
3552
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003553/// \brief Write the block containing all of the declaration IDs
3554/// visible from the given DeclContext.
3555///
3556/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003557/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003558uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3559 DeclContext *DC) {
3560 if (DC->getPrimaryContext() != DC)
3561 return 0;
3562
3563 // Since there is no name lookup into functions or methods, don't bother to
3564 // build a visible-declarations table for these entities.
3565 if (DC->isFunctionOrMethod())
3566 return 0;
3567
3568 // If not in C++, we perform name lookup for the translation unit via the
3569 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003570 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003571 return 0;
3572
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003573 // Serialize the contents of the mapping used for lookup. Note that,
3574 // although we have two very different code paths, the serialized
3575 // representation is the same for both cases: a declaration name,
3576 // followed by a size, followed by references to the visible
3577 // declarations that have that name.
3578 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003579 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003580 if (!Map || Map->empty())
3581 return 0;
3582
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003583 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003584 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003585 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003586
3587 // Write the lookup table
3588 RecordData Record;
3589 Record.push_back(DECL_CONTEXT_VISIBLE);
3590 Record.push_back(BucketOffset);
3591 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3592 LookupTable.str());
3593
3594 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3595 ++NumVisibleDeclContexts;
3596 return Offset;
3597}
3598
Sebastian Redla4071b42010-08-24 00:50:09 +00003599/// \brief Write an UPDATE_VISIBLE block for the given context.
3600///
3601/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3602/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003603/// (in C++), for namespaces, and for classes with forward-declared unscoped
3604/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003605void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003606 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003607 if (!Map || Map->empty())
3608 return;
3609
Sebastian Redla4071b42010-08-24 00:50:09 +00003610 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003611 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003612 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003613
3614 // Write the lookup table
3615 RecordData Record;
3616 Record.push_back(UPDATE_VISIBLE);
3617 Record.push_back(getDeclID(cast<Decl>(DC)));
3618 Record.push_back(BucketOffset);
3619 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3620}
3621
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003622/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3623void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3624 RecordData Record;
3625 Record.push_back(Opts.fp_contract);
3626 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3627}
3628
3629/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3630void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003631 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003632 return;
3633
3634 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3635 RecordData Record;
3636#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3637#include "clang/Basic/OpenCLExtensions.def"
3638 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3639}
3640
Douglas Gregor358cd442012-01-15 16:58:34 +00003641void ASTWriter::WriteRedeclarations() {
3642 RecordData LocalRedeclChains;
3643 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3644
3645 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3646 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003647 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003648
3649 Decl *MostRecent = First->getMostRecentDecl();
3650
3651 // If we only have a single declaration, there is no point in storing
3652 // a redeclaration chain.
3653 if (First == MostRecent)
3654 continue;
3655
3656 unsigned Offset = LocalRedeclChains.size();
3657 unsigned Size = 0;
3658 LocalRedeclChains.push_back(0); // Placeholder for the size.
3659
3660 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003661 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003662 Prev = Prev->getPreviousDecl()) {
3663 if (!Prev->isFromASTFile()) {
3664 AddDeclRef(Prev, LocalRedeclChains);
3665 ++Size;
3666 }
3667 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003668
3669 if (!First->isFromASTFile() && Chain) {
3670 Decl *FirstFromAST = MostRecent;
3671 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3672 if (Prev->isFromASTFile())
3673 FirstFromAST = Prev;
3674 }
3675
3676 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3677 }
3678
Douglas Gregor358cd442012-01-15 16:58:34 +00003679 LocalRedeclChains[Offset] = Size;
3680
3681 // Reverse the set of local redeclarations, so that we store them in
3682 // order (since we found them in reverse order).
3683 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3684
Douglas Gregor6168bd22013-02-18 15:53:43 +00003685 // Add the mapping from the first ID from the AST to the set of local
3686 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003687 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3688 LocalRedeclsMap.push_back(Info);
3689
3690 assert(N == Redeclarations.size() &&
3691 "Deserialized a declaration we shouldn't have");
3692 }
3693
3694 if (LocalRedeclChains.empty())
3695 return;
3696
3697 // Sort the local redeclarations map by the first declaration ID,
3698 // since the reader will be performing binary searches on this information.
3699 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3700
3701 // Emit the local redeclarations map.
3702 using namespace llvm;
3703 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3704 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3705 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3706 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3707 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3708
3709 RecordData Record;
3710 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3711 Record.push_back(LocalRedeclsMap.size());
3712 Stream.EmitRecordWithBlob(AbbrevID, Record,
3713 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3714 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3715
3716 // Emit the redeclaration chains.
3717 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3718}
3719
Douglas Gregor404cdde2012-01-27 01:47:08 +00003720void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003721 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003722 RecordData Categories;
3723
3724 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3725 unsigned Size = 0;
3726 unsigned StartIndex = Categories.size();
3727
3728 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3729
3730 // Allocate space for the size.
3731 Categories.push_back(0);
3732
3733 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003734 for (ObjCInterfaceDecl::known_categories_iterator
3735 Cat = Class->known_categories_begin(),
3736 CatEnd = Class->known_categories_end();
3737 Cat != CatEnd; ++Cat, ++Size) {
3738 assert(getDeclID(*Cat) != 0 && "Bogus category");
3739 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003740 }
3741
3742 // Update the size.
3743 Categories[StartIndex] = Size;
3744
3745 // Record this interface -> category map.
3746 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3747 CategoriesMap.push_back(CatInfo);
3748 }
3749
3750 // Sort the categories map by the definition ID, since the reader will be
3751 // performing binary searches on this information.
3752 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3753
3754 // Emit the categories map.
3755 using namespace llvm;
3756 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3757 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3760 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3761
3762 RecordData Record;
3763 Record.push_back(OBJC_CATEGORIES_MAP);
3764 Record.push_back(CategoriesMap.size());
3765 Stream.EmitRecordWithBlob(AbbrevID, Record,
3766 reinterpret_cast<char*>(CategoriesMap.data()),
3767 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3768
3769 // Emit the category lists.
3770 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3771}
3772
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003773void ASTWriter::WriteMergedDecls() {
3774 if (!Chain || Chain->MergedDecls.empty())
3775 return;
3776
3777 RecordData Record;
3778 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3779 IEnd = Chain->MergedDecls.end();
3780 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003781 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003782 : getDeclID(I->first);
3783 assert(CanonID && "Merged declaration not known?");
3784
3785 Record.push_back(CanonID);
3786 Record.push_back(I->second.size());
3787 Record.append(I->second.begin(), I->second.end());
3788 }
3789 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3790}
3791
Richard Smithe40f2ba2013-08-07 21:41:30 +00003792void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3793 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3794
3795 if (LPTMap.empty())
3796 return;
3797
3798 RecordData Record;
3799 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3800 ItEnd = LPTMap.end();
3801 It != ItEnd; ++It) {
3802 LateParsedTemplate *LPT = It->second;
3803 AddDeclRef(It->first, Record);
3804 AddDeclRef(LPT->D, Record);
3805 Record.push_back(LPT->Toks.size());
3806
3807 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3808 TokEnd = LPT->Toks.end();
3809 TokIt != TokEnd; ++TokIt) {
3810 AddToken(*TokIt, Record);
3811 }
3812 }
3813 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3814}
3815
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003816//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003817// General Serialization Routines
3818//===----------------------------------------------------------------------===//
3819
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003820/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003821void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3822 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003823 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003824 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3825 e = Attrs.end(); i != e; ++i){
3826 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003827 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003828 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003829
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003830#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003831
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003832 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003833}
3834
John McCallf413f5e2013-05-03 00:10:13 +00003835void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3836 AddSourceLocation(Tok.getLocation(), Record);
3837 Record.push_back(Tok.getLength());
3838
3839 // FIXME: When reading literal tokens, reconstruct the literal pointer
3840 // if it is needed.
3841 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3842 // FIXME: Should translate token kind to a stable encoding.
3843 Record.push_back(Tok.getKind());
3844 // FIXME: Should translate token flags to a stable encoding.
3845 Record.push_back(Tok.getFlags());
3846}
3847
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003848void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003849 Record.push_back(Str.size());
3850 Record.insert(Record.end(), Str.begin(), Str.end());
3851}
3852
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003853void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3854 RecordDataImpl &Record) {
3855 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003856 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003857 Record.push_back(*Minor + 1);
3858 else
3859 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003860 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003861 Record.push_back(*Subminor + 1);
3862 else
3863 Record.push_back(0);
3864}
3865
Douglas Gregore84a9da2009-04-20 20:36:09 +00003866/// \brief Note that the identifier II occurs at the given offset
3867/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003868void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003869 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003870 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003871 // up earlier in the chain and thus don't need an offset.
3872 if (ID >= FirstIdentID)
3873 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003874}
3875
Douglas Gregor95c13f52009-04-25 17:48:32 +00003876/// \brief Note that the selector Sel occurs at the given offset
3877/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003878void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003879 unsigned ID = SelectorIDs[Sel];
3880 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003881 // Don't record offsets for selectors that are also available in a different
3882 // file.
3883 if (ID < FirstSelectorID)
3884 return;
3885 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003886}
3887
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003888ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003889 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003890 WritingAST(false), DoneWritingDeclsAndTypes(false),
3891 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003892 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003893 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003894 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3895 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003896 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3897 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003898 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003899 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003900 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003901 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003902 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003903 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003904 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3905 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3906 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003907 DeclTypedefAbbrev(0),
3908 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3909 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003910{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003911}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003912
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003913ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003914 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003915}
3916
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003917void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003918 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003919 Module *WritingModule, StringRef isysroot,
3920 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003921 WritingAST = true;
3922
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003923 ASTHasCompilerErrors = hasErrors;
3924
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003925 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003926 Stream.Emit((unsigned)'C', 8);
3927 Stream.Emit((unsigned)'P', 8);
3928 Stream.Emit((unsigned)'C', 8);
3929 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003930
Chris Lattner28fa4e62009-04-26 22:26:21 +00003931 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003932
Douglas Gregoreda8e122011-08-09 15:13:55 +00003933 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003934 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003935 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003936 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003937 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003938 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003939 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003940
3941 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003942}
3943
Douglas Gregora94a1542011-07-27 21:45:57 +00003944template<typename Vector>
3945static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3946 ASTWriter::RecordData &Record) {
3947 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3948 I != E; ++I) {
3949 Writer.AddDeclRef(*I, Record);
3950 }
3951}
3952
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003953void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003954 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003955 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003956 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003957 using namespace llvm;
3958
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003959 bool isModule = WritingModule != 0;
3960
Douglas Gregorcf68c582011-12-01 22:20:10 +00003961 // Make sure that the AST reader knows to finalize itself.
3962 if (Chain)
3963 Chain->finalizeForWriting();
3964
Sebastian Redl143413f2010-07-12 22:02:52 +00003965 ASTContext &Context = SemaRef.Context;
3966 Preprocessor &PP = SemaRef.PP;
3967
Douglas Gregordab42432011-08-12 00:15:20 +00003968 // Set up predefined declaration IDs.
3969 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003970 if (Context.ObjCIdDecl)
3971 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003972 if (Context.ObjCSelDecl)
3973 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003974 if (Context.ObjCClassDecl)
3975 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003976 if (Context.ObjCProtocolClassDecl)
3977 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003978 if (Context.Int128Decl)
3979 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3980 if (Context.UInt128Decl)
3981 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003982 if (Context.ObjCInstanceTypeDecl)
3983 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003984 if (Context.BuiltinVaListDecl)
3985 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3986
Douglas Gregor851443c2011-08-12 01:39:19 +00003987 if (!Chain) {
3988 // Make sure that we emit IdentifierInfos (and any attached
3989 // declarations) for builtins. We don't need to do this when we're
3990 // emitting chained PCH files, because all of the builtins will be
3991 // in the original PCH file.
3992 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003993 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003994 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003995 if (!Context.getLangOpts().NoBuiltin) {
3996 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3997 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003998 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3999 getIdentifierRef(&Table.get(BuiltinNames[I]));
4000 }
4001
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004002 // If there are any out-of-date identifiers, bring them up to date.
4003 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00004004 // Find out-of-date identifiers.
4005 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004006 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4007 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00004008 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004009 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00004010 OutOfDate.push_back(ID->second);
4011 }
4012
4013 // Update the out-of-date identifiers.
4014 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4015 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4016 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004017 }
4018
Chris Lattner0c797362009-09-08 18:19:27 +00004019 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004020 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004021 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004022 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004023 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004024
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004025 // Build a record containing all of the file scoped decls in this file.
4026 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004027 if (!isModule)
4028 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4029 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004030
Douglas Gregor851443c2011-08-12 01:39:19 +00004031 // Build a record containing all of the delegating constructors we still need
4032 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004033 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004034 if (!isModule)
4035 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004036
Douglas Gregor851443c2011-08-12 01:39:19 +00004037 // Write the set of weak, undeclared identifiers. We always write the
4038 // entire table, since later PCH files in a PCH chain are only interested in
4039 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004040 RecordData WeakUndeclaredIdentifiers;
4041 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004042 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004043 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4044 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4045 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4046 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4047 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4048 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4049 }
4050 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004051
Richard Smith78165b52013-01-10 23:43:47 +00004052 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004053 // declarations in this header file. Generally, this record will be
4054 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00004055 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004056 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00004057 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00004058 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00004059 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4060 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00004061 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004062 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00004063 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00004064 }
4065
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004066 // Build a record containing all of the ext_vector declarations.
4067 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004068 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004069
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004070 // Build a record containing all of the VTable uses information.
4071 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004072 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004073 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4074 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4075 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4076 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4077 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004078 }
4079
4080 // Build a record containing all of dynamic classes declarations.
4081 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004082 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004083
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004084 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004085 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004086 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004087 I = SemaRef.PendingInstantiations.begin(),
4088 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4089 AddDeclRef(I->first, PendingInstantiations);
4090 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004091 }
4092 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4093 "There are local ones at end of translation unit!");
4094
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004095 // Build a record containing some declaration references.
4096 RecordData SemaDeclRefs;
4097 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4098 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4099 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4100 }
4101
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004102 RecordData CUDASpecialDeclRefs;
4103 if (Context.getcudaConfigureCallDecl()) {
4104 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4105 }
4106
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004107 // Build a record containing all of the known namespaces.
4108 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004109 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004110 I = SemaRef.KnownNamespaces.begin(),
4111 IEnd = SemaRef.KnownNamespaces.end();
4112 I != IEnd; ++I) {
4113 if (!I->second)
4114 AddDeclRef(I->first, KnownNamespaces);
4115 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004116
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004117 // Build a record of all used, undefined objects that require definitions.
4118 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004119
4120 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004121 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004122 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4123 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004124 AddDeclRef(I->first, UndefinedButUsed);
4125 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004126 }
4127
Douglas Gregor112b9072012-10-18 05:31:06 +00004128 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004129 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004130
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004131 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004132 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004133 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004134
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004135 // This is so that older clang versions, before the introduction
4136 // of the control block, can read and reject the newer PCH format.
4137 Record.clear();
4138 Record.push_back(VERSION_MAJOR);
4139 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4140
Douglas Gregor851443c2011-08-12 01:39:19 +00004141 // Create a lexical update block containing all of the declarations in the
4142 // translation unit that do not come from other AST files.
4143 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4144 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004145 for (const auto *I : TU->noload_decls()) {
4146 if (!I->isFromASTFile())
4147 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004148 }
4149
4150 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4151 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4152 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4153 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4154 Record.clear();
4155 Record.push_back(TU_UPDATE_LEXICAL);
4156 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4157 data(NewGlobalDecls));
4158
4159 // And a visible updates block for the translation unit.
4160 Abv = new llvm::BitCodeAbbrev();
4161 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4162 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4163 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4164 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4165 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4166 WriteDeclContextVisibleUpdate(TU);
4167
4168 // If the translation unit has an anonymous namespace, and we don't already
4169 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004170 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004171 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4172 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004173 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004174 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004175 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004176
Richard Smith5652c0f2014-03-21 01:48:23 +00004177 // Add update records for all mangling numbers and static local numbers.
4178 // These aren't really update records, but this is a convenient way of
4179 // tagging this rare extra data onto the declarations.
4180 for (const auto &Number : Context.MangleNumbers)
4181 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004182 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4183 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004184 for (const auto &Number : Context.StaticLocalNumbers)
4185 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004186 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4187 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004188
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004189 // Make sure visible decls, added to DeclContexts previously loaded from
4190 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004191 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004192 I = UpdatingVisibleDecls.begin(),
4193 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4194 GetDeclRef(*I);
4195 }
4196
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004197 // Make sure all decls associated with an identifier are registered for
4198 // serialization.
4199 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4200 IDEnd = PP.getIdentifierTable().end();
4201 ID != IDEnd; ++ID) {
4202 const IdentifierInfo *II = ID->second;
4203 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4204 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4205 DEnd = SemaRef.IdResolver.end();
4206 D != DEnd; ++D) {
4207 GetDeclRef(*D);
4208 }
4209 }
4210 }
4211
Douglas Gregor5204bde2011-08-02 16:26:37 +00004212 // Form the record of special types.
4213 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004214 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004215 AddTypeRef(Context.getFILEType(), SpecialTypes);
4216 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4217 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4218 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4219 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004220 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004221 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004222
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004223 if (Chain) {
4224 // Write the mapping information describing our module dependencies and how
4225 // each of those modules were mapped into our own offset/ID space, so that
4226 // the reader can build the appropriate mapping to its own offset/ID space.
4227 // The map consists solely of a blob with the following format:
4228 // *(module-name-len:i16 module-name:len*i8
4229 // source-location-offset:i32
4230 // identifier-id:i32
4231 // preprocessed-entity-id:i32
4232 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004233 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004234 // selector-id:i32
4235 // declaration-id:i32
4236 // c++-base-specifiers-id:i32
4237 // type-id:i32)
4238 //
4239 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4240 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4242 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004243 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004244 {
4245 llvm::raw_svector_ostream Out(Buffer);
4246 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004247 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004248 M != MEnd; ++M) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004249 using namespace llvm::support;
4250 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004251 StringRef FileName = (*M)->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004252 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004253 Out.write(FileName.data(), FileName.size());
Justin Bognere1c147c2014-03-28 22:03:19 +00004254 LE.write<uint32_t>((*M)->SLocEntryBaseOffset);
4255 LE.write<uint32_t>((*M)->BaseIdentifierID);
4256 LE.write<uint32_t>((*M)->BaseMacroID);
4257 LE.write<uint32_t>((*M)->BasePreprocessedEntityID);
4258 LE.write<uint32_t>((*M)->BaseSubmoduleID);
4259 LE.write<uint32_t>((*M)->BaseSelectorID);
4260 LE.write<uint32_t>((*M)->BaseDeclID);
4261 LE.write<uint32_t>((*M)->BaseTypeIndex);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004262 }
4263 }
4264 Record.clear();
4265 Record.push_back(MODULE_OFFSET_MAP);
4266 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4267 Buffer.data(), Buffer.size());
4268 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004269
Richard Smithb9eab6d2014-03-20 19:44:17 +00004270 RecordData DeclUpdatesOffsetsRecord;
4271
Richard Smith59442b42014-03-20 20:07:19 +00004272 // Keep writing types, declarations, and declaration update records
4273 // until we've emitted all of them.
Richard Smithb9eab6d2014-03-20 19:44:17 +00004274 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
4275 WriteDeclsBlockAbbrevs();
4276 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4277 E = DeclsToRewrite.end();
4278 I != E; ++I)
4279 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004280 do {
4281 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4282 while (!DeclTypesToEmit.empty()) {
4283 DeclOrType DOT = DeclTypesToEmit.front();
4284 DeclTypesToEmit.pop();
4285 if (DOT.isType())
4286 WriteType(DOT.getType());
4287 else
4288 WriteDecl(Context, DOT.getDecl());
4289 }
4290 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004291 Stream.ExitBlock();
4292
Richard Smithb9eab6d2014-03-20 19:44:17 +00004293 DoneWritingDeclsAndTypes = true;
4294
4295 // These things can only be done once we've written out decls and types.
4296 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004297 if (!DeclUpdatesOffsetsRecord.empty())
4298 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004299 WriteCXXBaseSpecifiersOffsets();
4300 WriteFileDeclIDsMap();
4301 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4302
4303 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004304 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004305 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004306 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004307 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004308 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004309 WriteFPPragmaOptions(SemaRef.getFPOptions());
4310 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004311 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004312
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004313 // If we're emitting a module, write out the submodule information.
4314 if (WritingModule)
4315 WriteSubmodules(WritingModule);
4316
Douglas Gregor5204bde2011-08-02 16:26:37 +00004317 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4318
Douglas Gregord4df8652009-04-22 22:02:47 +00004319 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004320 if (!EagerlyDeserializedDecls.empty())
4321 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004322
4323 // Write the record containing tentative definitions.
4324 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004325 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004326
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004327 // Write the record containing unused file scoped decls.
4328 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004329 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004330
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004331 // Write the record containing weak undeclared identifiers.
4332 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004333 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004334 WeakUndeclaredIdentifiers);
4335
Richard Smith78165b52013-01-10 23:43:47 +00004336 // Write the record containing locally-scoped extern "C" definitions.
4337 if (!LocallyScopedExternCDecls.empty())
4338 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4339 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004340
4341 // Write the record containing ext_vector type names.
4342 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004343 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004344
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004345 // Write the record containing VTable uses information.
4346 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004347 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004348
4349 // Write the record containing dynamic classes declarations.
4350 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004351 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004352
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004353 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004354 if (!PendingInstantiations.empty())
4355 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004356
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004357 // Write the record containing declaration references of Sema.
4358 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004359 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004360
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004361 // Write the record containing CUDA-specific declaration references.
4362 if (!CUDASpecialDeclRefs.empty())
4363 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004364
4365 // Write the delegating constructors.
4366 if (!DelegatingCtorDecls.empty())
4367 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004368
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004369 // Write the known namespaces.
4370 if (!KnownNamespaces.empty())
4371 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004372
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004373 // Write the undefined internal functions and variables, and inline functions.
4374 if (!UndefinedButUsed.empty())
4375 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004376
Douglas Gregor851443c2011-08-12 01:39:19 +00004377 // Write the visible updates to DeclContexts.
4378 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4379 I = UpdatedDeclContexts.begin(),
4380 E = UpdatedDeclContexts.end();
4381 I != E; ++I)
4382 WriteDeclContextVisibleUpdate(*I);
4383
Douglas Gregor959bb062011-12-03 01:15:29 +00004384 if (!WritingModule) {
4385 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004386 struct ModuleInfo {
4387 uint64_t ID;
4388 Module *M;
4389 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4390 };
Richard Smith56be7542014-03-21 00:33:59 +00004391 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004392 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004393 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004394 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4395 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004396 }
Richard Smith56be7542014-03-21 00:33:59 +00004397
4398 if (!Imports.empty()) {
4399 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4400 return A.ID < B.ID;
4401 };
4402
4403 // Sort and deduplicate module IDs.
4404 std::sort(Imports.begin(), Imports.end(), Cmp);
4405 Imports.erase(std::unique(Imports.begin(), Imports.end(), Cmp),
4406 Imports.end());
4407
4408 RecordData ImportedModules;
4409 for (const auto &Import : Imports) {
4410 ImportedModules.push_back(Import.ID);
4411 // FIXME: If the module has macros imported then later has declarations
4412 // imported, this location won't be the right one as a location for the
4413 // declaration imports.
4414 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4415 }
4416
Douglas Gregor959bb062011-12-03 01:15:29 +00004417 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4418 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004419 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004420
Douglas Gregor851443c2011-08-12 01:39:19 +00004421 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004422 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004423 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004424 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004425 WriteLateParsedTemplates(SemaRef);
4426
Douglas Gregor08f01292009-04-17 22:13:46 +00004427 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004428 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004429 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004430 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004431 Record.push_back(NumLexicalDeclContexts);
4432 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004433 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004434 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004435}
4436
Richard Smithb9eab6d2014-03-20 19:44:17 +00004437void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004438 if (DeclUpdates.empty())
4439 return;
4440
Richard Smith59442b42014-03-20 20:07:19 +00004441 DeclUpdateMap LocalUpdates;
4442 LocalUpdates.swap(DeclUpdates);
4443
Richard Smith6ef42932014-03-20 21:02:00 +00004444 for (auto &DeclUpdate : LocalUpdates) {
4445 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004446 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004447 continue; // The decl will be written completely,no need to store updates.
4448
Richard Smith6ef42932014-03-20 21:02:00 +00004449 OffsetsRecord.push_back(GetDeclRef(D));
4450 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4451
Richard Smithd28ac5b2014-03-22 23:33:22 +00004452 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004453 RecordData Record;
4454 for (auto &Update : DeclUpdate.second) {
4455 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4456
4457 Record.push_back(Kind);
4458 switch (Kind) {
4459 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4460 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4461 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4462 Record.push_back(GetDeclRef(Update.getDecl()));
4463 break;
4464
4465 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4466 AddSourceLocation(Update.getLoc(), Record);
4467 break;
4468
Richard Smithd28ac5b2014-03-22 23:33:22 +00004469 case UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION:
4470 // An updated body is emitted last, so that the reader doesn't need
4471 // to skip over the lazy body to reach statements for other records.
4472 Record.pop_back();
4473 HasUpdatedBody = true;
4474 break;
4475
Richard Smith564417a2014-03-20 21:47:22 +00004476 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4477 addExceptionSpec(
4478 *this,
4479 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4480 Record);
4481 break;
4482
Richard Smith6ef42932014-03-20 21:02:00 +00004483 case UPD_CXX_DEDUCED_RETURN_TYPE:
4484 Record.push_back(GetOrCreateTypeID(Update.getType()));
4485 break;
4486
4487 case UPD_DECL_MARKED_USED:
4488 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004489
4490 case UPD_MANGLING_NUMBER:
4491 case UPD_STATIC_LOCAL_NUMBER:
4492 Record.push_back(Update.getNumber());
4493 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004494 }
4495 }
4496
Richard Smithd28ac5b2014-03-22 23:33:22 +00004497 if (HasUpdatedBody) {
4498 const FunctionDecl *Def = cast<FunctionDecl>(D);
4499 Record.push_back(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION);
4500 Record.push_back(Def->isInlined());
4501 AddSourceLocation(Def->getInnerLocStart(), Record);
4502 AddFunctionDefinition(Def, Record);
4503 }
4504
Richard Smith6ef42932014-03-20 21:02:00 +00004505 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004506
Richard Smithb9eab6d2014-03-20 19:44:17 +00004507 // Flush any statements that were written as part of this update record.
4508 FlushStmts();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004509 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004510}
4511
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004512void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004513 if (ReplacedDecls.empty())
4514 return;
4515
4516 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004517 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4518 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004519 Record.push_back(I->ID);
4520 Record.push_back(I->Offset);
4521 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004522 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004523 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004524}
4525
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004526void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004527 Record.push_back(Loc.getRawEncoding());
4528}
4529
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004530void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004531 AddSourceLocation(Range.getBegin(), Record);
4532 AddSourceLocation(Range.getEnd(), Record);
4533}
4534
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004535void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004536 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004537 const uint64_t *Words = Value.getRawData();
4538 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004539}
4540
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004541void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004542 Record.push_back(Value.isUnsigned());
4543 AddAPInt(Value, Record);
4544}
4545
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004546void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004547 AddAPInt(Value.bitcastToAPInt(), Record);
4548}
4549
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004550void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004551 Record.push_back(getIdentifierRef(II));
4552}
4553
Sebastian Redl539c5062010-08-18 23:57:32 +00004554IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004555 if (II == 0)
4556 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004557
Sebastian Redl539c5062010-08-18 23:57:32 +00004558 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004559 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004560 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004561 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004562}
4563
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004564MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004565 // Don't emit builtin macros like __LINE__ to the AST file unless they
4566 // have been redefined by the header (in which case they are not
4567 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004568 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004569 return 0;
4570
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004571 MacroID &ID = MacroIDs[MI];
4572 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004573 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004574 MacroInfoToEmitData Info = { Name, MI, ID };
4575 MacroInfosToEmit.push_back(Info);
4576 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004577 return ID;
4578}
4579
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004580MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4581 if (MI == 0 || MI->isBuiltinMacro())
4582 return 0;
4583
4584 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4585 return MacroIDs[MI];
4586}
4587
4588uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4589 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4590 return IdentMacroDirectivesOffsetMap[Name];
4591}
4592
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004593void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004594 Record.push_back(getSelectorRef(SelRef));
4595}
4596
Sebastian Redl539c5062010-08-18 23:57:32 +00004597SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004598 if (Sel.getAsOpaquePtr() == 0) {
4599 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004600 }
4601
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004602 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004603 if (SID == 0 && Chain) {
4604 // This might trigger a ReadSelector callback, which will set the ID for
4605 // this selector.
4606 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004607 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004608 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004609 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004610 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004611 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004612 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004613 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004614}
4615
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004616void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004617 AddDeclRef(Temp->getDestructor(), Record);
4618}
4619
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004620void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4621 CXXBaseSpecifier const *BasesEnd,
4622 RecordDataImpl &Record) {
4623 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4624 CXXBaseSpecifiersToWrite.push_back(
4625 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4626 Bases, BasesEnd));
4627 Record.push_back(NextCXXBaseSpecifiersID++);
4628}
4629
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004630void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004631 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004632 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004633 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004634 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004635 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004636 break;
4637 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004638 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004639 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004640 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004641 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004642 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004643 break;
4644 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004645 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004646 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004647 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004648 break;
John McCall0ad16662009-10-29 08:12:44 +00004649 case TemplateArgument::Null:
4650 case TemplateArgument::Integral:
4651 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004652 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004653 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004654 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004655 break;
4656 }
4657}
4658
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004659void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004660 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004661 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004662
4663 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4664 bool InfoHasSameExpr
4665 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4666 Record.push_back(InfoHasSameExpr);
4667 if (InfoHasSameExpr)
4668 return; // Avoid storing the same expr twice.
4669 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004670 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4671 Record);
4672}
4673
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004674void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4675 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004676 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004677 AddTypeRef(QualType(), Record);
4678 return;
4679 }
4680
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004681 AddTypeLoc(TInfo->getTypeLoc(), Record);
4682}
4683
4684void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4685 AddTypeRef(TL.getType(), Record);
4686
John McCall8f115c62009-10-16 21:56:05 +00004687 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004688 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004689 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004690}
4691
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004692void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004693 Record.push_back(GetOrCreateTypeID(T));
4694}
4695
Douglas Gregoreda8e122011-08-09 15:13:55 +00004696TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004697 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004698 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004699 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4700}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004701
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004702TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004703 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004704 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004705 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004706}
4707
4708TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4709 if (T.isNull())
4710 return TypeIdx();
4711 assert(!T.getLocalFastQualifiers());
4712
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004713 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004714 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004715 if (DoneWritingDeclsAndTypes) {
4716 assert(0 && "New type seen after serializing all the types to emit!");
4717 return TypeIdx();
4718 }
4719
Douglas Gregor1970d882009-04-26 03:49:13 +00004720 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004721 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004722 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004723 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004724 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004725 return Idx;
4726}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004727
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004728TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004729 if (T.isNull())
4730 return TypeIdx();
4731 assert(!T.getLocalFastQualifiers());
4732
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004733 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4734 assert(I != TypeIdxs.end() && "Type not emitted!");
4735 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004736}
4737
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004738void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004739 Record.push_back(GetDeclRef(D));
4740}
4741
Sebastian Redl539c5062010-08-18 23:57:32 +00004742DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004743 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4744
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004745 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004746 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004747 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004748
4749 // If D comes from an AST file, its declaration ID is already known and
4750 // fixed.
4751 if (D->isFromASTFile())
4752 return D->getGlobalID();
4753
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004754 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004755 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004756 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004757 if (DoneWritingDeclsAndTypes) {
4758 assert(0 && "New decl seen after serializing all the decls to emit!");
4759 return 0;
4760 }
4761
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004762 // We haven't seen this declaration before. Give it a new ID and
4763 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004764 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004765 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004766 }
4767
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004768 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004769}
4770
Sebastian Redl539c5062010-08-18 23:57:32 +00004771DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004772 if (D == 0)
4773 return 0;
4774
Douglas Gregorb3163e52012-01-05 22:33:30 +00004775 // If D comes from an AST file, its declaration ID is already known and
4776 // fixed.
4777 if (D->isFromASTFile())
4778 return D->getGlobalID();
4779
Douglas Gregore84a9da2009-04-20 20:36:09 +00004780 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4781 return DeclIDs[D];
4782}
4783
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004784void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004785 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004786 assert(D);
4787
4788 SourceLocation Loc = D->getLocation();
4789 if (Loc.isInvalid())
4790 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004791
4792 // We only keep track of the file-level declarations of each file.
4793 if (!D->getLexicalDeclContext()->isFileContext())
4794 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004795 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4796 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004797 if (isa<ParmVarDecl>(D))
4798 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004799
4800 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004801 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004802 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004803 FileID FID;
4804 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004805 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004806 if (FID.isInvalid())
4807 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004808 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004809
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004810 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004811 if (!Info)
4812 Info = new DeclIDInFileInfo();
4813
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004814 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004815 LocDeclIDsTy &Decls = Info->DeclIDs;
4816
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004817 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004818 Decls.push_back(LocDecl);
4819 return;
4820 }
4821
Benjamin Kramer45025c02013-08-24 13:22:59 +00004822 LocDeclIDsTy::iterator I =
4823 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004824
4825 Decls.insert(I, LocDecl);
4826}
4827
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004828void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004829 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004830 Record.push_back(Name.getNameKind());
4831 switch (Name.getNameKind()) {
4832 case DeclarationName::Identifier:
4833 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4834 break;
4835
4836 case DeclarationName::ObjCZeroArgSelector:
4837 case DeclarationName::ObjCOneArgSelector:
4838 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004839 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004840 break;
4841
4842 case DeclarationName::CXXConstructorName:
4843 case DeclarationName::CXXDestructorName:
4844 case DeclarationName::CXXConversionFunctionName:
4845 AddTypeRef(Name.getCXXNameType(), Record);
4846 break;
4847
4848 case DeclarationName::CXXOperatorName:
4849 Record.push_back(Name.getCXXOverloadedOperator());
4850 break;
4851
Alexis Hunt3d221f22009-11-29 07:34:05 +00004852 case DeclarationName::CXXLiteralOperatorName:
4853 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4854 break;
4855
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004856 case DeclarationName::CXXUsingDirective:
4857 // No extra data to emit
4858 break;
4859 }
4860}
Chris Lattnerca025db2010-05-07 21:43:38 +00004861
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004862void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004863 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004864 switch (Name.getNameKind()) {
4865 case DeclarationName::CXXConstructorName:
4866 case DeclarationName::CXXDestructorName:
4867 case DeclarationName::CXXConversionFunctionName:
4868 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4869 break;
4870
4871 case DeclarationName::CXXOperatorName:
4872 AddSourceLocation(
4873 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4874 Record);
4875 AddSourceLocation(
4876 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4877 Record);
4878 break;
4879
4880 case DeclarationName::CXXLiteralOperatorName:
4881 AddSourceLocation(
4882 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4883 Record);
4884 break;
4885
4886 case DeclarationName::Identifier:
4887 case DeclarationName::ObjCZeroArgSelector:
4888 case DeclarationName::ObjCOneArgSelector:
4889 case DeclarationName::ObjCMultiArgSelector:
4890 case DeclarationName::CXXUsingDirective:
4891 break;
4892 }
4893}
4894
4895void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004896 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004897 AddDeclarationName(NameInfo.getName(), Record);
4898 AddSourceLocation(NameInfo.getLoc(), Record);
4899 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4900}
4901
4902void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004903 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004904 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004905 Record.push_back(Info.NumTemplParamLists);
4906 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4907 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4908}
4909
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004910void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004911 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004912 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004913 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004914 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004915
4916 // Push each of the NNS's onto a stack for serialization in reverse order.
4917 while (NNS) {
4918 NestedNames.push_back(NNS);
4919 NNS = NNS->getPrefix();
4920 }
4921
4922 Record.push_back(NestedNames.size());
4923 while(!NestedNames.empty()) {
4924 NNS = NestedNames.pop_back_val();
4925 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4926 Record.push_back(Kind);
4927 switch (Kind) {
4928 case NestedNameSpecifier::Identifier:
4929 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4930 break;
4931
4932 case NestedNameSpecifier::Namespace:
4933 AddDeclRef(NNS->getAsNamespace(), Record);
4934 break;
4935
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004936 case NestedNameSpecifier::NamespaceAlias:
4937 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4938 break;
4939
Chris Lattnerca025db2010-05-07 21:43:38 +00004940 case NestedNameSpecifier::TypeSpec:
4941 case NestedNameSpecifier::TypeSpecWithTemplate:
4942 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4943 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4944 break;
4945
4946 case NestedNameSpecifier::Global:
4947 // Don't need to write an associated value.
4948 break;
4949 }
4950 }
4951}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004952
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004953void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4954 RecordDataImpl &Record) {
4955 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004956 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004957 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004958
4959 // Push each of the nested-name-specifiers's onto a stack for
4960 // serialization in reverse order.
4961 while (NNS) {
4962 NestedNames.push_back(NNS);
4963 NNS = NNS.getPrefix();
4964 }
4965
4966 Record.push_back(NestedNames.size());
4967 while(!NestedNames.empty()) {
4968 NNS = NestedNames.pop_back_val();
4969 NestedNameSpecifier::SpecifierKind Kind
4970 = NNS.getNestedNameSpecifier()->getKind();
4971 Record.push_back(Kind);
4972 switch (Kind) {
4973 case NestedNameSpecifier::Identifier:
4974 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4975 AddSourceRange(NNS.getLocalSourceRange(), Record);
4976 break;
4977
4978 case NestedNameSpecifier::Namespace:
4979 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4980 AddSourceRange(NNS.getLocalSourceRange(), Record);
4981 break;
4982
4983 case NestedNameSpecifier::NamespaceAlias:
4984 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4985 AddSourceRange(NNS.getLocalSourceRange(), Record);
4986 break;
4987
4988 case NestedNameSpecifier::TypeSpec:
4989 case NestedNameSpecifier::TypeSpecWithTemplate:
4990 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4991 AddTypeLoc(NNS.getTypeLoc(), Record);
4992 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4993 break;
4994
4995 case NestedNameSpecifier::Global:
4996 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4997 break;
4998 }
4999 }
5000}
5001
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005002void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005003 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005004 Record.push_back(Kind);
5005 switch (Kind) {
5006 case TemplateName::Template:
5007 AddDeclRef(Name.getAsTemplateDecl(), Record);
5008 break;
5009
5010 case TemplateName::OverloadedTemplate: {
5011 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5012 Record.push_back(OvT->size());
5013 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5014 I != E; ++I)
5015 AddDeclRef(*I, Record);
5016 break;
5017 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005018
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005019 case TemplateName::QualifiedTemplate: {
5020 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5021 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5022 Record.push_back(QualT->hasTemplateKeyword());
5023 AddDeclRef(QualT->getTemplateDecl(), Record);
5024 break;
5025 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005026
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005027 case TemplateName::DependentTemplate: {
5028 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5029 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5030 Record.push_back(DepT->isIdentifier());
5031 if (DepT->isIdentifier())
5032 AddIdentifierRef(DepT->getIdentifier(), Record);
5033 else
5034 Record.push_back(DepT->getOperator());
5035 break;
5036 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005037
5038 case TemplateName::SubstTemplateTemplateParm: {
5039 SubstTemplateTemplateParmStorage *subst
5040 = Name.getAsSubstTemplateTemplateParm();
5041 AddDeclRef(subst->getParameter(), Record);
5042 AddTemplateName(subst->getReplacement(), Record);
5043 break;
5044 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005045
5046 case TemplateName::SubstTemplateTemplateParmPack: {
5047 SubstTemplateTemplateParmPackStorage *SubstPack
5048 = Name.getAsSubstTemplateTemplateParmPack();
5049 AddDeclRef(SubstPack->getParameterPack(), Record);
5050 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5051 break;
5052 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005053 }
5054}
5055
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005056void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005057 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005058 Record.push_back(Arg.getKind());
5059 switch (Arg.getKind()) {
5060 case TemplateArgument::Null:
5061 break;
5062 case TemplateArgument::Type:
5063 AddTypeRef(Arg.getAsType(), Record);
5064 break;
5065 case TemplateArgument::Declaration:
5066 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005067 Record.push_back(Arg.isDeclForReferenceParam());
5068 break;
5069 case TemplateArgument::NullPtr:
5070 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005071 break;
5072 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005073 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005074 AddTypeRef(Arg.getIntegralType(), Record);
5075 break;
5076 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005077 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5078 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005079 case TemplateArgument::TemplateExpansion:
5080 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005081 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005082 Record.push_back(*NumExpansions + 1);
5083 else
5084 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005085 break;
5086 case TemplateArgument::Expression:
5087 AddStmt(Arg.getAsExpr());
5088 break;
5089 case TemplateArgument::Pack:
5090 Record.push_back(Arg.pack_size());
5091 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
5092 I != E; ++I)
5093 AddTemplateArgument(*I, Record);
5094 break;
5095 }
5096}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005097
5098void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005099ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005100 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005101 assert(TemplateParams && "No TemplateParams!");
5102 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5103 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5104 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5105 Record.push_back(TemplateParams->size());
5106 for (TemplateParameterList::const_iterator
5107 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5108 P != PEnd; ++P)
5109 AddDeclRef(*P, Record);
5110}
5111
5112/// \brief Emit a template argument list.
5113void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005114ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005115 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005116 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005117 Record.push_back(TemplateArgs->size());
5118 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005119 AddTemplateArgument(TemplateArgs->get(i), Record);
5120}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005121
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005122void
5123ASTWriter::AddASTTemplateArgumentListInfo
5124(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5125 assert(ASTTemplArgList && "No ASTTemplArgList!");
5126 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5127 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5128 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5129 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5130 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5131 AddTemplateArgumentLoc(TemplArgs[i], Record);
5132}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005133
5134void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005135ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005136 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005137 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005138 I = Set.begin(), E = Set.end(); I != E; ++I) {
5139 AddDeclRef(I.getDecl(), Record);
5140 Record.push_back(I.getAccess());
5141 }
5142}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005143
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005144void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005145 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005146 Record.push_back(Base.isVirtual());
5147 Record.push_back(Base.isBaseOfClass());
5148 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005149 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005150 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005151 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005152 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5153 : SourceLocation(),
5154 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005155}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005156
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005157void ASTWriter::FlushCXXBaseSpecifiers() {
5158 RecordData Record;
5159 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5160 Record.clear();
5161
5162 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005163 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005164 if (Index == CXXBaseSpecifiersOffsets.size())
5165 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5166 else {
5167 if (Index > CXXBaseSpecifiersOffsets.size())
5168 CXXBaseSpecifiersOffsets.resize(Index + 1);
5169 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5170 }
5171
5172 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5173 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5174 Record.push_back(BEnd - B);
5175 for (; B != BEnd; ++B)
5176 AddCXXBaseSpecifier(*B, Record);
5177 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005178
5179 // Flush any expressions that were written as part of the base specifiers.
5180 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005181 }
5182
5183 CXXBaseSpecifiersToWrite.clear();
5184}
5185
Alexis Hunt1d792652011-01-08 20:30:50 +00005186void ASTWriter::AddCXXCtorInitializers(
5187 const CXXCtorInitializer * const *CtorInitializers,
5188 unsigned NumCtorInitializers,
5189 RecordDataImpl &Record) {
5190 Record.push_back(NumCtorInitializers);
5191 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5192 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005193
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005194 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005195 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005196 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005197 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005198 } else if (Init->isDelegatingInitializer()) {
5199 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005200 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005201 } else if (Init->isMemberInitializer()){
5202 Record.push_back(CTOR_INITIALIZER_MEMBER);
5203 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005204 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005205 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5206 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005207 }
Francois Pichetd583da02010-12-04 09:14:42 +00005208
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005209 AddSourceLocation(Init->getMemberLocation(), Record);
5210 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005211 AddSourceLocation(Init->getLParenLoc(), Record);
5212 AddSourceLocation(Init->getRParenLoc(), Record);
5213 Record.push_back(Init->isWritten());
5214 if (Init->isWritten()) {
5215 Record.push_back(Init->getSourceOrder());
5216 } else {
5217 Record.push_back(Init->getNumArrayIndices());
5218 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5219 AddDeclRef(Init->getArrayIndex(i), Record);
5220 }
5221 }
5222}
5223
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005224void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5225 assert(D->DefinitionData);
5226 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005227 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005228 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005229 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005230 Record.push_back(Data.Aggregate);
5231 Record.push_back(Data.PlainOldData);
5232 Record.push_back(Data.Empty);
5233 Record.push_back(Data.Polymorphic);
5234 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005235 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005236 Record.push_back(Data.HasNoNonEmptyBases);
5237 Record.push_back(Data.HasPrivateFields);
5238 Record.push_back(Data.HasProtectedFields);
5239 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005240 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005241 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005242 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005243 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005244 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005245 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5246 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5247 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5248 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5249 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5250 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005251 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005252 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005253 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005254 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005255 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005256 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005257 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005258 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005259 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005260 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005261 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5262 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5263 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5264 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005265 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005266
5267 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005268 if (Data.NumBases > 0)
5269 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5270 Record);
5271
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005272 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5273 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005274 if (Data.NumVBases > 0)
5275 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5276 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005277
Richard Smitha4ba74c2013-08-30 04:46:40 +00005278 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5279 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005280 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005281 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005282
5283 // Add lambda-specific data.
5284 if (Data.IsLambda) {
5285 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005286 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005287 Record.push_back(Lambda.IsGenericLambda);
5288 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005289 Record.push_back(Lambda.NumCaptures);
5290 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005291 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005292 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005293 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005294 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5295 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5296 AddSourceLocation(Capture.getLocation(), Record);
5297 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005298 Record.push_back(Capture.getCaptureKind());
5299 switch (Capture.getCaptureKind()) {
5300 case LCK_This:
5301 break;
5302 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005303 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005304 VarDecl *Var =
5305 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5306 AddDeclRef(Var, Record);
5307 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5308 : SourceLocation(),
5309 Record);
5310 break;
5311 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005312 }
5313 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005314}
5315
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005316void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005317 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005318 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005319 assert(FirstDeclID == NextDeclID &&
5320 FirstTypeID == NextTypeID &&
5321 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005322 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005323 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005324 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005325 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005326
Sebastian Redl07a89a82010-07-30 00:29:29 +00005327 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005328
Douglas Gregordf0c1512011-08-18 04:12:04 +00005329 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5330 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5331 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005332 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005333 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005334 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005335 NextDeclID = FirstDeclID;
5336 NextTypeID = FirstTypeID;
5337 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005338 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005339 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005340 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005341}
5342
Sebastian Redl539c5062010-08-18 23:57:32 +00005343void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005344 // Always keep the highest ID. See \p TypeRead() for more information.
5345 IdentID &StoredID = IdentifierIDs[II];
5346 if (ID > StoredID)
5347 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005348}
5349
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005350void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005351 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005352 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005353 if (ID > StoredID)
5354 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005355}
5356
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005357void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005358 // Always take the highest-numbered type index. This copes with an interesting
5359 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005360 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005361 // keep the higher-numbered entry so that we can properly write it out to
5362 // the AST file.
5363 TypeIdx &StoredIdx = TypeIdxs[T];
5364 if (Idx.getIndex() >= StoredIdx.getIndex())
5365 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005366}
5367
Sebastian Redl539c5062010-08-18 23:57:32 +00005368void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005369 // Always keep the highest ID. See \p TypeRead() for more information.
5370 SelectorID &StoredID = SelectorIDs[S];
5371 if (ID > StoredID)
5372 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005373}
Douglas Gregor91096292010-10-02 19:29:26 +00005374
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005375void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005376 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005377 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005378 MacroDefinitions[MD] = ID;
5379}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005380
Douglas Gregore37a85a2011-12-02 17:30:13 +00005381void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5382 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5383 SubmoduleIDs[Mod] = ID;
5384}
5385
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005386void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005387 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005388 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005389 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5390 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005391 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005392 // A forward reference was mutated into a definition. Rewrite it.
5393 // FIXME: This happens during template instantiation, should we
5394 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005395 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005396 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005397 }
5398}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005399
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005400void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005401 assert(!WritingAST && "Already writing the AST!");
5402
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005403 // TU and namespaces are handled elsewhere.
5404 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5405 return;
5406
Douglas Gregorb3722e22011-09-09 23:01:35 +00005407 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005408 return; // Not a source decl added to a DeclContext from PCH.
5409
Douglas Gregor9f782892013-01-21 15:25:38 +00005410 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005411 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005412 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005413}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005414
5415void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005416 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005417 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005418 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005419 return; // Not a source member added to a class from PCH.
5420 if (!isa<CXXMethodDecl>(D))
5421 return; // We are interested in lazily declared implicit methods.
5422
5423 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005424 assert(RD->isCompleteDefinition());
Aaron Ballman4f45b712014-03-21 15:22:56 +00005425 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005426}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005427
5428void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5429 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005430 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005431 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005432 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005433 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005434 return; // Not a source specialization added to a template from PCH.
5435
Aaron Ballman4f45b712014-03-21 15:22:56 +00005436 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5437 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005438}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005439
Larisse Voufo39a1e502013-08-06 01:03:05 +00005440void ASTWriter::AddedCXXTemplateSpecialization(
5441 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5442 // The specializations set is kept in the canonical template.
5443 assert(!WritingAST && "Already writing the AST!");
5444 TD = TD->getCanonicalDecl();
5445 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5446 return; // Not a source specialization added to a template from PCH.
5447
Aaron Ballman4f45b712014-03-21 15:22:56 +00005448 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5449 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005450}
5451
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005452void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5453 const FunctionDecl *D) {
5454 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005455 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005456 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005457 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005458 return; // Not a source specialization added to a template from PCH.
5459
Aaron Ballman4f45b712014-03-21 15:22:56 +00005460 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5461 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005462}
5463
Richard Smith564417a2014-03-20 21:47:22 +00005464void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5465 assert(!WritingAST && "Already writing the AST!");
5466 FD = FD->getCanonicalDecl();
5467 if (!FD->isFromASTFile())
5468 return; // Not a function declared in PCH and defined outside.
5469
5470 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5471}
5472
Richard Smith1fa5d642013-05-11 05:45:24 +00005473void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5474 assert(!WritingAST && "Already writing the AST!");
5475 FD = FD->getCanonicalDecl();
5476 if (!FD->isFromASTFile())
5477 return; // Not a function declared in PCH and defined outside.
5478
Aaron Ballman4f45b712014-03-21 15:22:56 +00005479 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith1fa5d642013-05-11 05:45:24 +00005480}
5481
Sebastian Redlab238a72011-04-24 16:28:06 +00005482void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005483 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005484 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005485 return; // Declaration not imported from PCH.
5486
5487 // Implicit decl from a PCH was defined.
5488 // FIXME: Should implicit definition be a separate FunctionDecl?
5489 RewriteDecl(D);
5490}
5491
Richard Smithd28ac5b2014-03-22 23:33:22 +00005492void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5493 assert(!WritingAST && "Already writing the AST!");
5494 if (!D->isFromASTFile())
5495 return;
5496
5497 // Since the actual instantiation is delayed, this really means that we need
5498 // to update the instantiation location.
5499 DeclUpdates[D].push_back(
5500 DeclUpdate(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION));
5501}
5502
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005503void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005504 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005505 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005506 return;
5507
5508 // Since the actual instantiation is delayed, this really means that we need
5509 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005510 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005511 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5512 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005513}
5514
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005515void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5516 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005517 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005518 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005519 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005520
5521 assert(IFD->getDefinition() && "Category on a class without a definition?");
5522 ObjCClassesWithCategories.insert(
5523 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005524}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005525
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005526
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005527void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5528 const ObjCPropertyDecl *OrigProp,
5529 const ObjCCategoryDecl *ClassExt) {
5530 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5531 if (!D)
5532 return;
5533
5534 assert(!WritingAST && "Already writing the AST!");
5535 if (!D->isFromASTFile())
5536 return; // Declaration not imported from PCH.
5537
5538 RewriteDecl(D);
5539}
Eli Friedman276dd182013-09-05 00:02:25 +00005540
5541void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5542 assert(!WritingAST && "Already writing the AST!");
5543 if (!D->isFromASTFile())
5544 return;
5545
Aaron Ballman4f45b712014-03-21 15:22:56 +00005546 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005547}