blob: 973d8a28e1412ce7e4f3419fcc02560c1575f611 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Richard Smith961eae52014-03-25 01:14:22 +000020#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000026#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000028#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000032#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000034#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000035#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/HeaderSearchOptions.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Lex/PreprocessingRecord.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Lex/PreprocessorOptions.h"
41#include "clang/Sema/IdentifierResolver.h"
42#include "clang/Sema/Sema.h"
43#include "clang/Serialization/ASTReader.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000044#include "llvm/ADT/APFloat.h"
45#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000047#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include "llvm/Bitcode/BitstreamWriter.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000049#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000050#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000051#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000052#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000053#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000054#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000055#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000056#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000057#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000058using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000059using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000060
Sebastian Redl3df5a082010-07-30 17:03:48 +000061template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000062static StringRef data(const std::vector<T, Allocator> &v) {
63 if (v.empty()) return StringRef();
64 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000066}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000067
68template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000069static StringRef data(const SmallVectorImpl<T> &v) {
70 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000071 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000072}
73
Douglas Gregoref84c4b2009-04-09 22:27:44 +000074//===----------------------------------------------------------------------===//
75// Type serialization
76//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000077
Douglas Gregoref84c4b2009-04-09 22:27:44 +000078namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000079 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000080 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000081 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082
83 public:
84 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000085 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +000086 /// \brief Abbreviation to use for the record, if any.
87 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000089 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000090 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000091
92 void VisitArrayType(const ArrayType *T);
93 void VisitFunctionType(const FunctionType *T);
94 void VisitTagType(const TagType *T);
95
96#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
97#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098#include "clang/AST/TypeNodes.def"
99 };
100}
101
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000103 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000113 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114}
115
Reid Kleckner8a365022013-06-24 17:51:48 +0000116void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
117 Writer.AddTypeRef(T->getOriginalType(), Record);
118 Code = TYPE_DECAYED;
119}
120
Reid Kleckner0503a872013-12-05 01:23:43 +0000121void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
122 Writer.AddTypeRef(T->getOriginalType(), Record);
123 Writer.AddTypeRef(T->getAdjustedType(), Record);
124 Code = TYPE_ADJUSTED;
125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000128 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000129 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130}
131
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000133 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
134 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000139 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000140 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000141}
142
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000143void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000144 Writer.AddTypeRef(T->getPointeeType(), Record);
145 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000146 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147}
148
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150 Writer.AddTypeRef(T->getElementType(), Record);
151 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000152 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153}
154
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156 VisitArrayType(T);
157 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000163 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000168 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
169 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000170 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000171 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000172}
173
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000174void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175 Writer.AddTypeRef(T->getElementType(), Record);
176 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000177 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000178 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179}
180
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000182 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000183 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184}
185
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000187 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000188 FunctionType::ExtInfo C = T->getExtInfo();
189 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000190 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000191 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000192 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000193 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000194 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000195
196 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
197 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198}
199
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000200void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000201 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000202 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000203}
204
Richard Smith564417a2014-03-20 21:47:22 +0000205static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
206 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000207 Record.push_back(T->getExceptionSpecType());
208 if (T->getExceptionSpecType() == EST_Dynamic) {
209 Record.push_back(T->getNumExceptions());
210 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
211 Writer.AddTypeRef(T->getExceptionType(I), Record);
212 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
213 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000214 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
215 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
216 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000217 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
218 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000219 }
Richard Smith564417a2014-03-20 21:47:22 +0000220}
221
222void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
223 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000224
Richard Smith564417a2014-03-20 21:47:22 +0000225 Record.push_back(T->isVariadic());
226 Record.push_back(T->hasTrailingReturn());
227 Record.push_back(T->getTypeQuals());
228 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
229 addExceptionSpec(Writer, T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000230
231 Record.push_back(T->getNumParams());
232 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
233 Writer.AddTypeRef(T->getParamType(I), Record);
234
235 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
236 T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
237 AbbrevToUse = 0;
238
Sebastian Redl539c5062010-08-18 23:57:32 +0000239 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240}
241
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000242void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000243 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000244 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000245}
John McCallb96ec562009-12-04 22:46:56 +0000246
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000249 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
250 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000251 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000252}
253
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000255 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000256 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000257}
258
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000259void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000261 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262}
263
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000264void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000265 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000266 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000267 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000268}
269
Alexis Hunte852b102011-05-24 22:41:36 +0000270void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
271 Writer.AddTypeRef(T->getBaseType(), Record);
272 Writer.AddTypeRef(T->getUnderlyingType(), Record);
273 Record.push_back(T->getUTTKind());
274 Code = TYPE_UNARY_TRANSFORM;
275}
276
Richard Smith30482bc2011-02-20 03:19:35 +0000277void ASTTypeWriter::VisitAutoType(const AutoType *T) {
278 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000279 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000280 if (T->getDeducedType().isNull())
281 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000282 Code = TYPE_AUTO;
283}
284
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000285void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000286 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000287 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000288 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000289 "Cannot serialize in the middle of a type definition");
290}
291
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000292void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000293 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000294 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000295}
296
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000297void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000298 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000299 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000300}
301
John McCall81904512011-01-06 01:58:22 +0000302void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
303 Writer.AddTypeRef(T->getModifiedType(), Record);
304 Writer.AddTypeRef(T->getEquivalentType(), Record);
305 Record.push_back(T->getAttrKind());
306 Code = TYPE_ATTRIBUTED;
307}
308
Mike Stump11289f42009-09-09 15:08:12 +0000309void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000310ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000311 const SubstTemplateTypeParmType *T) {
312 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
313 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000314 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000315}
316
317void
Douglas Gregorada4b792011-01-14 02:55:32 +0000318ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
319 const SubstTemplateTypeParmPackType *T) {
320 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
321 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
322 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
323}
324
325void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000326ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000327 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000328 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000329 Writer.AddTemplateName(T->getTemplateName(), Record);
330 Record.push_back(T->getNumArgs());
331 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
332 ArgI != ArgE; ++ArgI)
333 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000334 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
335 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000336 : T->getCanonicalTypeInternal(),
337 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000338 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000339}
340
341void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000342ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000343 VisitArrayType(T);
344 Writer.AddStmt(T->getSizeExpr());
345 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000346 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000347}
348
349void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000350ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000351 const DependentSizedExtVectorType *T) {
352 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000353 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000354}
355
356void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000357ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000358 Record.push_back(T->getDepth());
359 Record.push_back(T->getIndex());
360 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000361 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000362 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000363}
364
365void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000366ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000367 Record.push_back(T->getKeyword());
368 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
369 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000370 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
371 : T->getCanonicalTypeInternal(),
372 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000373 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000374}
375
376void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000377ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000378 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000379 Record.push_back(T->getKeyword());
380 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
381 Writer.AddIdentifierRef(T->getIdentifier(), Record);
382 Record.push_back(T->getNumArgs());
383 for (DependentTemplateSpecializationType::iterator
384 I = T->begin(), E = T->end(); I != E; ++I)
385 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000386 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000387}
388
Douglas Gregord2fa7662010-12-20 02:24:11 +0000389void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
390 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000391 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000392 Record.push_back(*NumExpansions + 1);
393 else
394 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000395 Code = TYPE_PACK_EXPANSION;
396}
397
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000398void ASTTypeWriter::VisitParenType(const ParenType *T) {
399 Writer.AddTypeRef(T->getInnerType(), Record);
400 Code = TYPE_PAREN;
401}
402
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000403void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000404 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000405 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
406 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000407 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000408}
409
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000410void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000411 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000412 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000413 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000414}
415
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000416void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000417 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000418 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000419}
420
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000421void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000422 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000423 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000424 for (const auto *I : T->quals())
425 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000426 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000427}
428
Steve Narofffb4330f2009-06-17 22:40:22 +0000429void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000430ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000431 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000432 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000433}
434
Eli Friedman0dfb8892011-10-06 23:00:33 +0000435void
436ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
437 Writer.AddTypeRef(T->getValueType(), Record);
438 Code = TYPE_ATOMIC;
439}
440
John McCall8f115c62009-10-16 21:56:05 +0000441namespace {
442
443class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000444 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000445 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000446
447public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000448 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000449 : Writer(Writer), Record(Record) { }
450
John McCall17001972009-10-18 01:05:36 +0000451#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000452#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000453 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000454#include "clang/AST/TypeLocNodes.def"
455
John McCall17001972009-10-18 01:05:36 +0000456 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
457 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000458};
459
460}
461
John McCall17001972009-10-18 01:05:36 +0000462void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
463 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000464}
John McCall17001972009-10-18 01:05:36 +0000465void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000466 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
467 if (TL.needsExtraLocalData()) {
468 Record.push_back(TL.getWrittenTypeSpec());
469 Record.push_back(TL.getWrittenSignSpec());
470 Record.push_back(TL.getWrittenWidthSpec());
471 Record.push_back(TL.hasModeAttr());
472 }
John McCall8f115c62009-10-16 21:56:05 +0000473}
John McCall17001972009-10-18 01:05:36 +0000474void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
475 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000476}
John McCall17001972009-10-18 01:05:36 +0000477void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000479}
Reid Kleckner8a365022013-06-24 17:51:48 +0000480void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
481 // nothing to do
482}
Reid Kleckner0503a872013-12-05 01:23:43 +0000483void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
484 // nothing to do
485}
John McCall17001972009-10-18 01:05:36 +0000486void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000488}
John McCall17001972009-10-18 01:05:36 +0000489void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000491}
John McCall17001972009-10-18 01:05:36 +0000492void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000494}
John McCall17001972009-10-18 01:05:36 +0000495void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000497 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000498}
John McCall17001972009-10-18 01:05:36 +0000499void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
501 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
502 Record.push_back(TL.getSizeExpr() ? 1 : 0);
503 if (TL.getSizeExpr())
504 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000505}
John McCall17001972009-10-18 01:05:36 +0000506void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
507 VisitArrayTypeLoc(TL);
508}
509void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
510 VisitArrayTypeLoc(TL);
511}
512void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
513 VisitArrayTypeLoc(TL);
514}
515void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
516 DependentSizedArrayTypeLoc TL) {
517 VisitArrayTypeLoc(TL);
518}
519void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
520 DependentSizedExtVectorTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
523void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getNameLoc(), Record);
525}
526void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
529void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000530 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000531 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
532 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000533 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000534 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
535 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000536}
537void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
538 VisitFunctionTypeLoc(TL);
539}
540void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
541 VisitFunctionTypeLoc(TL);
542}
John McCallb96ec562009-12-04 22:46:56 +0000543void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
544 Writer.AddSourceLocation(TL.getNameLoc(), Record);
545}
John McCall17001972009-10-18 01:05:36 +0000546void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getNameLoc(), Record);
548}
549void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000550 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
551 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
552 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000553}
554void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000555 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
556 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
557 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
558 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000559}
560void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
562}
Alexis Hunte852b102011-05-24 22:41:36 +0000563void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
564 Writer.AddSourceLocation(TL.getKWLoc(), Record);
565 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
566 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
567 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
568}
Richard Smith30482bc2011-02-20 03:19:35 +0000569void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
570 Writer.AddSourceLocation(TL.getNameLoc(), Record);
571}
John McCall17001972009-10-18 01:05:36 +0000572void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
573 Writer.AddSourceLocation(TL.getNameLoc(), Record);
574}
575void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
576 Writer.AddSourceLocation(TL.getNameLoc(), Record);
577}
John McCall81904512011-01-06 01:58:22 +0000578void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
579 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
580 if (TL.hasAttrOperand()) {
581 SourceRange range = TL.getAttrOperandParensRange();
582 Writer.AddSourceLocation(range.getBegin(), Record);
583 Writer.AddSourceLocation(range.getEnd(), Record);
584 }
585 if (TL.hasAttrExprOperand()) {
586 Expr *operand = TL.getAttrExprOperand();
587 Record.push_back(operand ? 1 : 0);
588 if (operand) Writer.AddStmt(operand);
589 } else if (TL.hasAttrEnumOperand()) {
590 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
591 }
592}
John McCall17001972009-10-18 01:05:36 +0000593void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
594 Writer.AddSourceLocation(TL.getNameLoc(), Record);
595}
John McCallcebee162009-10-18 09:09:24 +0000596void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
597 SubstTemplateTypeParmTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getNameLoc(), Record);
599}
Douglas Gregorada4b792011-01-14 02:55:32 +0000600void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
601 SubstTemplateTypeParmPackTypeLoc TL) {
602 Writer.AddSourceLocation(TL.getNameLoc(), Record);
603}
John McCall17001972009-10-18 01:05:36 +0000604void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
605 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000606 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000607 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
608 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
609 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
610 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000611 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
612 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000613}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000614void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
615 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
616 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
617}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000618void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000619 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000620 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000621}
John McCalle78aac42010-03-10 03:28:59 +0000622void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
623 Writer.AddSourceLocation(TL.getNameLoc(), Record);
624}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000625void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000626 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000627 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000628 Writer.AddSourceLocation(TL.getNameLoc(), Record);
629}
John McCallc392f372010-06-11 00:33:02 +0000630void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
631 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000632 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000633 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000634 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000635 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000636 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
637 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
638 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000639 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
640 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000641}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000642void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
643 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
644}
John McCall17001972009-10-18 01:05:36 +0000645void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
646 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000647}
648void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
649 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000650 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
651 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
652 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
653 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000654}
John McCallfc93cf92009-10-22 22:37:11 +0000655void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
656 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000657}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000658void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
659 Writer.AddSourceLocation(TL.getKWLoc(), Record);
660 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
661 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
662}
John McCall8f115c62009-10-16 21:56:05 +0000663
Richard Smith01b2cb42014-07-26 06:37:51 +0000664void ASTWriter::WriteTypeAbbrevs() {
665 using namespace llvm;
666
667 BitCodeAbbrev *Abv;
668
669 // Abbreviation for TYPE_EXT_QUAL
670 Abv = new BitCodeAbbrev();
671 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
672 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
673 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
674 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
675
676 // Abbreviation for TYPE_FUNCTION_PROTO
677 Abv = new BitCodeAbbrev();
678 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
679 // FunctionType
680 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
682 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
683 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
684 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
685 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
686 // FunctionProtoType
687 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
688 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
689 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
690 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
691 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
692 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
694 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
695 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
696}
697
Chris Lattner19cea4e2009-04-22 05:57:30 +0000698//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000699// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000700//===----------------------------------------------------------------------===//
701
Chris Lattner28fa4e62009-04-26 22:26:21 +0000702static void EmitBlockID(unsigned ID, const char *Name,
703 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000704 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000705 Record.clear();
706 Record.push_back(ID);
707 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
708
709 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000710 if (!Name || Name[0] == 0)
711 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000712 Record.clear();
713 while (*Name)
714 Record.push_back(*Name++);
715 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
716}
717
718static void EmitRecordID(unsigned ID, const char *Name,
719 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000720 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000721 Record.clear();
722 Record.push_back(ID);
723 while (*Name)
724 Record.push_back(*Name++);
725 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000726}
727
728static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000729 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000730#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000731 RECORD(STMT_STOP);
732 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000733 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000734 RECORD(STMT_NULL);
735 RECORD(STMT_COMPOUND);
736 RECORD(STMT_CASE);
737 RECORD(STMT_DEFAULT);
738 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000739 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000740 RECORD(STMT_IF);
741 RECORD(STMT_SWITCH);
742 RECORD(STMT_WHILE);
743 RECORD(STMT_DO);
744 RECORD(STMT_FOR);
745 RECORD(STMT_GOTO);
746 RECORD(STMT_INDIRECT_GOTO);
747 RECORD(STMT_CONTINUE);
748 RECORD(STMT_BREAK);
749 RECORD(STMT_RETURN);
750 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000751 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000752 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000753 RECORD(EXPR_PREDEFINED);
754 RECORD(EXPR_DECL_REF);
755 RECORD(EXPR_INTEGER_LITERAL);
756 RECORD(EXPR_FLOATING_LITERAL);
757 RECORD(EXPR_IMAGINARY_LITERAL);
758 RECORD(EXPR_STRING_LITERAL);
759 RECORD(EXPR_CHARACTER_LITERAL);
760 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000761 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000762 RECORD(EXPR_UNARY_OPERATOR);
763 RECORD(EXPR_SIZEOF_ALIGN_OF);
764 RECORD(EXPR_ARRAY_SUBSCRIPT);
765 RECORD(EXPR_CALL);
766 RECORD(EXPR_MEMBER);
767 RECORD(EXPR_BINARY_OPERATOR);
768 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
769 RECORD(EXPR_CONDITIONAL_OPERATOR);
770 RECORD(EXPR_IMPLICIT_CAST);
771 RECORD(EXPR_CSTYLE_CAST);
772 RECORD(EXPR_COMPOUND_LITERAL);
773 RECORD(EXPR_EXT_VECTOR_ELEMENT);
774 RECORD(EXPR_INIT_LIST);
775 RECORD(EXPR_DESIGNATED_INIT);
776 RECORD(EXPR_IMPLICIT_VALUE_INIT);
777 RECORD(EXPR_VA_ARG);
778 RECORD(EXPR_ADDR_LABEL);
779 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000780 RECORD(EXPR_CHOOSE);
781 RECORD(EXPR_GNU_NULL);
782 RECORD(EXPR_SHUFFLE_VECTOR);
783 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000784 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000785 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000786 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000787 RECORD(EXPR_OBJC_ARRAY_LITERAL);
788 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000789 RECORD(EXPR_OBJC_ENCODE);
790 RECORD(EXPR_OBJC_SELECTOR_EXPR);
791 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
792 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
793 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
794 RECORD(EXPR_OBJC_KVC_REF_EXPR);
795 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000796 RECORD(STMT_OBJC_FOR_COLLECTION);
797 RECORD(STMT_OBJC_CATCH);
798 RECORD(STMT_OBJC_FINALLY);
799 RECORD(STMT_OBJC_AT_TRY);
800 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
801 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000802 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000803 RECORD(STMT_CXX_CATCH);
804 RECORD(STMT_CXX_TRY);
805 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000806 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000807 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000808 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000809 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000810 RECORD(EXPR_CXX_STATIC_CAST);
811 RECORD(EXPR_CXX_DYNAMIC_CAST);
812 RECORD(EXPR_CXX_REINTERPRET_CAST);
813 RECORD(EXPR_CXX_CONST_CAST);
814 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000815 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000816 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000817 RECORD(EXPR_CXX_BOOL_LITERAL);
818 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000819 RECORD(EXPR_CXX_TYPEID_EXPR);
820 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000821 RECORD(EXPR_CXX_THIS);
822 RECORD(EXPR_CXX_THROW);
823 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000824 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000825 RECORD(EXPR_CXX_BIND_TEMPORARY);
826 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
827 RECORD(EXPR_CXX_NEW);
828 RECORD(EXPR_CXX_DELETE);
829 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
830 RECORD(EXPR_EXPR_WITH_CLEANUPS);
831 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
832 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
833 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
834 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
835 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000836 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000837 RECORD(EXPR_CXX_NOEXCEPT);
838 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000839 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
840 RECORD(EXPR_TYPE_TRAIT);
841 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000842 RECORD(EXPR_PACK_EXPANSION);
843 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000844 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000845 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000846 RECORD(EXPR_FUNCTION_PARM_PACK);
847 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000848 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000849 RECORD(EXPR_CXX_UUIDOF_EXPR);
850 RECORD(EXPR_CXX_UUIDOF_TYPE);
851 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000852#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000853}
Mike Stump11289f42009-09-09 15:08:12 +0000854
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000855void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000856 RecordData Record;
857 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000858
Sebastian Redl539c5062010-08-18 23:57:32 +0000859#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
860#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000861
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000862 // Control Block.
863 BLOCK(CONTROL_BLOCK);
864 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000865 RECORD(MODULE_NAME);
866 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000867 RECORD(IMPORTS);
868 RECORD(LANGUAGE_OPTIONS);
869 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000870 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000871 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000872 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000873 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000874 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000875 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000876 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000877 RECORD(PREPROCESSOR_OPTIONS);
878
Douglas Gregor108cb222012-10-19 00:45:00 +0000879 BLOCK(INPUT_FILES_BLOCK);
880 RECORD(INPUT_FILE);
881
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000882 // AST Top-Level Block.
883 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000884 RECORD(TYPE_OFFSET);
885 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000886 RECORD(IDENTIFIER_OFFSET);
887 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000888 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000889 RECORD(SPECIAL_TYPES);
890 RECORD(STATISTICS);
891 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000892 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000893 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000894 RECORD(SELECTOR_OFFSETS);
895 RECORD(METHOD_POOL);
896 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000897 RECORD(SOURCE_LOCATION_OFFSETS);
898 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000899 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000900 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000901 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000902 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000903 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000904 RECORD(SEMA_DECL_REFS);
905 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
906 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
907 RECORD(DECL_REPLACEMENTS);
908 RECORD(UPDATE_VISIBLE);
909 RECORD(DECL_UPDATE_OFFSETS);
910 RECORD(DECL_UPDATES);
911 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
912 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000913 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000914 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000915 RECORD(FP_PRAGMA_OPTIONS);
916 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000917 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000918 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000919 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000920 RECORD(MODULE_OFFSET_MAP);
921 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000922 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000923 RECORD(FILE_SORTED_DECLS);
924 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000925 RECORD(MERGED_DECLARATIONS);
926 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000927 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000928 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000929 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000930 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000931 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Douglas Gregor358cd442012-01-15 16:58:34 +0000932
Chris Lattner28fa4e62009-04-26 22:26:21 +0000933 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000934 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000935 RECORD(SM_SLOC_FILE_ENTRY);
936 RECORD(SM_SLOC_BUFFER_ENTRY);
937 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000938 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000939
Chris Lattner28fa4e62009-04-26 22:26:21 +0000940 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000941 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000942 RECORD(PP_MACRO_OBJECT_LIKE);
943 RECORD(PP_MACRO_FUNCTION_LIKE);
944 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000945
Douglas Gregor12bfa382009-10-17 00:13:19 +0000946 // Decls and Types block.
947 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000948 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000949 RECORD(TYPE_COMPLEX);
950 RECORD(TYPE_POINTER);
951 RECORD(TYPE_BLOCK_POINTER);
952 RECORD(TYPE_LVALUE_REFERENCE);
953 RECORD(TYPE_RVALUE_REFERENCE);
954 RECORD(TYPE_MEMBER_POINTER);
955 RECORD(TYPE_CONSTANT_ARRAY);
956 RECORD(TYPE_INCOMPLETE_ARRAY);
957 RECORD(TYPE_VARIABLE_ARRAY);
958 RECORD(TYPE_VECTOR);
959 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000960 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000961 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000962 RECORD(TYPE_TYPEDEF);
963 RECORD(TYPE_TYPEOF_EXPR);
964 RECORD(TYPE_TYPEOF);
965 RECORD(TYPE_RECORD);
966 RECORD(TYPE_ENUM);
967 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000968 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000969 RECORD(TYPE_DECLTYPE);
970 RECORD(TYPE_ELABORATED);
971 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
972 RECORD(TYPE_UNRESOLVED_USING);
973 RECORD(TYPE_INJECTED_CLASS_NAME);
974 RECORD(TYPE_OBJC_OBJECT);
975 RECORD(TYPE_TEMPLATE_TYPE_PARM);
976 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
977 RECORD(TYPE_DEPENDENT_NAME);
978 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
979 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
980 RECORD(TYPE_PAREN);
981 RECORD(TYPE_PACK_EXPANSION);
982 RECORD(TYPE_ATTRIBUTED);
983 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000984 RECORD(TYPE_AUTO);
985 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000986 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000987 RECORD(TYPE_DECAYED);
988 RECORD(TYPE_ADJUSTED);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000989 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +0000990 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000991 RECORD(DECL_ENUM);
992 RECORD(DECL_RECORD);
993 RECORD(DECL_ENUM_CONSTANT);
994 RECORD(DECL_FUNCTION);
995 RECORD(DECL_OBJC_METHOD);
996 RECORD(DECL_OBJC_INTERFACE);
997 RECORD(DECL_OBJC_PROTOCOL);
998 RECORD(DECL_OBJC_IVAR);
999 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001000 RECORD(DECL_OBJC_CATEGORY);
1001 RECORD(DECL_OBJC_CATEGORY_IMPL);
1002 RECORD(DECL_OBJC_IMPLEMENTATION);
1003 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1004 RECORD(DECL_OBJC_PROPERTY);
1005 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001006 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001007 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001008 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001009 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001010 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001011 RECORD(DECL_FILE_SCOPE_ASM);
1012 RECORD(DECL_BLOCK);
1013 RECORD(DECL_CONTEXT_LEXICAL);
1014 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001015 RECORD(DECL_NAMESPACE);
1016 RECORD(DECL_NAMESPACE_ALIAS);
1017 RECORD(DECL_USING);
1018 RECORD(DECL_USING_SHADOW);
1019 RECORD(DECL_USING_DIRECTIVE);
1020 RECORD(DECL_UNRESOLVED_USING_VALUE);
1021 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1022 RECORD(DECL_LINKAGE_SPEC);
1023 RECORD(DECL_CXX_RECORD);
1024 RECORD(DECL_CXX_METHOD);
1025 RECORD(DECL_CXX_CONSTRUCTOR);
1026 RECORD(DECL_CXX_DESTRUCTOR);
1027 RECORD(DECL_CXX_CONVERSION);
1028 RECORD(DECL_ACCESS_SPEC);
1029 RECORD(DECL_FRIEND);
1030 RECORD(DECL_FRIEND_TEMPLATE);
1031 RECORD(DECL_CLASS_TEMPLATE);
1032 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1033 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001034 RECORD(DECL_VAR_TEMPLATE);
1035 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1036 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001037 RECORD(DECL_FUNCTION_TEMPLATE);
1038 RECORD(DECL_TEMPLATE_TYPE_PARM);
1039 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1040 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1041 RECORD(DECL_STATIC_ASSERT);
1042 RECORD(DECL_CXX_BASE_SPECIFIERS);
1043 RECORD(DECL_INDIRECTFIELD);
1044 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1045
Douglas Gregor03412ba2011-06-03 02:27:19 +00001046 // Statements and Exprs can occur in the Decls and Types block.
1047 AddStmtsExprs(Stream, Record);
1048
Douglas Gregor92a96f52011-02-08 21:58:10 +00001049 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001050 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001051 RECORD(PPD_MACRO_DEFINITION);
1052 RECORD(PPD_INCLUSION_DIRECTIVE);
1053
Chris Lattner28fa4e62009-04-26 22:26:21 +00001054#undef RECORD
1055#undef BLOCK
1056 Stream.ExitBlock();
1057}
1058
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001059/// \brief Adjusts the given filename to only write out the portion of the
1060/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001061///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001062/// \param Filename the file name to adjust.
1063///
1064/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
1065/// the returned filename will be adjusted by this system root.
1066///
1067/// \returns either the original filename (if it needs no adjustment) or the
1068/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001069static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +00001070adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001071 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001072
Douglas Gregorc567ba22011-07-22 16:35:34 +00001073 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001074 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001075
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001076 // Verify that the filename and the system root have the same prefix.
1077 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001078 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001079 if (Filename[Pos] != isysroot[Pos])
1080 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001081
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001082 // We hit the end of the filename before we hit the end of the system root.
1083 if (!Filename[Pos])
1084 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001085
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001086 // If the file name has a '/' at the current position, skip over the '/'.
1087 // We distinguish sysroot-based includes from absolute includes by the
1088 // absence of '/' at the beginning of sysroot-based includes.
1089 if (Filename[Pos] == '/')
1090 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001091
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001092 return Filename + Pos;
1093}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001094
Douglas Gregor112b9072012-10-18 05:31:06 +00001095/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001096void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1097 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001098 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001099 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001100 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1101 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001102
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001103 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001104 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1105 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1106 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1107 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1108 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1109 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1110 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1111 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1112 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1113 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1114 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001115 Record.push_back(VERSION_MAJOR);
1116 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001117 Record.push_back(CLANG_VERSION_MAJOR);
1118 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001119 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001120 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001121 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1122 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001123
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001124 // Module name
1125 if (WritingModule) {
1126 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1127 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1129 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1130 RecordData Record;
1131 Record.push_back(MODULE_NAME);
1132 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1133 }
1134
1135 // Module map file
1136 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001137 Record.clear();
1138 auto addModMap = [&](const FileEntry *F) {
1139 SmallString<128> ModuleMap(F->getName());
1140 llvm::sys::fs::make_absolute(ModuleMap);
1141 AddString(ModuleMap.str(), Record);
1142 };
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001143
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001144 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1145
1146 // Primary module map file.
1147 addModMap(Map.getModuleMapFileForUniquing(WritingModule));
1148
1149 // Additional module map files.
1150 if (auto *AdditionalModMaps = Map.getAdditionalModuleMapFiles(WritingModule)) {
1151 Record.push_back(AdditionalModMaps->size());
1152 for (const FileEntry *F : *AdditionalModMaps)
1153 addModMap(F);
1154 } else {
1155 Record.push_back(0);
1156 }
1157
1158 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001159 }
1160
Douglas Gregor112b9072012-10-18 05:31:06 +00001161 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001162 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001163 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001164 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001165
1166 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1167 M != MEnd; ++M) {
1168 // Skip modules that weren't directly imported.
1169 if (!(*M)->isDirectlyImported())
1170 continue;
1171
1172 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001173 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001174 Record.push_back((*M)->File->getSize());
1175 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001176 const std::string &FileName = (*M)->FileName;
1177 Record.push_back(FileName.size());
1178 Record.append(FileName.begin(), FileName.end());
1179 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001180 Stream.EmitRecord(IMPORTS, Record);
1181 }
Mike Stump11289f42009-09-09 15:08:12 +00001182
Douglas Gregor112b9072012-10-18 05:31:06 +00001183 // Language options.
1184 Record.clear();
1185 const LangOptions &LangOpts = Context.getLangOpts();
1186#define LANGOPT(Name, Bits, Default, Description) \
1187 Record.push_back(LangOpts.Name);
1188#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1189 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1190#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001191#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1192#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001193
1194 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1195 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1196
1197 Record.push_back(LangOpts.CurrentModule.size());
1198 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001199
1200 // Comment options.
1201 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1202 for (CommentOptions::BlockCommandNamesTy::const_iterator
1203 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1204 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1205 I != IEnd; ++I) {
1206 AddString(*I, Record);
1207 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001208 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001209
Douglas Gregor112b9072012-10-18 05:31:06 +00001210 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1211
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001212 // Target options.
1213 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001214 const TargetInfo &Target = Context.getTargetInfo();
1215 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001216 AddString(TargetOpts.Triple, Record);
1217 AddString(TargetOpts.CPU, Record);
1218 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001219 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1220 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1221 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1222 }
1223 Record.push_back(TargetOpts.Features.size());
1224 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1225 AddString(TargetOpts.Features[I], Record);
1226 }
1227 Stream.EmitRecord(TARGET_OPTIONS, Record);
1228
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001229 // Diagnostic options.
1230 Record.clear();
1231 const DiagnosticOptions &DiagOpts
1232 = Context.getDiagnostics().getDiagnosticOptions();
1233#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1234#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1235 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1236#include "clang/Basic/DiagnosticOptions.def"
1237 Record.push_back(DiagOpts.Warnings.size());
1238 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1239 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001240 Record.push_back(DiagOpts.Remarks.size());
1241 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1242 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001243 // Note: we don't serialize the log or serialization file names, because they
1244 // are generally transient files and will almost always be overridden.
1245 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1246
Douglas Gregorc6317db2012-10-24 15:49:58 +00001247 // File system options.
1248 Record.clear();
1249 const FileSystemOptions &FSOpts
1250 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1251 AddString(FSOpts.WorkingDir, Record);
1252 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1253
Douglas Gregor2d302362012-10-24 16:50:34 +00001254 // Header search options.
1255 Record.clear();
1256 const HeaderSearchOptions &HSOpts
1257 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1258 AddString(HSOpts.Sysroot, Record);
1259
1260 // Include entries.
1261 Record.push_back(HSOpts.UserEntries.size());
1262 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1263 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1264 AddString(Entry.Path, Record);
1265 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001266 Record.push_back(Entry.IsFramework);
1267 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001268 }
1269
1270 // System header prefixes.
1271 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1272 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1273 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1274 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1275 }
1276
1277 AddString(HSOpts.ResourceDir, Record);
1278 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001279 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001280 Record.push_back(HSOpts.DisableModuleHash);
1281 Record.push_back(HSOpts.UseBuiltinIncludes);
1282 Record.push_back(HSOpts.UseStandardSystemIncludes);
1283 Record.push_back(HSOpts.UseStandardCXXIncludes);
1284 Record.push_back(HSOpts.UseLibcxx);
1285 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1286
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001287 // Preprocessor options.
1288 Record.clear();
1289 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1290
1291 // Macro definitions.
1292 Record.push_back(PPOpts.Macros.size());
1293 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1294 AddString(PPOpts.Macros[I].first, Record);
1295 Record.push_back(PPOpts.Macros[I].second);
1296 }
1297
1298 // Includes
1299 Record.push_back(PPOpts.Includes.size());
1300 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1301 AddString(PPOpts.Includes[I], Record);
1302
1303 // Macro includes
1304 Record.push_back(PPOpts.MacroIncludes.size());
1305 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1306 AddString(PPOpts.MacroIncludes[I], Record);
1307
Douglas Gregorb6368752012-10-24 23:41:50 +00001308 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001309 // Detailed record is important since it is used for the module cache hash.
1310 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001311 AddString(PPOpts.ImplicitPCHInclude, Record);
1312 AddString(PPOpts.ImplicitPTHInclude, Record);
1313 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1314 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1315
Douglas Gregora3b20262011-05-06 21:43:30 +00001316 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001317 SourceManager &SM = Context.getSourceManager();
1318 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1319 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001320 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1321 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001322 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1323 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1324
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001325 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001326
Michael J. Spencer740857f2010-12-21 16:45:57 +00001327 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001328
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001329 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001330 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001331 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001332 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001333 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001334 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001335 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001336 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001337
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001338 Record.clear();
1339 Record.push_back(SM.getMainFileID().getOpaqueValue());
1340 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1341
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001342 // Original PCH directory
1343 if (!OutputFile.empty() && OutputFile != "-") {
1344 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1345 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1347 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1348
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001349 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001350
1351 llvm::sys::fs::make_absolute(OutputPath);
1352 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1353
1354 RecordData Record;
1355 Record.push_back(ORIGINAL_PCH_DIR);
1356 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1357 }
1358
Douglas Gregor49491f72013-03-15 22:15:07 +00001359 WriteInputFiles(Context.SourceMgr,
1360 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001361 isysroot,
1362 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001363 Stream.ExitBlock();
1364}
1365
Douglas Gregor49491f72013-03-15 22:15:07 +00001366namespace {
1367 /// \brief An input file.
1368 struct InputFileEntry {
1369 const FileEntry *File;
1370 bool IsSystemFile;
1371 bool BufferOverridden;
1372 };
1373}
1374
1375void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1376 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001377 StringRef isysroot,
1378 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001379 using namespace llvm;
1380 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1381 RecordData Record;
1382
1383 // Create input-file abbreviation.
1384 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1385 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001386 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001387 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1388 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001389 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001390 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1391 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1392
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001393 // Get all ContentCache objects for files, sorted by whether the file is a
1394 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001395 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001396 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1397 // Get this source location entry.
1398 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001399 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001400
1401 // We only care about file entries that were not overridden.
1402 if (!SLoc->isFile())
1403 continue;
1404 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001405 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001406 continue;
1407
Douglas Gregor49491f72013-03-15 22:15:07 +00001408 InputFileEntry Entry;
1409 Entry.File = Cache->OrigEntry;
1410 Entry.IsSystemFile = Cache->IsSystemFile;
1411 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001412 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001413 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001414 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001415 SortedFiles.push_front(Entry);
1416 }
1417
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001418 unsigned UserFilesNum = 0;
1419 // Write out all of the input files.
1420 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001421 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001422 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001423 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001424
Douglas Gregor49491f72013-03-15 22:15:07 +00001425 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001426 if (InputFileID != 0)
1427 continue; // already recorded this file.
1428
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001429 // Record this entry's offset.
1430 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001431
1432 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001433
Douglas Gregor49491f72013-03-15 22:15:07 +00001434 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001435 ++UserFilesNum;
1436
Douglas Gregor72be3902012-10-19 00:38:02 +00001437 Record.clear();
1438 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001439 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001440
1441 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001442 Record.push_back(Entry.File->getSize());
1443 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001444
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001445 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001446 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001447
Douglas Gregor72be3902012-10-19 00:38:02 +00001448 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001449 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001450 SmallString<128> FilePath(Filename);
1451
1452 // Ask the file manager to fixup the relative path for us. This will
1453 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001454 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001455
1456 // FIXME: This call to make_absolute shouldn't be necessary, the
1457 // call to FixupRelativePath should always return an absolute path.
1458 llvm::sys::fs::make_absolute(FilePath);
1459 Filename = FilePath.c_str();
1460
1461 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1462
1463 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1464 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001465
Douglas Gregor112b9072012-10-18 05:31:06 +00001466 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001467
1468 // Create input file offsets abbreviation.
1469 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1470 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1471 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001472 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1473 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001474 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1475 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1476
1477 // Write input file offsets.
1478 Record.clear();
1479 Record.push_back(INPUT_FILE_OFFSETS);
1480 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001481 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001482 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001483}
1484
Douglas Gregora7f71a92009-04-10 03:52:48 +00001485//===----------------------------------------------------------------------===//
1486// Source Manager Serialization
1487//===----------------------------------------------------------------------===//
1488
1489/// \brief Create an abbreviation for the SLocEntry that refers to a
1490/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001491static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001492 using namespace llvm;
1493 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001494 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1497 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001499 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001504 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001505}
1506
1507/// \brief Create an abbreviation for the SLocEntry that refers to a
1508/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001509static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001510 using namespace llvm;
1511 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001512 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1515 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1516 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1517 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001518 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001519}
1520
1521/// \brief Create an abbreviation for the SLocEntry that refers to a
1522/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001523static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001524 using namespace llvm;
1525 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001526 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001528 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001529}
1530
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001531/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1532/// expansion.
1533static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001534 using namespace llvm;
1535 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001536 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001542 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001543}
1544
Douglas Gregor09b69892011-02-10 17:09:37 +00001545namespace {
1546 // Trait used for the on-disk hash table of header search information.
1547 class HeaderFileInfoTrait {
1548 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001549 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001550
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001551 // Keep track of the framework names we've used during serialization.
1552 SmallVector<char, 128> FrameworkStringData;
1553 llvm::StringMap<unsigned> FrameworkNameOffset;
1554
Douglas Gregor09b69892011-02-10 17:09:37 +00001555 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001556 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1557 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001558
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001559 struct key_type {
1560 const FileEntry *FE;
1561 const char *Filename;
1562 };
1563 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001564
1565 typedef HeaderFileInfo data_type;
1566 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001567 typedef unsigned hash_value_type;
1568 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001569
Justin Bogner25463f12014-04-18 20:27:24 +00001570 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001571 // The hash is based only on size/time of the file, so that the reader can
1572 // match even when symlinking or excess path elements ("foo/../", "../")
1573 // change the form of the name. However, complete path is still the key.
1574 return llvm::hash_combine(key.FE->getSize(),
1575 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001576 }
1577
1578 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001579 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001580 using namespace llvm::support;
1581 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001582 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001583 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001584 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001585 if (Data.isModuleHeader)
1586 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001587 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001588 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001589 }
1590
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001591 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001592 using namespace llvm::support;
1593 endian::Writer<little> LE(Out);
1594 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001595 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001596 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001597 KeyLen -= 8;
1598 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001599 }
1600
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001601 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001602 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001603 using namespace llvm::support;
1604 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001605 uint64_t Start = Out.tell(); (void)Start;
1606
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001607 unsigned char Flags = (Data.HeaderRole << 6)
1608 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001609 | (Data.isPragmaOnce << 4)
1610 | (Data.DirInfo << 2)
1611 | (Data.Resolved << 1)
1612 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001613 LE.write<uint8_t>(Flags);
1614 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001615
1616 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001617 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001618 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001619 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001620
1621 unsigned Offset = 0;
1622 if (!Data.Framework.empty()) {
1623 // If this header refers into a framework, save the framework name.
1624 llvm::StringMap<unsigned>::iterator Pos
1625 = FrameworkNameOffset.find(Data.Framework);
1626 if (Pos == FrameworkNameOffset.end()) {
1627 Offset = FrameworkStringData.size() + 1;
1628 FrameworkStringData.append(Data.Framework.begin(),
1629 Data.Framework.end());
1630 FrameworkStringData.push_back(0);
1631
1632 FrameworkNameOffset[Data.Framework] = Offset;
1633 } else
1634 Offset = Pos->second;
1635 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001636 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001637
1638 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001639 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001640 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001641 }
1642
Douglas Gregor09b69892011-02-10 17:09:37 +00001643 assert(Out.tell() - Start == DataLen && "Wrong data length");
1644 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001645
1646 const char *strings_begin() const { return FrameworkStringData.begin(); }
1647 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001648 };
1649} // end anonymous namespace
1650
1651/// \brief Write the header search block for the list of files that
1652///
1653/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001654void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001655 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001656 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1657
1658 if (FilesByUID.size() > HS.header_file_size())
1659 FilesByUID.resize(HS.header_file_size());
1660
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001661 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001662 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001663 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001664 unsigned NumHeaderSearchEntries = 0;
1665 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1666 const FileEntry *File = FilesByUID[UID];
1667 if (!File)
1668 continue;
1669
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001670 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1671 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001672 HeaderFileInfo HFI;
1673 if (!HS.tryGetFileInfo(File, HFI) ||
1674 (HFI.External && Chain) ||
1675 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001676 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001677
1678 // Turn the file name into an absolute path, if it isn't already.
1679 const char *Filename = File->getName();
1680 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1681
1682 // If we performed any translation on the file name at all, we need to
1683 // save this string, since the generator will refer to it later.
1684 if (Filename != File->getName()) {
1685 Filename = strdup(Filename);
1686 SavedStrings.push_back(Filename);
1687 }
1688
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001689 HeaderFileInfoTrait::key_type key = { File, Filename };
1690 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001691 ++NumHeaderSearchEntries;
1692 }
1693
1694 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001695 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001696 uint32_t BucketOffset;
1697 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001698 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001699 llvm::raw_svector_ostream Out(TableData);
1700 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001701 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001702 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1703 }
1704
1705 // Create a blob abbreviation
1706 using namespace llvm;
1707 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1708 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1710 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1713 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1714
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001715 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001716 RecordData Record;
1717 Record.push_back(HEADER_SEARCH_TABLE);
1718 Record.push_back(BucketOffset);
1719 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001720 Record.push_back(TableData.size());
1721 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001722 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1723
1724 // Free all of the strings we had to duplicate.
1725 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001726 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001727}
1728
Douglas Gregora7f71a92009-04-10 03:52:48 +00001729/// \brief Writes the block containing the serialized form of the
1730/// source manager.
1731///
1732/// TODO: We should probably use an on-disk hash table (stored in a
1733/// blob), indexed based on the file name, so that we only create
1734/// entries for files that we actually need. In the common case (no
1735/// errors), we probably won't have to create file entries for any of
1736/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001737void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001738 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001739 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001740 RecordData Record;
1741
Chris Lattner0910e3b2009-04-10 17:16:57 +00001742 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001743 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001744
1745 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001746 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1747 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1748 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001749 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001750
Douglas Gregor258ae542009-04-27 06:38:32 +00001751 // Write out the source location entry table. We skip the first
1752 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001753 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001754 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001755 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1756 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001757 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001758 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001759 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001760 FileID FID = FileID::get(I);
1761 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001762
Douglas Gregor258ae542009-04-27 06:38:32 +00001763 // Record the offset of this source-location entry.
1764 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1765
1766 // Figure out which record code to use.
1767 unsigned Code;
1768 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001769 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1770 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001771 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001772 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001773 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001774 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001775 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001776 Record.clear();
1777 Record.push_back(Code);
1778
Douglas Gregor925296b2011-07-19 16:10:42 +00001779 // Starting offset of this entry within this module, so skip the dummy.
1780 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001781 if (SLoc->isFile()) {
1782 const SrcMgr::FileInfo &File = SLoc->getFile();
1783 Record.push_back(File.getIncludeLoc().getRawEncoding());
1784 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1785 Record.push_back(File.hasLineDirectives());
1786
1787 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001788 if (Content->OrigEntry) {
1789 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001790 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001791
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001792 // The source location entry is a file. Emit input file ID.
1793 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1794 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001795
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001796 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001797
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001798 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001799 if (FDI != FileDeclIDs.end()) {
1800 Record.push_back(FDI->second->FirstDeclIndex);
1801 Record.push_back(FDI->second->DeclIDs.size());
1802 } else {
1803 Record.push_back(0);
1804 Record.push_back(0);
1805 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001806
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001807 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001808
1809 if (Content->BufferOverridden) {
1810 Record.clear();
1811 Record.push_back(SM_SLOC_BUFFER_BLOB);
1812 const llvm::MemoryBuffer *Buffer
1813 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1814 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1815 StringRef(Buffer->getBufferStart(),
1816 Buffer->getBufferSize() + 1));
1817 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001818 } else {
1819 // The source location entry is a buffer. The blob associated
1820 // with this entry contains the contents of the buffer.
1821
1822 // We add one to the size so that we capture the trailing NULL
1823 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1824 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001825 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001826 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001827 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001828 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001829 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001830 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001831 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001832 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001833 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001834 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001835
Douglas Gregor925296b2011-07-19 16:10:42 +00001836 if (strcmp(Name, "<built-in>") == 0) {
1837 PreloadSLocs.push_back(SLocEntryOffsets.size());
1838 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001839 }
1840 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001841 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001842 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001843 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1844 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001845 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1846 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001847
1848 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001849 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001850 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001851 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001852 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001853 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001854 }
1855 }
1856
Douglas Gregor8f45df52009-04-16 22:23:12 +00001857 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001858
1859 if (SLocEntryOffsets.empty())
1860 return;
1861
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001862 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001863 // table is used for lazily loading source-location information.
1864 using namespace llvm;
1865 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001866 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1870 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001871
Douglas Gregor258ae542009-04-27 06:38:32 +00001872 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001873 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001874 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001875 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001876 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001877
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001878 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001879 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001880 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001881
1882 // Write the line table. It depends on remapping working, so it must come
1883 // after the source location offsets.
1884 if (SourceMgr.hasLineTable()) {
1885 LineTableInfo &LineTable = SourceMgr.getLineTable();
1886
1887 Record.clear();
1888 // Emit the file names
1889 Record.push_back(LineTable.getNumFilenames());
1890 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1891 // Emit the file name
1892 const char *Filename = LineTable.getFilename(I);
1893 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1894 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1895 Record.push_back(FilenameLen);
1896 if (FilenameLen)
1897 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1898 }
1899
1900 // Emit the line entries
1901 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1902 L != LEnd; ++L) {
1903 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001904 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001905 continue;
1906
1907 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001908 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001909
1910 // Emit the line entries
1911 Record.push_back(L->second.size());
1912 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1913 LEEnd = L->second.end();
1914 LE != LEEnd; ++LE) {
1915 Record.push_back(LE->FileOffset);
1916 Record.push_back(LE->LineNo);
1917 Record.push_back(LE->FilenameID);
1918 Record.push_back((unsigned)LE->FileKind);
1919 Record.push_back(LE->IncludeOffset);
1920 }
1921 }
1922 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1923 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001924}
1925
Douglas Gregorc5046832009-04-27 18:38:38 +00001926//===----------------------------------------------------------------------===//
1927// Preprocessor Serialization
1928//===----------------------------------------------------------------------===//
1929
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001930namespace {
1931class ASTMacroTableTrait {
1932public:
1933 typedef IdentID key_type;
1934 typedef key_type key_type_ref;
1935
1936 struct Data {
1937 uint32_t MacroDirectivesOffset;
1938 };
1939
1940 typedef Data data_type;
1941 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001942 typedef unsigned hash_value_type;
1943 typedef unsigned offset_type;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001944
Justin Bogner25463f12014-04-18 20:27:24 +00001945 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001946 return llvm::hash_value(IdID);
1947 }
1948
1949 std::pair<unsigned,unsigned>
1950 static EmitKeyDataLength(raw_ostream& Out,
1951 key_type_ref Key, data_type_ref Data) {
1952 unsigned KeyLen = 4; // IdentID.
1953 unsigned DataLen = 4; // MacroDirectivesOffset.
1954 return std::make_pair(KeyLen, DataLen);
1955 }
1956
1957 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001958 using namespace llvm::support;
1959 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001960 }
1961
1962 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1963 unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001964 using namespace llvm::support;
1965 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001966 }
1967};
1968} // end anonymous namespace
1969
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001970static int compareMacroDirectives(
1971 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1972 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1973 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001974}
1975
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001976static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1977 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001978 if (MacroInfo *MI = MD->getMacroInfo())
1979 if (MI->isBuiltinMacro())
1980 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001981
1982 if (IsModule) {
Richard Smithe657bbd2014-07-18 22:13:40 +00001983 // Re-export any imported directives.
Richard Smithdaa69e02014-07-25 04:40:03 +00001984 if (MD->isImported())
1985 return false;
Richard Smithe657bbd2014-07-18 22:13:40 +00001986
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001987 SourceLocation Loc = MD->getLocation();
1988 if (Loc.isInvalid())
1989 return true;
1990 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1991 return true;
1992 }
1993
1994 return false;
1995}
1996
Chris Lattnereeffaef2009-04-10 17:15:23 +00001997/// \brief Writes the block containing the serialized form of the
1998/// preprocessor.
1999///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002000void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002001 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2002 if (PPRec)
2003 WritePreprocessorDetail(*PPRec);
2004
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002005 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002006
Chris Lattner0af3ba12009-04-13 01:29:17 +00002007 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2008 if (PP.getCounterValue() != 0) {
2009 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002010 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002011 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002012 }
2013
2014 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002015 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002016
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002017 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002018 // FIXME: use diagnostics subsystem for localization etc.
2019 if (PP.SawDateOrTime())
2020 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00002021
Douglas Gregor796d76a2010-10-20 22:00:55 +00002022
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002023 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002024 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002025
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002026 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002027 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002028 MacroDirectives;
2029 for (Preprocessor::macro_iterator
2030 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2031 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002032 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002033 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002034 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002035
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002036 // Sort the set of macro definitions that need to be serialized by the
2037 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002038 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2039 &compareMacroDirectives);
2040
Justin Bognerbb094f02014-04-18 19:57:06 +00002041 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002042
2043 // Emit the macro directives as a list and associate the offset with the
2044 // identifier they belong to.
2045 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2046 const IdentifierInfo *Name = MacroDirectives[I].first;
2047 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2048 MacroDirective *MD = MacroDirectives[I].second;
2049
2050 // If the macro or identifier need no updates, don't write the macro history
2051 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002052 // FIXME: Chain the macro history instead of re-writing it.
2053 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002054 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2055 continue;
2056
2057 // Emit the macro directives in reverse source order.
2058 for (; MD; MD = MD->getPrevious()) {
2059 if (shouldIgnoreMacro(MD, IsModule, PP))
2060 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002061
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002062 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002063 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002064 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002065 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2066 Record.push_back(InfoID);
Richard Smithdaa69e02014-07-25 04:40:03 +00002067 Record.push_back(DefMD->getOwningModuleID());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002068 Record.push_back(DefMD->isAmbiguous());
Richard Smithdaa69e02014-07-25 04:40:03 +00002069 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2070 Record.push_back(UndefMD->getOwningModuleID());
2071 } else {
2072 auto *VisMD = cast<VisibilityMacroDirective>(MD);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002073 Record.push_back(VisMD->isPublic());
2074 }
Richard Smithdaa69e02014-07-25 04:40:03 +00002075
2076 if (MD->isImported()) {
2077 auto Overrides = MD->getOverriddenModules();
2078 Record.push_back(Overrides.size());
2079 for (auto Override : Overrides)
2080 Record.push_back(Override);
2081 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002082 }
2083 if (Record.empty())
2084 continue;
2085
2086 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2087 Record.clear();
2088
2089 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2090
2091 IdentID NameID = getIdentifierRef(Name);
2092 ASTMacroTableTrait::Data data;
2093 data.MacroDirectivesOffset = MacroDirectiveOffset;
2094 Generator.insert(NameID, data);
2095 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002096
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002097 /// \brief Offsets of each of the macros into the bitstream, indexed by
2098 /// the local macro ID
2099 ///
2100 /// For each identifier that is associated with a macro, this map
2101 /// provides the offset into the bitstream where that macro is
2102 /// defined.
2103 std::vector<uint32_t> MacroOffsets;
2104
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002105 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2106 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2107 MacroInfo *MI = MacroInfosToEmit[I].MI;
2108 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002109
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002110 if (ID < FirstMacroID) {
2111 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2112 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002113 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002114
2115 // Record the local offset of this macro.
2116 unsigned Index = ID - FirstMacroID;
2117 if (Index == MacroOffsets.size())
2118 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2119 else {
2120 if (Index > MacroOffsets.size())
2121 MacroOffsets.resize(Index + 1);
2122
2123 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2124 }
2125
2126 AddIdentifierRef(Name, Record);
2127 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2128 AddSourceLocation(MI->getDefinitionLoc(), Record);
2129 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2130 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002131 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002132 unsigned Code;
2133 if (MI->isObjectLike()) {
2134 Code = PP_MACRO_OBJECT_LIKE;
2135 } else {
2136 Code = PP_MACRO_FUNCTION_LIKE;
2137
2138 Record.push_back(MI->isC99Varargs());
2139 Record.push_back(MI->isGNUVarargs());
2140 Record.push_back(MI->hasCommaPasting());
2141 Record.push_back(MI->getNumArgs());
2142 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2143 I != E; ++I)
2144 AddIdentifierRef(*I, Record);
2145 }
2146
2147 // If we have a detailed preprocessing record, record the macro definition
2148 // ID that corresponds to this macro.
2149 if (PPRec)
2150 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2151
2152 Stream.EmitRecord(Code, Record);
2153 Record.clear();
2154
2155 // Emit the tokens array.
2156 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2157 // Note that we know that the preprocessor does not have any annotation
2158 // tokens in it because they are created by the parser, and thus can't
2159 // be in a macro definition.
2160 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002161 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002162 Stream.EmitRecord(PP_TOKEN, Record);
2163 Record.clear();
2164 }
2165 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002166 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002167
Douglas Gregor92a96f52011-02-08 21:58:10 +00002168 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002169
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002170 // Create the on-disk hash table in a buffer.
2171 SmallString<4096> MacroTable;
2172 uint32_t BucketOffset;
2173 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002174 using namespace llvm::support;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002175 llvm::raw_svector_ostream Out(MacroTable);
2176 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002177 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002178 BucketOffset = Generator.Emit(Out);
2179 }
2180
2181 // Write the macro table
2182 using namespace llvm;
2183 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2184 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2187 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2188
2189 Record.push_back(MACRO_TABLE);
2190 Record.push_back(BucketOffset);
2191 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2192 Record.clear();
2193
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002194 // Write the offsets table for macro IDs.
2195 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002196 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002197 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2201
2202 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2203 Record.clear();
2204 Record.push_back(MACRO_OFFSET);
2205 Record.push_back(MacroOffsets.size());
2206 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2207 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2208 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002209}
2210
2211void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002212 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002213 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002214
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002215 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002216
Douglas Gregor92a96f52011-02-08 21:58:10 +00002217 // Enter the preprocessor block.
2218 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002219
Douglas Gregoraae92242010-03-19 21:51:54 +00002220 // If the preprocessor has a preprocessing record, emit it.
2221 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002222 using namespace llvm;
2223
2224 // Set up the abbreviation for
2225 unsigned InclusionAbbrev = 0;
2226 {
2227 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2228 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2234 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2235 }
2236
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002237 unsigned FirstPreprocessorEntityID
2238 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2239 + NUM_PREDEF_PP_ENTITY_IDS;
2240 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002241 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002242 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2243 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002244 E != EEnd;
2245 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002246 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002247
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002248 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2249 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002250
Douglas Gregor92a96f52011-02-08 21:58:10 +00002251 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002252 // Record this macro definition's ID.
2253 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002254
Douglas Gregor92a96f52011-02-08 21:58:10 +00002255 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002256 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2257 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002258 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002259
Chandler Carrutha88a22182011-07-14 08:20:46 +00002260 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002261 Record.push_back(ME->isBuiltinMacro());
2262 if (ME->isBuiltinMacro())
2263 AddIdentifierRef(ME->getName(), Record);
2264 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002265 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002266 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002267 continue;
2268 }
2269
2270 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2271 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002272 Record.push_back(ID->getFileName().size());
2273 Record.push_back(ID->wasInQuotes());
2274 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002275 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002276 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002277 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002278 // Check that the FileEntry is not null because it was not resolved and
2279 // we create a PCH even with compiler errors.
2280 if (ID->getFile())
2281 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002282 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2283 continue;
2284 }
2285
2286 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2287 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002288 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002289
Douglas Gregoraae92242010-03-19 21:51:54 +00002290 // Write the offsets table for the preprocessing record.
2291 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002292 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2293
Douglas Gregoraae92242010-03-19 21:51:54 +00002294 // Write the offsets table for identifier IDs.
2295 using namespace llvm;
2296 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002297 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002300 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002301
Douglas Gregoraae92242010-03-19 21:51:54 +00002302 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002303 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002304 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002305 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2306 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002307 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002308}
2309
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002310unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2311 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2312 if (Known != SubmoduleIDs.end())
2313 return Known->second;
2314
2315 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2316}
2317
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002318unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2319 if (!Mod)
2320 return 0;
2321
2322 llvm::DenseMap<Module *, unsigned>::const_iterator
2323 Known = SubmoduleIDs.find(Mod);
2324 if (Known != SubmoduleIDs.end())
2325 return Known->second;
2326
2327 return 0;
2328}
2329
Douglas Gregor253eefe2011-12-01 00:59:36 +00002330/// \brief Compute the number of modules within the given tree (including the
2331/// given module).
2332static unsigned getNumberOfModules(Module *Mod) {
2333 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002334 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2335 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002336 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002337 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002338
2339 return ChildModules + 1;
2340}
2341
Douglas Gregorde3ef502011-11-30 23:21:26 +00002342void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002343 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002344 // FIXME: This feels like it belongs somewhere else, but there are no
2345 // other consumers of this information.
2346 SourceManager &SrcMgr = PP->getSourceManager();
2347 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002348 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002349 if (Module *ImportedFrom
2350 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2351 SrcMgr))) {
2352 ImportedFrom->Imports.push_back(I->getImportedModule());
2353 }
2354 }
2355
Douglas Gregor69021972011-11-30 17:33:56 +00002356 // Enter the submodule description block.
Richard Smith01b2cb42014-07-26 06:37:51 +00002357 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/4);
Douglas Gregor69021972011-11-30 17:33:56 +00002358
2359 // Write the abbreviations needed for the submodules block.
2360 using namespace llvm;
2361 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2362 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2366 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002367 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2374 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2375
2376 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002377 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2379 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2380
2381 Abbrev = new BitCodeAbbrev();
2382 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2384 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002385
2386 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002387 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2389 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2390
2391 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002392 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2394 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2395
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002396 Abbrev = new BitCodeAbbrev();
2397 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002400 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2401
Douglas Gregor59527662012-10-15 06:28:11 +00002402 Abbrev = new BitCodeAbbrev();
2403 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2405 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2406
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002407 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002408 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2410 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2411
2412 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002413 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2416 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2417
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002418 Abbrev = new BitCodeAbbrev();
2419 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2421 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2422
Douglas Gregorfb912652013-03-20 21:10:35 +00002423 Abbrev = new BitCodeAbbrev();
2424 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2427 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2428
Douglas Gregor253eefe2011-12-01 00:59:36 +00002429 // Write the submodule metadata block.
2430 RecordData Record;
2431 Record.push_back(getNumberOfModules(WritingModule));
2432 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2433 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2434
Douglas Gregor69021972011-11-30 17:33:56 +00002435 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002436 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002437 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002438 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002439 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002440 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002441 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002442
2443 // Emit the definition of the block.
2444 Record.clear();
2445 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002446 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002447 if (Mod->Parent) {
2448 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2449 Record.push_back(SubmoduleIDs[Mod->Parent]);
2450 } else {
2451 Record.push_back(0);
2452 }
2453 Record.push_back(Mod->IsFramework);
2454 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002455 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002456 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002457 Record.push_back(Mod->InferSubmodules);
2458 Record.push_back(Mod->InferExplicitSubmodules);
2459 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002460 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002461 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2462
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002463 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002464 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002465 Record.clear();
2466 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002467 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002468 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002469 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002470 }
2471
Douglas Gregor69021972011-11-30 17:33:56 +00002472 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002473 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002474 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002475 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002476 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002477 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002478 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2479 Record.clear();
2480 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2481 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2482 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002483 }
2484
2485 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002486 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002487 Record.clear();
2488 Record.push_back(SUBMODULE_HEADER);
2489 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002490 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002491 }
Douglas Gregor59527662012-10-15 06:28:11 +00002492 // Emit the excluded headers.
2493 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2494 Record.clear();
2495 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2496 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2497 Mod->ExcludedHeaders[I]->getName());
2498 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002499 // Emit the private headers.
2500 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2501 Record.clear();
2502 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2503 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2504 Mod->PrivateHeaders[I]->getName());
2505 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002506 ArrayRef<const FileEntry *>
2507 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2508 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002509 Record.clear();
2510 Record.push_back(SUBMODULE_TOPHEADER);
2511 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002512 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002513 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002514
2515 // Emit the imports.
2516 if (!Mod->Imports.empty()) {
2517 Record.clear();
2518 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002519 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002520 assert(ImportedID && "Unknown submodule!");
2521 Record.push_back(ImportedID);
2522 }
2523 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2524 }
2525
Douglas Gregor24bb9232011-12-02 18:58:38 +00002526 // Emit the exports.
2527 if (!Mod->Exports.empty()) {
2528 Record.clear();
2529 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002530 if (Module *Exported = Mod->Exports[I].getPointer()) {
2531 unsigned ExportedID = SubmoduleIDs[Exported];
2532 assert(ExportedID > 0 && "Unknown submodule ID?");
2533 Record.push_back(ExportedID);
2534 } else {
2535 Record.push_back(0);
2536 }
2537
Douglas Gregor24bb9232011-12-02 18:58:38 +00002538 Record.push_back(Mod->Exports[I].getInt());
2539 }
2540 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2541 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002542
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002543 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2544 // Might be unnecessary as use declarations are only used to build the
2545 // module itself.
2546
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002547 // Emit the link libraries.
2548 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2549 Record.clear();
2550 Record.push_back(SUBMODULE_LINK_LIBRARY);
2551 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2552 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2553 Mod->LinkLibraries[I].Library);
2554 }
2555
Douglas Gregorfb912652013-03-20 21:10:35 +00002556 // Emit the conflicts.
2557 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2558 Record.clear();
2559 Record.push_back(SUBMODULE_CONFLICT);
2560 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2561 assert(OtherID && "Unknown submodule!");
2562 Record.push_back(OtherID);
2563 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2564 Mod->Conflicts[I].Message);
2565 }
2566
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002567 // Emit the configuration macros.
2568 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2569 Record.clear();
2570 Record.push_back(SUBMODULE_CONFIG_MACRO);
2571 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2572 Mod->ConfigMacros[I]);
2573 }
2574
Douglas Gregor69021972011-11-30 17:33:56 +00002575 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002576 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2577 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002578 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002579 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002580 }
2581
2582 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002583
2584 assert((NextSubmoduleID - FirstSubmoduleID
2585 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002586}
2587
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002588serialization::SubmoduleID
2589ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002590 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002591 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002592
2593 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002594 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002595 Module *OwningMod
2596 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002597 if (!OwningMod)
2598 return 0;
2599
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002600 // Check whether this submodule is part of our own module.
2601 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002602 return 0;
2603
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002604 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002605}
2606
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002607void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2608 bool isModule) {
2609 // Make sure set diagnostic pragmas don't affect the translation unit that
2610 // imports the module.
2611 // FIXME: Make diagnostic pragma sections work properly with modules.
2612 if (isModule)
2613 return;
2614
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002615 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2616 DiagStateIDMap;
2617 unsigned CurrID = 0;
2618 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002619 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002620 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002621 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2622 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002623 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002624 if (point.Loc.isInvalid())
2625 continue;
2626
2627 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002628 unsigned &DiagStateID = DiagStateIDMap[point.State];
2629 Record.push_back(DiagStateID);
2630
2631 if (DiagStateID == 0) {
2632 DiagStateID = ++CurrID;
2633 for (DiagnosticsEngine::DiagState::const_iterator
2634 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2635 if (I->second.isPragma()) {
2636 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002637 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002638 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002639 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002640 Record.push_back(-1); // mark the end of the diag/map pairs for this
2641 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002642 }
2643 }
2644
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002645 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002646 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002647}
2648
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002649void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2650 if (CXXBaseSpecifiersOffsets.empty())
2651 return;
2652
2653 RecordData Record;
2654
2655 // Create a blob abbreviation for the C++ base specifiers offsets.
2656 using namespace llvm;
2657
2658 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2659 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2660 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2661 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2662 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2663
Douglas Gregorc27b2872011-08-04 00:01:48 +00002664 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002665 Record.clear();
2666 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2667 Record.push_back(CXXBaseSpecifiersOffsets.size());
2668 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002669 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002670}
2671
Douglas Gregorc5046832009-04-27 18:38:38 +00002672//===----------------------------------------------------------------------===//
2673// Type Serialization
2674//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002675
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002676/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002677void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002678 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002679 if (Idx.getIndex() == 0) // we haven't seen this type before.
2680 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002681
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002682 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002683
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002684 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002685 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002686 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002687 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002688 else if (TypeOffsets.size() < Index) {
2689 TypeOffsets.resize(Index + 1);
2690 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002691 }
2692
2693 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002694
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002695 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002696 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002697 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002698
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002699 if (T.hasLocalNonFastQualifiers()) {
2700 Qualifiers Qs = T.getLocalQualifiers();
2701 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002702 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002703 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002704 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002705 } else {
2706 switch (T->getTypeClass()) {
2707 // For all of the concrete, non-dependent types, call the
2708 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002709#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002710 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002711#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002712#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002713 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002714 }
2715
2716 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002717 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002718
2719 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002720 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002721}
2722
Douglas Gregorc5046832009-04-27 18:38:38 +00002723//===----------------------------------------------------------------------===//
2724// Declaration Serialization
2725//===----------------------------------------------------------------------===//
2726
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727/// \brief Write the block containing all of the declaration IDs
2728/// lexically declared within the given DeclContext.
2729///
2730/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2731/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002732uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002733 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002734 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002735 return 0;
2736
Douglas Gregor8f45df52009-04-16 22:23:12 +00002737 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002738 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002739 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002740 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002741 for (const auto *D : DC->decls())
2742 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002743
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002744 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002745 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002746 return Offset;
2747}
2748
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002749void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002750 using namespace llvm;
2751 RecordData Record;
2752
2753 // Write the type offsets array
2754 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002755 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2759 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2760 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002761 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002762 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002763 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002764 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002765
2766 // Write the declaration offsets array
2767 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002768 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2772 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2773 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002774 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002775 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002776 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002777 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002778}
2779
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002780void ASTWriter::WriteFileDeclIDsMap() {
2781 using namespace llvm;
2782 RecordData Record;
2783
2784 // Join the vectors of DeclIDs from all files.
2785 SmallVector<DeclID, 256> FileSortedIDs;
2786 for (FileDeclIDsTy::iterator
2787 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2788 DeclIDInFileInfo &Info = *FI->second;
2789 Info.FirstDeclIndex = FileSortedIDs.size();
2790 for (LocDeclIDsTy::iterator
2791 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2792 FileSortedIDs.push_back(DI->second);
2793 }
2794
2795 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2796 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2799 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2800 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002801 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002802 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2803}
2804
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002805void ASTWriter::WriteComments() {
2806 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002807 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002808 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002809 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2810 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002811 I != E; ++I) {
2812 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002813 AddSourceRange((*I)->getSourceRange(), Record);
2814 Record.push_back((*I)->getKind());
2815 Record.push_back((*I)->isTrailingComment());
2816 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002817 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2818 }
2819 Stream.ExitBlock();
2820}
2821
Douglas Gregorc5046832009-04-27 18:38:38 +00002822//===----------------------------------------------------------------------===//
2823// Global Method Pool and Selector Serialization
2824//===----------------------------------------------------------------------===//
2825
Douglas Gregore84a9da2009-04-20 20:36:09 +00002826namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002827// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002828class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002829 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002830
2831public:
2832 typedef Selector key_type;
2833 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002834
Sebastian Redl834bb972010-08-04 17:20:04 +00002835 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002836 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002837 ObjCMethodList Instance, Factory;
2838 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002839 typedef const data_type& data_type_ref;
2840
Justin Bogner25463f12014-04-18 20:27:24 +00002841 typedef unsigned hash_value_type;
2842 typedef unsigned offset_type;
2843
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002844 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002845
Justin Bogner25463f12014-04-18 20:27:24 +00002846 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002847 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002848 }
Mike Stump11289f42009-09-09 15:08:12 +00002849
2850 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002851 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002852 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002853 using namespace llvm::support;
2854 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002855 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002856 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002857 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2858 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002859 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002860 if (Method->Method)
2861 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002862 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002863 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002864 if (Method->Method)
2865 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002866 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002867 return std::make_pair(KeyLen, DataLen);
2868 }
Mike Stump11289f42009-09-09 15:08:12 +00002869
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002870 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002871 using namespace llvm::support;
2872 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002873 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002874 assert((Start >> 32) == 0 && "Selector key offset too large");
2875 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002876 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002877 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002878 if (N == 0)
2879 N = 1;
2880 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002881 LE.write<uint32_t>(
2882 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002883 }
Mike Stump11289f42009-09-09 15:08:12 +00002884
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002885 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002886 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002887 using namespace llvm::support;
2888 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002889 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002890 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002891 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002892 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002893 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002894 if (Method->Method)
2895 ++NumInstanceMethods;
2896
2897 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002898 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002899 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002900 if (Method->Method)
2901 ++NumFactoryMethods;
2902
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002903 unsigned InstanceBits = Methods.Instance.getBits();
2904 assert(InstanceBits < 4);
2905 unsigned NumInstanceMethodsAndBits =
2906 (NumInstanceMethods << 2) | InstanceBits;
2907 unsigned FactoryBits = Methods.Factory.getBits();
2908 assert(FactoryBits < 4);
2909 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Justin Bognere1c147c2014-03-28 22:03:19 +00002910 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2911 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002912 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002913 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002914 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002915 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002916 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002917 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002918 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002919 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002920
2921 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002922 }
2923};
2924} // end anonymous namespace
2925
Sebastian Redla19a67f2010-08-03 21:58:15 +00002926/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002927///
2928/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002929/// in an on-disk hash table indexed by the selector. The hash table also
2930/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002931void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002932 using namespace llvm;
2933
Sebastian Redla19a67f2010-08-03 21:58:15 +00002934 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002935 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002936 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002937 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002938 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002939 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002940 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002941 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002942
Sebastian Redla19a67f2010-08-03 21:58:15 +00002943 // Create the on-disk hash table representation. We walk through every
2944 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002945 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002946 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002947 I = SelectorIDs.begin(), E = SelectorIDs.end();
2948 I != E; ++I) {
2949 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002950 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002951 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002952 I->second,
2953 ObjCMethodList(),
2954 ObjCMethodList()
2955 };
2956 if (F != SemaRef.MethodPool.end()) {
2957 Data.Instance = F->second.first;
2958 Data.Factory = F->second.second;
2959 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002960 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002961 // changed.
2962 if (Chain && I->second < FirstSelectorID) {
2963 // Selector already exists. Did it change?
2964 bool changed = false;
2965 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002966 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002967 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002968 changed = true;
2969 }
2970 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002971 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002972 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002973 changed = true;
2974 }
2975 if (!changed)
2976 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002977 } else if (Data.Instance.Method || Data.Factory.Method) {
2978 // A new method pool entry.
2979 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002980 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002981 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002982 }
2983
Douglas Gregorc78d3462009-04-24 21:10:55 +00002984 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002985 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002986 uint32_t BucketOffset;
2987 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002988 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002989 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002990 llvm::raw_svector_ostream Out(MethodPool);
2991 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002992 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002993 BucketOffset = Generator.Emit(Out, Trait);
2994 }
2995
2996 // Create a blob abbreviation
2997 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002998 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3002 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3003
Douglas Gregor95c13f52009-04-25 17:48:32 +00003004 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003005 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003006 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003007 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003008 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003009 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00003010
3011 // Create a blob abbreviation for the selector table offsets.
3012 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003013 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3017 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3018
3019 // Write the selector offsets table.
3020 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003021 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003022 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003023 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003024 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003025 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003026 }
3027}
3028
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003029/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003030void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003031 using namespace llvm;
3032 if (SemaRef.ReferencedSelectors.empty())
3033 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003034
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003035 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003036
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003037 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003038 // very tricky to fix, and given that @selector shouldn't really appear in
3039 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003040 for (DenseMap<Selector, SourceLocation>::iterator S =
3041 SemaRef.ReferencedSelectors.begin(),
3042 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3043 Selector Sel = (*S).first;
3044 SourceLocation Loc = (*S).second;
3045 AddSelectorRef(Sel, Record);
3046 AddSourceLocation(Loc, Record);
3047 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003048 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003049}
3050
Douglas Gregorc5046832009-04-27 18:38:38 +00003051//===----------------------------------------------------------------------===//
3052// Identifier Table Serialization
3053//===----------------------------------------------------------------------===//
3054
Douglas Gregorc78d3462009-04-24 21:10:55 +00003055namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003056class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003057 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00003058 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003059 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003060 bool IsModule;
3061
Douglas Gregor1d583f22009-04-28 21:18:29 +00003062 /// \brief Determines whether this is an "interesting" identifier
3063 /// that needs a full IdentifierInfo structure written into the hash
3064 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003065 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003066 if (II->isPoisoned() ||
3067 II->isExtensionToken() ||
3068 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003069 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003070 II->getFETokenInfo<void>())
3071 return true;
3072
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003073 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00003074 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003075
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003076 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003077 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003078 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003079
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003080 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3081 if (!IsModule)
3082 return !shouldIgnoreMacro(Macro, IsModule, PP);
Richard Smithdaa69e02014-07-25 04:40:03 +00003083
3084 MacroState State;
3085 if (getFirstPublicSubmoduleMacro(Macro, State))
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003086 return true;
3087 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003088
3089 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003090 }
3091
Richard Smithdaa69e02014-07-25 04:40:03 +00003092 enum class SubmoduleMacroState {
3093 /// We've seen nothing about this macro.
3094 None,
3095 /// We've seen a public visibility directive.
3096 Public,
3097 /// We've either exported a macro for this module or found that the
3098 /// module's definition of this macro is private.
3099 Done
3100 };
3101 typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
Richard Smith49f906a2014-03-01 00:08:04 +00003102
3103 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003104 getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3105 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3106 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003107 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003108 }
3109
Richard Smith49f906a2014-03-01 00:08:04 +00003110 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003111 getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
Richard Smith49f906a2014-03-01 00:08:04 +00003112 if (MacroDirective *NextMD =
Richard Smithdaa69e02014-07-25 04:40:03 +00003113 getPublicSubmoduleMacro(MD->getPrevious(), State))
3114 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003115 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003116 }
3117
Richard Smithdaa69e02014-07-25 04:40:03 +00003118 /// \brief Traverses the macro directives history and returns the next
3119 /// public macro definition or undefinition that has not been found so far.
3120 ///
Richard Smith49f906a2014-03-01 00:08:04 +00003121 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003122 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00003123 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
Richard Smithdaa69e02014-07-25 04:40:03 +00003124 MacroState &State) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003125 if (!MD)
Craig Toppera13603a2014-05-22 05:54:18 +00003126 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003127
Richard Smith49f906a2014-03-01 00:08:04 +00003128 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003129 for (; MD; MD = MD->getPrevious()) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003130 // Once we hit an ignored macro, we're done: the rest of the chain
3131 // will all be ignored macros.
3132 if (shouldIgnoreMacro(MD, IsModule, PP))
3133 break;
Richard Smith57721ac2014-07-21 04:10:40 +00003134
Richard Smithdaa69e02014-07-25 04:40:03 +00003135 // If this macro was imported, re-export it.
3136 if (MD->isImported())
3137 return MD;
Richard Smith57721ac2014-07-21 04:10:40 +00003138
Richard Smithdaa69e02014-07-25 04:40:03 +00003139 SubmoduleID ModID = getSubmoduleID(MD);
3140 auto &S = State[ModID];
3141 assert(ModID && "found macro in no submodule");
Richard Smith49f906a2014-03-01 00:08:04 +00003142
Richard Smithdaa69e02014-07-25 04:40:03 +00003143 if (S == SubmoduleMacroState::Done)
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003144 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003145
Richard Smithdaa69e02014-07-25 04:40:03 +00003146 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3147 // The latest visibility directive for a name in a submodule affects all
3148 // the directives that come before it.
3149 if (S == SubmoduleMacroState::None)
3150 S = VisMD->isPublic() ? SubmoduleMacroState::Public
3151 : SubmoduleMacroState::Done;
3152 } else {
3153 S = SubmoduleMacroState::Done;
Richard Smith49f906a2014-03-01 00:08:04 +00003154 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003155 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003156 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003157
Craig Toppera13603a2014-05-22 05:54:18 +00003158 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003159 }
3160
Richard Smithdaa69e02014-07-25 04:40:03 +00003161 ArrayRef<SubmoduleID>
3162 getOverriddenSubmodules(MacroDirective *MD,
3163 SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3164 assert(!isa<VisibilityMacroDirective>(MD) &&
3165 "only #define and #undef can override");
3166 if (MD->isImported())
3167 return MD->getOverriddenModules();
3168
3169 ScratchSpace.clear();
3170 SubmoduleID ModID = getSubmoduleID(MD);
3171 for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3172 if (shouldIgnoreMacro(MD, IsModule, PP))
3173 break;
3174
3175 // If this is a definition from a submodule import, that submodule's
3176 // definition is overridden by the definition or undefinition that we
3177 // started with.
3178 if (MD->isImported()) {
3179 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3180 SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3181 assert(DefModuleID && "imported macro has no owning module");
3182 ScratchSpace.push_back(DefModuleID);
3183 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3184 // If we override a #undef, we override anything that #undef overrides.
3185 // We don't need to override it, since an active #undef doesn't affect
3186 // the meaning of a macro.
3187 auto Overrides = UndefMD->getOverriddenModules();
3188 ScratchSpace.insert(ScratchSpace.end(),
3189 Overrides.begin(), Overrides.end());
3190 }
3191 }
3192
3193 // Stop once we leave the original macro's submodule.
3194 //
3195 // Either this submodule #included another submodule of the same
3196 // module or it just happened to be built after the other module.
3197 // In the former case, we override the submodule's macro.
3198 //
3199 // FIXME: In the latter case, we shouldn't do so, but we can't tell
3200 // these cases apart.
3201 //
3202 // FIXME: We can leave this submodule and re-enter it if it #includes a
3203 // header within a different submodule of the same module. In such cases
3204 // the overrides list will be incomplete.
3205 SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3206 if (DirectiveModuleID != ModID) {
3207 if (DirectiveModuleID && !MD->isImported())
3208 ScratchSpace.push_back(DirectiveModuleID);
3209 break;
3210 }
3211 }
3212
3213 std::sort(ScratchSpace.begin(), ScratchSpace.end());
3214 ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3215 ScratchSpace.end());
3216 return ScratchSpace;
3217 }
3218
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003219 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Richard Smith57721ac2014-07-21 04:10:40 +00003220 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003221 }
3222
Douglas Gregore84a9da2009-04-20 20:36:09 +00003223public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003224 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003225 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003226
Sebastian Redl539c5062010-08-18 23:57:32 +00003227 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003228 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003229
Justin Bogner25463f12014-04-18 20:27:24 +00003230 typedef unsigned hash_value_type;
3231 typedef unsigned offset_type;
3232
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003233 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3234 IdentifierResolver &IdResolver, bool IsModule)
3235 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003236
Justin Bogner25463f12014-04-18 20:27:24 +00003237 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003238 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003239 }
Mike Stump11289f42009-09-09 15:08:12 +00003240
3241 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003242 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003243 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003244 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Craig Toppera13603a2014-05-22 05:54:18 +00003245 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003246 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003247 DataLen += 2; // 2 bytes for builtin ID
3248 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003249 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003250 DataLen += 4; // MacroDirectives offset.
3251 if (IsModule) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003252 MacroState State;
3253 SmallVector<SubmoduleID, 16> Scratch;
3254 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3255 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003256 DataLen += 4; // MacroInfo ID or ModuleID.
Richard Smithdaa69e02014-07-25 04:40:03 +00003257 if (unsigned NumOverrides =
3258 getOverriddenSubmodules(MD, Scratch).size())
3259 DataLen += 4 * (1 + NumOverrides);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003260 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003261 DataLen += 4; // 0 terminator.
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003262 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003263 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003264
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003265 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3266 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003267 D != DEnd; ++D)
Richard Smithdaa69e02014-07-25 04:40:03 +00003268 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003269 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003270 using namespace llvm::support;
3271 endian::Writer<little> LE(Out);
3272
3273 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003274 // We emit the key length after the data length so that every
3275 // string is preceded by a 16-bit length. This matches the PTH
3276 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003277 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003278 return std::make_pair(KeyLen, DataLen);
3279 }
Mike Stump11289f42009-09-09 15:08:12 +00003280
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003281 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003282 unsigned KeyLen) {
3283 // Record the location of the key data. This is used when generating
3284 // the mapping from persistent IDs to strings.
3285 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003286 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003287 }
Mike Stump11289f42009-09-09 15:08:12 +00003288
Richard Smith49f906a2014-03-01 00:08:04 +00003289 static void emitMacroOverrides(raw_ostream &Out,
Craig Topper00bbdcf2014-06-28 23:22:23 +00003290 ArrayRef<SubmoduleID> Overridden) {
Richard Smith49f906a2014-03-01 00:08:04 +00003291 if (!Overridden.empty()) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003292 using namespace llvm::support;
3293 endian::Writer<little> LE(Out);
3294 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Richard Smithdaa69e02014-07-25 04:40:03 +00003295 for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3296 assert(Overridden[I] && "zero module ID for override");
Justin Bognere1c147c2014-03-28 22:03:19 +00003297 LE.write<uint32_t>(Overridden[I]);
Richard Smithdaa69e02014-07-25 04:40:03 +00003298 }
Richard Smith49f906a2014-03-01 00:08:04 +00003299 }
3300 }
3301
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003302 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003303 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003304 using namespace llvm::support;
3305 endian::Writer<little> LE(Out);
Craig Toppera13603a2014-05-22 05:54:18 +00003306 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003307 if (!isInterestingIdentifier(II, Macro)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003308 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003309 return;
3310 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003311
Justin Bognere1c147c2014-03-28 22:03:19 +00003312 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003313 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3314 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003315 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003316 Bits = 0;
3317 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003318 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003319 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003320 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3321 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003322 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003323 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003324 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003325
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003326 if (HadMacroDefinition) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003327 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003328 if (IsModule) {
3329 // Write the IDs of macros coming from different submodules.
Richard Smithdaa69e02014-07-25 04:40:03 +00003330 MacroState State;
3331 SmallVector<SubmoduleID, 16> Scratch;
3332 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3333 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003334 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003335 // FIXME: If this macro directive was created by #pragma pop_macros,
3336 // or if it was created implicitly by resolving conflicting macros,
3337 // it may be for a different submodule from the one in the MacroInfo
3338 // object. If so, we should write out its owning ModuleID.
3339 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Richard Smith49f906a2014-03-01 00:08:04 +00003340 assert(InfoID);
Justin Bognere1c147c2014-03-28 22:03:19 +00003341 LE.write<uint32_t>(InfoID << 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003342 } else {
Richard Smithdaa69e02014-07-25 04:40:03 +00003343 auto *UndefMD = cast<UndefMacroDirective>(MD);
3344 SubmoduleID Mod = UndefMD->isImported()
3345 ? UndefMD->getOwningModuleID()
3346 : getSubmoduleID(UndefMD);
3347 LE.write<uint32_t>((Mod << 1) | 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003348 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003349 emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003350 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003351 LE.write<uint32_t>(0xdeadbeef);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003352 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003353 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003354
Douglas Gregora868bbd2009-04-21 22:25:48 +00003355 // Emit the declaration IDs in reverse order, because the
3356 // IdentifierResolver provides the declarations as they would be
3357 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003358 // "stat"), but the ASTReader adds declarations to the end of the list
3359 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003360 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003361 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3362 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003363 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003364 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003365 D != DEnd; ++D)
Justin Bognere1c147c2014-03-28 22:03:19 +00003366 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003367 }
3368
3369 /// \brief Returns the most recent local decl or the given decl if there are
3370 /// no local ones. The given decl is assumed to be the most recent one.
3371 Decl *getMostRecentLocalDecl(Decl *Orig) {
3372 // The only way a "from AST file" decl would be more recent from a local one
3373 // is if it came from a module.
3374 if (!PP.getLangOpts().Modules)
3375 return Orig;
3376
3377 // Look for a local in the decl chain.
3378 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3379 if (!D->isFromASTFile())
3380 return D;
3381 // If we come up a decl from a (chained-)PCH stop since we won't find a
3382 // local one.
3383 if (D->getOwningModuleID() == 0)
3384 break;
3385 }
3386
3387 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003388 }
3389};
3390} // end anonymous namespace
3391
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003392/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003393///
3394/// The identifier table consists of a blob containing string data
3395/// (the actual identifiers themselves) and a separate "offsets" index
3396/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003397void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3398 IdentifierResolver &IdResolver,
3399 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003400 using namespace llvm;
3401
3402 // Create and write out the blob that contains the identifier
3403 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003404 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003405 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003406 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003407
Douglas Gregore6648fb2009-04-28 20:33:11 +00003408 // Look for any identifiers that were named while processing the
3409 // headers, but are otherwise not needed. We add these to the hash
3410 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003411 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003412 // file.
3413 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3414 IDEnd = PP.getIdentifierTable().end();
3415 ID != IDEnd; ++ID)
3416 getIdentifierRef(ID->second);
3417
Sebastian Redlff4a2952010-07-23 23:49:55 +00003418 // Create the on-disk hash table representation. We only store offsets
3419 // for identifiers that appear here for the first time.
3420 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003421 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003422 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3423 ID != IDEnd; ++ID) {
3424 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003425 if (!Chain || !ID->first->isFromAST() ||
3426 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003427 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003428 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003429 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003430
Douglas Gregore84a9da2009-04-20 20:36:09 +00003431 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003432 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003433 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003434 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003435 using namespace llvm::support;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003436 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003437 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003438 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003439 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003440 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003441 }
3442
3443 // Create a blob abbreviation
3444 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003445 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003448 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003449
3450 // Write the identifier table
3451 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003452 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003453 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003454 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003455 }
3456
3457 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003458 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003459 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3463 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3464
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003465#ifndef NDEBUG
3466 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3467 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3468#endif
3469
Douglas Gregor0e149972009-04-25 19:10:14 +00003470 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003471 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003472 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003473 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003474 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003475 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003476}
3477
Douglas Gregorc5046832009-04-27 18:38:38 +00003478//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003479// DeclContext's Name Lookup Table Serialization
3480//===----------------------------------------------------------------------===//
3481
Richard Smithbb853c72014-08-13 01:23:33 +00003482/// Determine the declaration that should be put into the name lookup table to
3483/// represent the given declaration in this module. This is usually D itself,
3484/// but if D was imported and merged into a local declaration, we want the most
3485/// recent local declaration instead. The chosen declaration will be the most
3486/// recent declaration in any module that imports this one.
3487static NamedDecl *getDeclForLocalLookup(NamedDecl *D) {
Richard Smith8c913ec2014-08-14 02:21:01 +00003488 if (!D->isFromASTFile())
3489 return D;
3490
3491 if (Decl *Redecl = D->getPreviousDecl()) {
3492 // For Redeclarable decls, a prior declaration might be local.
3493 for (; Redecl; Redecl = Redecl->getPreviousDecl())
3494 if (!Redecl->isFromASTFile())
3495 return cast<NamedDecl>(Redecl);
3496 } else if (Decl *First = D->getCanonicalDecl()) {
3497 // For Mergeable decls, the first decl might be local.
3498 if (!First->isFromASTFile())
3499 return cast<NamedDecl>(First);
3500 }
3501
3502 // All declarations are imported. Our most recent declaration will also be
3503 // the most recent one in anyone who imports us.
Richard Smithbb853c72014-08-13 01:23:33 +00003504 return D;
3505}
3506
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003507namespace {
3508// Trait used for the on-disk hash table used in the method pool.
3509class ASTDeclContextNameLookupTrait {
3510 ASTWriter &Writer;
3511
3512public:
3513 typedef DeclarationName key_type;
3514 typedef key_type key_type_ref;
3515
3516 typedef DeclContext::lookup_result data_type;
3517 typedef const data_type& data_type_ref;
3518
Justin Bogner25463f12014-04-18 20:27:24 +00003519 typedef unsigned hash_value_type;
3520 typedef unsigned offset_type;
3521
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003522 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3523
Justin Bogner25463f12014-04-18 20:27:24 +00003524 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003525 llvm::FoldingSetNodeID ID;
3526 ID.AddInteger(Name.getNameKind());
3527
3528 switch (Name.getNameKind()) {
3529 case DeclarationName::Identifier:
3530 ID.AddString(Name.getAsIdentifierInfo()->getName());
3531 break;
3532 case DeclarationName::ObjCZeroArgSelector:
3533 case DeclarationName::ObjCOneArgSelector:
3534 case DeclarationName::ObjCMultiArgSelector:
3535 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3536 break;
3537 case DeclarationName::CXXConstructorName:
3538 case DeclarationName::CXXDestructorName:
3539 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003540 break;
3541 case DeclarationName::CXXOperatorName:
3542 ID.AddInteger(Name.getCXXOverloadedOperator());
3543 break;
3544 case DeclarationName::CXXLiteralOperatorName:
3545 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3546 case DeclarationName::CXXUsingDirective:
3547 break;
3548 }
3549
3550 return ID.ComputeHash();
3551 }
3552
3553 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003554 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003555 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003556 using namespace llvm::support;
3557 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003558 unsigned KeyLen = 1;
3559 switch (Name.getNameKind()) {
3560 case DeclarationName::Identifier:
3561 case DeclarationName::ObjCZeroArgSelector:
3562 case DeclarationName::ObjCOneArgSelector:
3563 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003564 case DeclarationName::CXXLiteralOperatorName:
3565 KeyLen += 4;
3566 break;
3567 case DeclarationName::CXXOperatorName:
3568 KeyLen += 1;
3569 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003570 case DeclarationName::CXXConstructorName:
3571 case DeclarationName::CXXDestructorName:
3572 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003573 case DeclarationName::CXXUsingDirective:
3574 break;
3575 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003576 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003577
3578 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003579 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003580 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003581
3582 return std::make_pair(KeyLen, DataLen);
3583 }
3584
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003585 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003586 using namespace llvm::support;
3587 endian::Writer<little> LE(Out);
3588 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003589 switch (Name.getNameKind()) {
3590 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003591 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003592 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003593 case DeclarationName::ObjCZeroArgSelector:
3594 case DeclarationName::ObjCOneArgSelector:
3595 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003596 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003597 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003598 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003599 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3600 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003601 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003602 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003603 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003604 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003605 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003606 case DeclarationName::CXXConstructorName:
3607 case DeclarationName::CXXDestructorName:
3608 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003609 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003610 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003611 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003612
3613 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003614 }
3615
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003616 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003617 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003618 using namespace llvm::support;
3619 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003620 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003621 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003622 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3623 I != E; ++I)
Richard Smithbb853c72014-08-13 01:23:33 +00003624 LE.write<uint32_t>(Writer.GetDeclRef(getDeclForLocalLookup(*I)));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003625
3626 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3627 }
3628};
3629} // end anonymous namespace
3630
Richard Smithcd45dbc2014-04-19 03:48:30 +00003631template<typename Visitor>
3632static void visitLocalLookupResults(const DeclContext *ConstDC,
3633 bool NeedToReconcileExternalVisibleStorage,
3634 Visitor AddLookupResult) {
3635 // FIXME: We need to build the lookups table, which is logically const.
3636 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Richard Smith961eae52014-03-25 01:14:22 +00003637 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3638
Richard Smith961eae52014-03-25 01:14:22 +00003639 SmallVector<DeclarationName, 16> ExternalNames;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003640 for (auto &Lookup : *DC->buildLookup()) {
Richard Smith961eae52014-03-25 01:14:22 +00003641 if (Lookup.second.hasExternalDecls() ||
Richard Smithcd45dbc2014-04-19 03:48:30 +00003642 NeedToReconcileExternalVisibleStorage) {
Richard Smith961eae52014-03-25 01:14:22 +00003643 // We don't know for sure what declarations are found by this name,
3644 // because the external source might have a different set from the set
3645 // that are in the lookup map, and we can't update it now without
3646 // risking invalidating our lookup iterator. So add it to a queue to
3647 // deal with later.
3648 ExternalNames.push_back(Lookup.first);
3649 continue;
3650 }
3651
3652 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3653 }
3654
3655 // Add the names we needed to defer. Note, this shouldn't add any new decls
3656 // to the list we need to serialize: any new declarations we find here should
3657 // be imported from an external source.
3658 // FIXME: What if the external source isn't an ASTReader?
3659 for (const auto &Name : ExternalNames)
Richard Smithcd45dbc2014-04-19 03:48:30 +00003660 AddLookupResult(Name, DC->lookup(Name));
3661}
3662
3663void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
3664 if (UpdatedDeclContexts.insert(DC) && WritingAST) {
3665 // Ensure we emit all the visible declarations.
3666 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3667 [&](DeclarationName Name,
3668 DeclContext::lookup_const_result Result) {
3669 for (auto *Decl : Result)
Richard Smithbb853c72014-08-13 01:23:33 +00003670 GetDeclRef(getDeclForLocalLookup(Decl));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003671 });
3672 }
3673}
3674
3675uint32_t
3676ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3677 llvm::SmallVectorImpl<char> &LookupTable) {
3678 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3679
3680 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3681 Generator;
3682 ASTDeclContextNameLookupTrait Trait(*this);
3683
3684 // Create the on-disk hash table representation.
3685 DeclarationName ConstructorName;
3686 DeclarationName ConversionName;
3687 SmallVector<NamedDecl *, 8> ConstructorDecls;
3688 SmallVector<NamedDecl *, 4> ConversionDecls;
3689
3690 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3691 [&](DeclarationName Name,
3692 DeclContext::lookup_result Result) {
3693 if (Result.empty())
3694 return;
3695
3696 // Different DeclarationName values of certain kinds are mapped to
3697 // identical serialized keys, because we don't want to use type
3698 // identifiers in the keys (since type ids are local to the module).
3699 switch (Name.getNameKind()) {
3700 case DeclarationName::CXXConstructorName:
3701 // There may be different CXXConstructorName DeclarationName values
3702 // in a DeclContext because a UsingDecl that inherits constructors
3703 // has the DeclarationName of the inherited constructors.
3704 if (!ConstructorName)
3705 ConstructorName = Name;
3706 ConstructorDecls.append(Result.begin(), Result.end());
3707 return;
3708
3709 case DeclarationName::CXXConversionFunctionName:
3710 if (!ConversionName)
3711 ConversionName = Name;
3712 ConversionDecls.append(Result.begin(), Result.end());
3713 return;
3714
3715 default:
3716 break;
3717 }
3718
3719 Generator.insert(Name, Result, Trait);
3720 });
Richard Smith961eae52014-03-25 01:14:22 +00003721
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003722 // Add the constructors.
3723 if (!ConstructorDecls.empty()) {
3724 Generator.insert(ConstructorName,
3725 DeclContext::lookup_result(ConstructorDecls.begin(),
3726 ConstructorDecls.end()),
3727 Trait);
3728 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003729
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003730 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003731 if (!ConversionDecls.empty()) {
3732 Generator.insert(ConversionName,
3733 DeclContext::lookup_result(ConversionDecls.begin(),
3734 ConversionDecls.end()),
3735 Trait);
3736 }
3737
3738 // Create the on-disk hash table in a buffer.
3739 llvm::raw_svector_ostream Out(LookupTable);
3740 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003741 using namespace llvm::support;
3742 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003743 return Generator.Emit(Out, Trait);
3744}
3745
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003746/// \brief Write the block containing all of the declaration IDs
3747/// visible from the given DeclContext.
3748///
3749/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003750/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003751uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3752 DeclContext *DC) {
3753 if (DC->getPrimaryContext() != DC)
3754 return 0;
3755
3756 // Since there is no name lookup into functions or methods, don't bother to
3757 // build a visible-declarations table for these entities.
3758 if (DC->isFunctionOrMethod())
3759 return 0;
3760
3761 // If not in C++, we perform name lookup for the translation unit via the
3762 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003763 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003764 return 0;
3765
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003766 // Serialize the contents of the mapping used for lookup. Note that,
3767 // although we have two very different code paths, the serialized
3768 // representation is the same for both cases: a declaration name,
3769 // followed by a size, followed by references to the visible
3770 // declarations that have that name.
3771 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003772 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003773 if (!Map || Map->empty())
3774 return 0;
3775
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003776 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003777 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003778 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003779
3780 // Write the lookup table
3781 RecordData Record;
3782 Record.push_back(DECL_CONTEXT_VISIBLE);
3783 Record.push_back(BucketOffset);
3784 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3785 LookupTable.str());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003786 ++NumVisibleDeclContexts;
3787 return Offset;
3788}
3789
Sebastian Redla4071b42010-08-24 00:50:09 +00003790/// \brief Write an UPDATE_VISIBLE block for the given context.
3791///
3792/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3793/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003794/// (in C++), for namespaces, and for classes with forward-declared unscoped
3795/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003796void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003797 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003798 if (!Map || Map->empty())
3799 return;
3800
Sebastian Redla4071b42010-08-24 00:50:09 +00003801 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003802 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003803 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003804
3805 // Write the lookup table
3806 RecordData Record;
3807 Record.push_back(UPDATE_VISIBLE);
3808 Record.push_back(getDeclID(cast<Decl>(DC)));
3809 Record.push_back(BucketOffset);
3810 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3811}
3812
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003813/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3814void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3815 RecordData Record;
3816 Record.push_back(Opts.fp_contract);
3817 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3818}
3819
3820/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3821void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003822 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003823 return;
3824
3825 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3826 RecordData Record;
3827#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3828#include "clang/Basic/OpenCLExtensions.def"
3829 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3830}
3831
Douglas Gregor358cd442012-01-15 16:58:34 +00003832void ASTWriter::WriteRedeclarations() {
3833 RecordData LocalRedeclChains;
3834 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3835
3836 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3837 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003838 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003839
3840 Decl *MostRecent = First->getMostRecentDecl();
3841
3842 // If we only have a single declaration, there is no point in storing
3843 // a redeclaration chain.
3844 if (First == MostRecent)
3845 continue;
3846
3847 unsigned Offset = LocalRedeclChains.size();
3848 unsigned Size = 0;
3849 LocalRedeclChains.push_back(0); // Placeholder for the size.
3850
3851 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003852 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003853 Prev = Prev->getPreviousDecl()) {
3854 if (!Prev->isFromASTFile()) {
3855 AddDeclRef(Prev, LocalRedeclChains);
3856 ++Size;
3857 }
3858 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003859
3860 if (!First->isFromASTFile() && Chain) {
3861 Decl *FirstFromAST = MostRecent;
3862 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3863 if (Prev->isFromASTFile())
3864 FirstFromAST = Prev;
3865 }
3866
Richard Smith2516ba22014-08-11 18:35:44 +00003867 // FIXME: Do we need to do this for the first declaration from each
3868 // redeclaration chain that was merged into this one?
Douglas Gregor6168bd22013-02-18 15:53:43 +00003869 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3870 }
3871
Douglas Gregor358cd442012-01-15 16:58:34 +00003872 LocalRedeclChains[Offset] = Size;
3873
3874 // Reverse the set of local redeclarations, so that we store them in
3875 // order (since we found them in reverse order).
3876 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3877
Douglas Gregor6168bd22013-02-18 15:53:43 +00003878 // Add the mapping from the first ID from the AST to the set of local
3879 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003880 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3881 LocalRedeclsMap.push_back(Info);
3882
3883 assert(N == Redeclarations.size() &&
3884 "Deserialized a declaration we shouldn't have");
3885 }
3886
3887 if (LocalRedeclChains.empty())
3888 return;
3889
3890 // Sort the local redeclarations map by the first declaration ID,
3891 // since the reader will be performing binary searches on this information.
3892 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3893
3894 // Emit the local redeclarations map.
3895 using namespace llvm;
3896 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3897 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3900 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3901
3902 RecordData Record;
3903 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3904 Record.push_back(LocalRedeclsMap.size());
3905 Stream.EmitRecordWithBlob(AbbrevID, Record,
3906 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3907 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3908
3909 // Emit the redeclaration chains.
3910 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3911}
3912
Douglas Gregor404cdde2012-01-27 01:47:08 +00003913void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003914 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003915 RecordData Categories;
3916
3917 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3918 unsigned Size = 0;
3919 unsigned StartIndex = Categories.size();
3920
3921 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3922
3923 // Allocate space for the size.
3924 Categories.push_back(0);
3925
3926 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003927 for (ObjCInterfaceDecl::known_categories_iterator
3928 Cat = Class->known_categories_begin(),
3929 CatEnd = Class->known_categories_end();
3930 Cat != CatEnd; ++Cat, ++Size) {
3931 assert(getDeclID(*Cat) != 0 && "Bogus category");
3932 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003933 }
3934
3935 // Update the size.
3936 Categories[StartIndex] = Size;
3937
3938 // Record this interface -> category map.
3939 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3940 CategoriesMap.push_back(CatInfo);
3941 }
3942
3943 // Sort the categories map by the definition ID, since the reader will be
3944 // performing binary searches on this information.
3945 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3946
3947 // Emit the categories map.
3948 using namespace llvm;
3949 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3950 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3953 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3954
3955 RecordData Record;
3956 Record.push_back(OBJC_CATEGORIES_MAP);
3957 Record.push_back(CategoriesMap.size());
3958 Stream.EmitRecordWithBlob(AbbrevID, Record,
3959 reinterpret_cast<char*>(CategoriesMap.data()),
3960 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3961
3962 // Emit the category lists.
3963 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3964}
3965
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003966void ASTWriter::WriteMergedDecls() {
3967 if (!Chain || Chain->MergedDecls.empty())
3968 return;
3969
3970 RecordData Record;
3971 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3972 IEnd = Chain->MergedDecls.end();
3973 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003974 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Richard Smithcd45dbc2014-04-19 03:48:30 +00003975 : GetDeclRef(I->first);
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003976 assert(CanonID && "Merged declaration not known?");
3977
3978 Record.push_back(CanonID);
3979 Record.push_back(I->second.size());
3980 Record.append(I->second.begin(), I->second.end());
3981 }
3982 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3983}
3984
Richard Smithe40f2ba2013-08-07 21:41:30 +00003985void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3986 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3987
3988 if (LPTMap.empty())
3989 return;
3990
3991 RecordData Record;
3992 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3993 ItEnd = LPTMap.end();
3994 It != ItEnd; ++It) {
3995 LateParsedTemplate *LPT = It->second;
3996 AddDeclRef(It->first, Record);
3997 AddDeclRef(LPT->D, Record);
3998 Record.push_back(LPT->Toks.size());
3999
4000 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4001 TokEnd = LPT->Toks.end();
4002 TokIt != TokEnd; ++TokIt) {
4003 AddToken(*TokIt, Record);
4004 }
4005 }
4006 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4007}
4008
Dario Domizioli13a0a382014-05-23 12:13:25 +00004009/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4010void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4011 RecordData Record;
4012 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4013 AddSourceLocation(PragmaLoc, Record);
4014 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4015}
4016
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004017//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004018// General Serialization Routines
4019//===----------------------------------------------------------------------===//
4020
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004021/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004022void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4023 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004024 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004025 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4026 e = Attrs.end(); i != e; ++i){
4027 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004028 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004029 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004030
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004031#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004032
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004033 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004034}
4035
John McCallf413f5e2013-05-03 00:10:13 +00004036void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4037 AddSourceLocation(Tok.getLocation(), Record);
4038 Record.push_back(Tok.getLength());
4039
4040 // FIXME: When reading literal tokens, reconstruct the literal pointer
4041 // if it is needed.
4042 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4043 // FIXME: Should translate token kind to a stable encoding.
4044 Record.push_back(Tok.getKind());
4045 // FIXME: Should translate token flags to a stable encoding.
4046 Record.push_back(Tok.getFlags());
4047}
4048
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004049void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004050 Record.push_back(Str.size());
4051 Record.insert(Record.end(), Str.begin(), Str.end());
4052}
4053
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004054void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4055 RecordDataImpl &Record) {
4056 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004057 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004058 Record.push_back(*Minor + 1);
4059 else
4060 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004061 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004062 Record.push_back(*Subminor + 1);
4063 else
4064 Record.push_back(0);
4065}
4066
Douglas Gregore84a9da2009-04-20 20:36:09 +00004067/// \brief Note that the identifier II occurs at the given offset
4068/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004069void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004070 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004071 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004072 // up earlier in the chain and thus don't need an offset.
4073 if (ID >= FirstIdentID)
4074 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004075}
4076
Douglas Gregor95c13f52009-04-25 17:48:32 +00004077/// \brief Note that the selector Sel occurs at the given offset
4078/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004079void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004080 unsigned ID = SelectorIDs[Sel];
4081 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004082 // Don't record offsets for selectors that are also available in a different
4083 // file.
4084 if (ID < FirstSelectorID)
4085 return;
4086 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004087}
4088
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004089ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Richard Smith01b2cb42014-07-26 06:37:51 +00004090 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4091 WritingModule(nullptr), WritingAST(false),
4092 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4093 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4094 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4095 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4096 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4097 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4098 NextSubmoduleID(FirstSubmoduleID),
4099 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4100 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4101 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4102 NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4103 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4104 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004105 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004106 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004107 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4108 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4109 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004110
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004111ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004112 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004113}
4114
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004115void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004116 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004117 Module *WritingModule, StringRef isysroot,
4118 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004119 WritingAST = true;
4120
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004121 ASTHasCompilerErrors = hasErrors;
4122
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004123 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004124 Stream.Emit((unsigned)'C', 8);
4125 Stream.Emit((unsigned)'P', 8);
4126 Stream.Emit((unsigned)'C', 8);
4127 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004128
Chris Lattner28fa4e62009-04-26 22:26:21 +00004129 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004130
Douglas Gregoreda8e122011-08-09 15:13:55 +00004131 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004132 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004133 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004134 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004135 Context = nullptr;
4136 PP = nullptr;
4137 this->WritingModule = nullptr;
4138
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004139 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004140}
4141
Douglas Gregora94a1542011-07-27 21:45:57 +00004142template<typename Vector>
4143static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4144 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004145 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4146 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004147 Writer.AddDeclRef(*I, Record);
4148 }
4149}
4150
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004151void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004152 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004153 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004154 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004155 using namespace llvm;
4156
Craig Toppera13603a2014-05-22 05:54:18 +00004157 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004158
Douglas Gregorcf68c582011-12-01 22:20:10 +00004159 // Make sure that the AST reader knows to finalize itself.
4160 if (Chain)
4161 Chain->finalizeForWriting();
4162
Sebastian Redl143413f2010-07-12 22:02:52 +00004163 ASTContext &Context = SemaRef.Context;
4164 Preprocessor &PP = SemaRef.PP;
4165
Douglas Gregordab42432011-08-12 00:15:20 +00004166 // Set up predefined declaration IDs.
4167 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00004168 if (Context.ObjCIdDecl)
4169 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00004170 if (Context.ObjCSelDecl)
4171 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00004172 if (Context.ObjCClassDecl)
4173 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00004174 if (Context.ObjCProtocolClassDecl)
4175 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00004176 if (Context.Int128Decl)
4177 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4178 if (Context.UInt128Decl)
4179 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00004180 if (Context.ObjCInstanceTypeDecl)
4181 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004182 if (Context.BuiltinVaListDecl)
4183 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4184
Douglas Gregor851443c2011-08-12 01:39:19 +00004185 if (!Chain) {
4186 // Make sure that we emit IdentifierInfos (and any attached
4187 // declarations) for builtins. We don't need to do this when we're
4188 // emitting chained PCH files, because all of the builtins will be
4189 // in the original PCH file.
4190 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004191 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004192 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00004193 if (!Context.getLangOpts().NoBuiltin) {
4194 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4195 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004196 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4197 getIdentifierRef(&Table.get(BuiltinNames[I]));
4198 }
4199
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004200 // If there are any out-of-date identifiers, bring them up to date.
4201 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00004202 // Find out-of-date identifiers.
4203 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004204 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4205 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00004206 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004207 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00004208 OutOfDate.push_back(ID->second);
4209 }
4210
4211 // Update the out-of-date identifiers.
4212 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4213 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4214 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004215 }
4216
Richard Smithcd45dbc2014-04-19 03:48:30 +00004217 // If we saw any DeclContext updates before we started writing the AST file,
4218 // make sure all visible decls in those DeclContexts are written out.
4219 if (!UpdatedDeclContexts.empty()) {
4220 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4221 UpdatedDeclContexts.clear();
4222 for (auto *DC : OldUpdatedDeclContexts)
4223 AddUpdatedDeclContext(DC);
4224 }
4225
Chris Lattner0c797362009-09-08 18:19:27 +00004226 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004227 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004228 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004229 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004230 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004231
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004232 // Build a record containing all of the file scoped decls in this file.
4233 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004234 if (!isModule)
4235 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4236 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004237
Douglas Gregor851443c2011-08-12 01:39:19 +00004238 // Build a record containing all of the delegating constructors we still need
4239 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004240 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004241 if (!isModule)
4242 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004243
Douglas Gregor851443c2011-08-12 01:39:19 +00004244 // Write the set of weak, undeclared identifiers. We always write the
4245 // entire table, since later PCH files in a PCH chain are only interested in
4246 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004247 RecordData WeakUndeclaredIdentifiers;
4248 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004249 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004250 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4251 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4252 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4253 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4254 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4255 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4256 }
4257 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004258
Richard Smith78165b52013-01-10 23:43:47 +00004259 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004260 // declarations in this header file. Generally, this record will be
4261 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00004262 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004263 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00004264 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00004265 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00004266 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4267 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00004268 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004269 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00004270 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00004271 }
4272
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004273 // Build a record containing all of the ext_vector declarations.
4274 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004275 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004276
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004277 // Build a record containing all of the VTable uses information.
4278 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004279 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004280 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4281 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4282 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4283 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4284 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004285 }
4286
Nico Weber72889432014-09-06 01:25:55 +00004287 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4288 RecordData UnusedLocalTypedefNameCandidates;
4289 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4290 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4291
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004292 // Build a record containing all of dynamic classes declarations.
4293 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004294 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004295
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004296 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004297 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004298 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004299 I = SemaRef.PendingInstantiations.begin(),
4300 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4301 AddDeclRef(I->first, PendingInstantiations);
4302 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004303 }
4304 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4305 "There are local ones at end of translation unit!");
4306
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004307 // Build a record containing some declaration references.
4308 RecordData SemaDeclRefs;
4309 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4310 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4311 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4312 }
4313
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004314 RecordData CUDASpecialDeclRefs;
4315 if (Context.getcudaConfigureCallDecl()) {
4316 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4317 }
4318
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004319 // Build a record containing all of the known namespaces.
4320 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004321 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004322 I = SemaRef.KnownNamespaces.begin(),
4323 IEnd = SemaRef.KnownNamespaces.end();
4324 I != IEnd; ++I) {
4325 if (!I->second)
4326 AddDeclRef(I->first, KnownNamespaces);
4327 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004328
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004329 // Build a record of all used, undefined objects that require definitions.
4330 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004331
4332 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004333 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004334 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4335 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004336 AddDeclRef(I->first, UndefinedButUsed);
4337 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004338 }
4339
Douglas Gregor112b9072012-10-18 05:31:06 +00004340 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004341 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004342
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004343 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004344 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004345 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004346
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004347 // This is so that older clang versions, before the introduction
4348 // of the control block, can read and reject the newer PCH format.
4349 Record.clear();
4350 Record.push_back(VERSION_MAJOR);
4351 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4352
Douglas Gregor851443c2011-08-12 01:39:19 +00004353 // Create a lexical update block containing all of the declarations in the
4354 // translation unit that do not come from other AST files.
4355 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4356 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004357 for (const auto *I : TU->noload_decls()) {
4358 if (!I->isFromASTFile())
4359 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004360 }
4361
4362 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4363 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4364 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4365 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4366 Record.clear();
4367 Record.push_back(TU_UPDATE_LEXICAL);
4368 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4369 data(NewGlobalDecls));
4370
4371 // And a visible updates block for the translation unit.
4372 Abv = new llvm::BitCodeAbbrev();
4373 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4374 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4375 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4376 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4377 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4378 WriteDeclContextVisibleUpdate(TU);
4379
4380 // If the translation unit has an anonymous namespace, and we don't already
4381 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004382 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004383 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4384 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004385 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004386 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004387 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004388
Richard Smith5652c0f2014-03-21 01:48:23 +00004389 // Add update records for all mangling numbers and static local numbers.
4390 // These aren't really update records, but this is a convenient way of
4391 // tagging this rare extra data onto the declarations.
4392 for (const auto &Number : Context.MangleNumbers)
4393 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004394 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4395 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004396 for (const auto &Number : Context.StaticLocalNumbers)
4397 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004398 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4399 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004400
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004401 // Make sure visible decls, added to DeclContexts previously loaded from
4402 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004403 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004404 I = UpdatingVisibleDecls.begin(),
4405 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4406 GetDeclRef(*I);
4407 }
4408
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004409 // Make sure all decls associated with an identifier are registered for
4410 // serialization.
4411 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4412 IDEnd = PP.getIdentifierTable().end();
4413 ID != IDEnd; ++ID) {
4414 const IdentifierInfo *II = ID->second;
4415 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4416 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4417 DEnd = SemaRef.IdResolver.end();
4418 D != DEnd; ++D) {
4419 GetDeclRef(*D);
4420 }
4421 }
4422 }
4423
Douglas Gregor5204bde2011-08-02 16:26:37 +00004424 // Form the record of special types.
4425 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004426 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004427 AddTypeRef(Context.getFILEType(), SpecialTypes);
4428 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4429 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4430 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4431 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004432 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004433 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004434
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004435 if (Chain) {
4436 // Write the mapping information describing our module dependencies and how
4437 // each of those modules were mapped into our own offset/ID space, so that
4438 // the reader can build the appropriate mapping to its own offset/ID space.
4439 // The map consists solely of a blob with the following format:
4440 // *(module-name-len:i16 module-name:len*i8
4441 // source-location-offset:i32
4442 // identifier-id:i32
4443 // preprocessed-entity-id:i32
4444 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004445 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004446 // selector-id:i32
4447 // declaration-id:i32
4448 // c++-base-specifiers-id:i32
4449 // type-id:i32)
4450 //
4451 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4452 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4454 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004455 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004456 {
4457 llvm::raw_svector_ostream Out(Buffer);
4458 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004459 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004460 M != MEnd; ++M) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004461 using namespace llvm::support;
4462 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004463 StringRef FileName = (*M)->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004464 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004465 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004466
4467 // These values should be unique within a chain, since they will be read
4468 // as keys into ContinuousRangeMaps.
Justin Bognere1c147c2014-03-28 22:03:19 +00004469 LE.write<uint32_t>((*M)->SLocEntryBaseOffset);
4470 LE.write<uint32_t>((*M)->BaseIdentifierID);
4471 LE.write<uint32_t>((*M)->BaseMacroID);
4472 LE.write<uint32_t>((*M)->BasePreprocessedEntityID);
4473 LE.write<uint32_t>((*M)->BaseSubmoduleID);
4474 LE.write<uint32_t>((*M)->BaseSelectorID);
4475 LE.write<uint32_t>((*M)->BaseDeclID);
4476 LE.write<uint32_t>((*M)->BaseTypeIndex);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004477 }
4478 }
4479 Record.clear();
4480 Record.push_back(MODULE_OFFSET_MAP);
4481 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4482 Buffer.data(), Buffer.size());
4483 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004484
Richard Smithb9eab6d2014-03-20 19:44:17 +00004485 RecordData DeclUpdatesOffsetsRecord;
4486
Richard Smith59442b42014-03-20 20:07:19 +00004487 // Keep writing types, declarations, and declaration update records
4488 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004489 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4490 WriteTypeAbbrevs();
4491 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004492 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4493 E = DeclsToRewrite.end();
4494 I != E; ++I)
4495 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004496 do {
4497 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4498 while (!DeclTypesToEmit.empty()) {
4499 DeclOrType DOT = DeclTypesToEmit.front();
4500 DeclTypesToEmit.pop();
4501 if (DOT.isType())
4502 WriteType(DOT.getType());
4503 else
4504 WriteDecl(Context, DOT.getDecl());
4505 }
4506 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004507 Stream.ExitBlock();
4508
Richard Smithb9eab6d2014-03-20 19:44:17 +00004509 DoneWritingDeclsAndTypes = true;
4510
4511 // These things can only be done once we've written out decls and types.
4512 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004513 if (!DeclUpdatesOffsetsRecord.empty())
4514 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004515 WriteCXXBaseSpecifiersOffsets();
4516 WriteFileDeclIDsMap();
4517 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4518
4519 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004520 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004521 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004522 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004523 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004524 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004525 WriteFPPragmaOptions(SemaRef.getFPOptions());
4526 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004527 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004528
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004529 // If we're emitting a module, write out the submodule information.
4530 if (WritingModule)
4531 WriteSubmodules(WritingModule);
4532
Douglas Gregor5204bde2011-08-02 16:26:37 +00004533 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4534
Douglas Gregord4df8652009-04-22 22:02:47 +00004535 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004536 if (!EagerlyDeserializedDecls.empty())
4537 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004538
4539 // Write the record containing tentative definitions.
4540 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004541 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004542
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004543 // Write the record containing unused file scoped decls.
4544 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004545 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004546
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004547 // Write the record containing weak undeclared identifiers.
4548 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004549 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004550 WeakUndeclaredIdentifiers);
4551
Richard Smith78165b52013-01-10 23:43:47 +00004552 // Write the record containing locally-scoped extern "C" definitions.
4553 if (!LocallyScopedExternCDecls.empty())
4554 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4555 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004556
4557 // Write the record containing ext_vector type names.
4558 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004559 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004560
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004561 // Write the record containing VTable uses information.
4562 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004563 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004564
4565 // Write the record containing dynamic classes declarations.
4566 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004567 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004568
Nico Weber72889432014-09-06 01:25:55 +00004569 // Write the record containing potentially unused local typedefs.
4570 if (!UnusedLocalTypedefNameCandidates.empty())
4571 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4572 UnusedLocalTypedefNameCandidates);
4573
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004574 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004575 if (!PendingInstantiations.empty())
4576 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004577
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004578 // Write the record containing declaration references of Sema.
4579 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004580 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004581
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004582 // Write the record containing CUDA-specific declaration references.
4583 if (!CUDASpecialDeclRefs.empty())
4584 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004585
4586 // Write the delegating constructors.
4587 if (!DelegatingCtorDecls.empty())
4588 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004589
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004590 // Write the known namespaces.
4591 if (!KnownNamespaces.empty())
4592 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004593
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004594 // Write the undefined internal functions and variables, and inline functions.
4595 if (!UndefinedButUsed.empty())
4596 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004597
Douglas Gregor851443c2011-08-12 01:39:19 +00004598 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004599 for (auto *DC : UpdatedDeclContexts)
4600 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004601
Douglas Gregor959bb062011-12-03 01:15:29 +00004602 if (!WritingModule) {
4603 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004604 struct ModuleInfo {
4605 uint64_t ID;
4606 Module *M;
4607 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4608 };
Richard Smith56be7542014-03-21 00:33:59 +00004609 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004610 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004611 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004612 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4613 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004614 }
Richard Smith56be7542014-03-21 00:33:59 +00004615
4616 if (!Imports.empty()) {
4617 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4618 return A.ID < B.ID;
4619 };
4620
4621 // Sort and deduplicate module IDs.
4622 std::sort(Imports.begin(), Imports.end(), Cmp);
4623 Imports.erase(std::unique(Imports.begin(), Imports.end(), Cmp),
4624 Imports.end());
4625
4626 RecordData ImportedModules;
4627 for (const auto &Import : Imports) {
4628 ImportedModules.push_back(Import.ID);
4629 // FIXME: If the module has macros imported then later has declarations
4630 // imported, this location won't be the right one as a location for the
4631 // declaration imports.
4632 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4633 }
4634
Douglas Gregor959bb062011-12-03 01:15:29 +00004635 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4636 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004637 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004638
Douglas Gregor851443c2011-08-12 01:39:19 +00004639 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004640 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004641 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004642 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004643 WriteLateParsedTemplates(SemaRef);
Dario Domizioli13a0a382014-05-23 12:13:25 +00004644 if(!WritingModule)
4645 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004646
Douglas Gregor08f01292009-04-17 22:13:46 +00004647 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004648 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004649 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004650 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004651 Record.push_back(NumLexicalDeclContexts);
4652 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004653 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004654 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004655}
4656
Richard Smithb9eab6d2014-03-20 19:44:17 +00004657void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004658 if (DeclUpdates.empty())
4659 return;
4660
Richard Smith59442b42014-03-20 20:07:19 +00004661 DeclUpdateMap LocalUpdates;
4662 LocalUpdates.swap(DeclUpdates);
4663
Richard Smith6ef42932014-03-20 21:02:00 +00004664 for (auto &DeclUpdate : LocalUpdates) {
4665 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004666 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004667 continue; // The decl will be written completely,no need to store updates.
4668
Richard Smithd28ac5b2014-03-22 23:33:22 +00004669 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004670 RecordData Record;
4671 for (auto &Update : DeclUpdate.second) {
4672 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4673
4674 Record.push_back(Kind);
4675 switch (Kind) {
4676 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4677 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4678 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004679 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004680 Record.push_back(GetDeclRef(Update.getDecl()));
4681 break;
4682
Richard Smith4d235792014-08-07 18:53:08 +00004683 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004684 // An updated body is emitted last, so that the reader doesn't need
4685 // to skip over the lazy body to reach statements for other records.
4686 Record.pop_back();
4687 HasUpdatedBody = true;
4688 break;
4689
Richard Smith4d235792014-08-07 18:53:08 +00004690 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4691 AddSourceLocation(Update.getLoc(), Record);
4692 break;
4693
Richard Smithcd45dbc2014-04-19 03:48:30 +00004694 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4695 auto *RD = cast<CXXRecordDecl>(D);
4696 AddUpdatedDeclContext(RD->getPrimaryContext());
4697 AddCXXDefinitionData(RD, Record);
4698 Record.push_back(WriteDeclContextLexicalBlock(
4699 *Context, const_cast<CXXRecordDecl *>(RD)));
4700
4701 // This state is sometimes updated by template instantiation, when we
4702 // switch from the specialization referring to the template declaration
4703 // to it referring to the template definition.
4704 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4705 Record.push_back(MSInfo->getTemplateSpecializationKind());
4706 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4707 } else {
4708 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4709 Record.push_back(Spec->getTemplateSpecializationKind());
4710 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004711
4712 // The instantiation might have been resolved to a partial
4713 // specialization. If so, record which one.
4714 auto From = Spec->getInstantiatedFrom();
4715 if (auto PartialSpec =
4716 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4717 Record.push_back(true);
4718 AddDeclRef(PartialSpec, Record);
4719 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4720 Record);
4721 } else {
4722 Record.push_back(false);
4723 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004724 }
4725 Record.push_back(RD->getTagKind());
4726 AddSourceLocation(RD->getLocation(), Record);
4727 AddSourceLocation(RD->getLocStart(), Record);
4728 AddSourceLocation(RD->getRBraceLoc(), Record);
4729
4730 // Instantiation may change attributes; write them all out afresh.
4731 Record.push_back(D->hasAttrs());
4732 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004733 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4734 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004735
4736 // FIXME: Ensure we don't get here for explicit instantiations.
4737 break;
4738 }
4739
Richard Smith564417a2014-03-20 21:47:22 +00004740 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4741 addExceptionSpec(
4742 *this,
4743 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4744 Record);
4745 break;
4746
Richard Smith6ef42932014-03-20 21:02:00 +00004747 case UPD_CXX_DEDUCED_RETURN_TYPE:
4748 Record.push_back(GetOrCreateTypeID(Update.getType()));
4749 break;
4750
4751 case UPD_DECL_MARKED_USED:
4752 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004753
4754 case UPD_MANGLING_NUMBER:
4755 case UPD_STATIC_LOCAL_NUMBER:
4756 Record.push_back(Update.getNumber());
4757 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004758 }
4759 }
4760
Richard Smithd28ac5b2014-03-22 23:33:22 +00004761 if (HasUpdatedBody) {
4762 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004763 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004764 Record.push_back(Def->isInlined());
4765 AddSourceLocation(Def->getInnerLocStart(), Record);
4766 AddFunctionDefinition(Def, Record);
Richard Smith4d235792014-08-07 18:53:08 +00004767 if (auto *DD = dyn_cast<CXXDestructorDecl>(Def))
4768 Record.push_back(GetDeclRef(DD->getOperatorDelete()));
Richard Smithd28ac5b2014-03-22 23:33:22 +00004769 }
4770
Richard Smithcd45dbc2014-04-19 03:48:30 +00004771 OffsetsRecord.push_back(GetDeclRef(D));
4772 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4773
Richard Smith6ef42932014-03-20 21:02:00 +00004774 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004775
Richard Smithb9eab6d2014-03-20 19:44:17 +00004776 // Flush any statements that were written as part of this update record.
4777 FlushStmts();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004778
4779 // Flush C++ base specifiers, if there are any.
4780 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004781 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004782}
4783
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004784void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004785 if (ReplacedDecls.empty())
4786 return;
4787
4788 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004789 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4790 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004791 Record.push_back(I->ID);
4792 Record.push_back(I->Offset);
4793 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004794 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004795 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004796}
4797
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004798void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004799 Record.push_back(Loc.getRawEncoding());
4800}
4801
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004802void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004803 AddSourceLocation(Range.getBegin(), Record);
4804 AddSourceLocation(Range.getEnd(), Record);
4805}
4806
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004807void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004808 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004809 const uint64_t *Words = Value.getRawData();
4810 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004811}
4812
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004813void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004814 Record.push_back(Value.isUnsigned());
4815 AddAPInt(Value, Record);
4816}
4817
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004818void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004819 AddAPInt(Value.bitcastToAPInt(), Record);
4820}
4821
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004822void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004823 Record.push_back(getIdentifierRef(II));
4824}
4825
Sebastian Redl539c5062010-08-18 23:57:32 +00004826IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004827 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004828 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004829
Sebastian Redl539c5062010-08-18 23:57:32 +00004830 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004831 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004832 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004833 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004834}
4835
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004836MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004837 // Don't emit builtin macros like __LINE__ to the AST file unless they
4838 // have been redefined by the header (in which case they are not
4839 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004840 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004841 return 0;
4842
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004843 MacroID &ID = MacroIDs[MI];
4844 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004845 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004846 MacroInfoToEmitData Info = { Name, MI, ID };
4847 MacroInfosToEmit.push_back(Info);
4848 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004849 return ID;
4850}
4851
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004852MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004853 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004854 return 0;
4855
4856 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4857 return MacroIDs[MI];
4858}
4859
4860uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4861 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4862 return IdentMacroDirectivesOffsetMap[Name];
4863}
4864
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004865void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004866 Record.push_back(getSelectorRef(SelRef));
4867}
4868
Sebastian Redl539c5062010-08-18 23:57:32 +00004869SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004870 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004871 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004872 }
4873
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004874 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004875 if (SID == 0 && Chain) {
4876 // This might trigger a ReadSelector callback, which will set the ID for
4877 // this selector.
4878 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004879 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004880 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004881 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004882 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004883 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004884 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004885 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004886}
4887
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004888void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004889 AddDeclRef(Temp->getDestructor(), Record);
4890}
4891
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004892void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4893 CXXBaseSpecifier const *BasesEnd,
4894 RecordDataImpl &Record) {
4895 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4896 CXXBaseSpecifiersToWrite.push_back(
4897 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4898 Bases, BasesEnd));
4899 Record.push_back(NextCXXBaseSpecifiersID++);
4900}
4901
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004902void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004903 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004904 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004905 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004906 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004907 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004908 break;
4909 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004910 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004911 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004912 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004913 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004914 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004915 break;
4916 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004917 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004918 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004919 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004920 break;
John McCall0ad16662009-10-29 08:12:44 +00004921 case TemplateArgument::Null:
4922 case TemplateArgument::Integral:
4923 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004924 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004925 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004926 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004927 break;
4928 }
4929}
4930
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004931void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004932 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004933 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004934
4935 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4936 bool InfoHasSameExpr
4937 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4938 Record.push_back(InfoHasSameExpr);
4939 if (InfoHasSameExpr)
4940 return; // Avoid storing the same expr twice.
4941 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004942 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4943 Record);
4944}
4945
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004946void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4947 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004948 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004949 AddTypeRef(QualType(), Record);
4950 return;
4951 }
4952
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004953 AddTypeLoc(TInfo->getTypeLoc(), Record);
4954}
4955
4956void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4957 AddTypeRef(TL.getType(), Record);
4958
John McCall8f115c62009-10-16 21:56:05 +00004959 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004960 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004961 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004962}
4963
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004964void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004965 Record.push_back(GetOrCreateTypeID(T));
4966}
4967
Douglas Gregoreda8e122011-08-09 15:13:55 +00004968TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004969 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004970 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004971 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4972}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004973
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004974TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004975 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004976 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004977 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004978}
4979
4980TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4981 if (T.isNull())
4982 return TypeIdx();
4983 assert(!T.getLocalFastQualifiers());
4984
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004985 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004986 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004987 if (DoneWritingDeclsAndTypes) {
4988 assert(0 && "New type seen after serializing all the types to emit!");
4989 return TypeIdx();
4990 }
4991
Douglas Gregor1970d882009-04-26 03:49:13 +00004992 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004993 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004994 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004995 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004996 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004997 return Idx;
4998}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004999
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005000TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005001 if (T.isNull())
5002 return TypeIdx();
5003 assert(!T.getLocalFastQualifiers());
5004
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005005 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5006 assert(I != TypeIdxs.end() && "Type not emitted!");
5007 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005008}
5009
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005010void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005011 Record.push_back(GetDeclRef(D));
5012}
5013
Sebastian Redl539c5062010-08-18 23:57:32 +00005014DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005015 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005016
5017 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005018 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005019 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005020
5021 // If D comes from an AST file, its declaration ID is already known and
5022 // fixed.
5023 if (D->isFromASTFile())
5024 return D->getGlobalID();
5025
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005026 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005027 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005028 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005029 if (DoneWritingDeclsAndTypes) {
5030 assert(0 && "New decl seen after serializing all the decls to emit!");
5031 return 0;
5032 }
5033
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005034 // We haven't seen this declaration before. Give it a new ID and
5035 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005036 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005037 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005038 }
5039
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005040 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005041}
5042
Sebastian Redl539c5062010-08-18 23:57:32 +00005043DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005044 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005045 return 0;
5046
Douglas Gregorb3163e52012-01-05 22:33:30 +00005047 // If D comes from an AST file, its declaration ID is already known and
5048 // fixed.
5049 if (D->isFromASTFile())
5050 return D->getGlobalID();
5051
Douglas Gregore84a9da2009-04-20 20:36:09 +00005052 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5053 return DeclIDs[D];
5054}
5055
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005056void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005057 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005058 assert(D);
5059
5060 SourceLocation Loc = D->getLocation();
5061 if (Loc.isInvalid())
5062 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005063
5064 // We only keep track of the file-level declarations of each file.
5065 if (!D->getLexicalDeclContext()->isFileContext())
5066 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005067 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5068 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005069 if (isa<ParmVarDecl>(D))
5070 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005071
5072 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005073 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005074 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005075 FileID FID;
5076 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005077 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005078 if (FID.isInvalid())
5079 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005080 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005081
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005082 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005083 if (!Info)
5084 Info = new DeclIDInFileInfo();
5085
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005086 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005087 LocDeclIDsTy &Decls = Info->DeclIDs;
5088
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005089 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005090 Decls.push_back(LocDecl);
5091 return;
5092 }
5093
Benjamin Kramer45025c02013-08-24 13:22:59 +00005094 LocDeclIDsTy::iterator I =
5095 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005096
5097 Decls.insert(I, LocDecl);
5098}
5099
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005100void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005101 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005102 Record.push_back(Name.getNameKind());
5103 switch (Name.getNameKind()) {
5104 case DeclarationName::Identifier:
5105 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5106 break;
5107
5108 case DeclarationName::ObjCZeroArgSelector:
5109 case DeclarationName::ObjCOneArgSelector:
5110 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005111 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005112 break;
5113
5114 case DeclarationName::CXXConstructorName:
5115 case DeclarationName::CXXDestructorName:
5116 case DeclarationName::CXXConversionFunctionName:
5117 AddTypeRef(Name.getCXXNameType(), Record);
5118 break;
5119
5120 case DeclarationName::CXXOperatorName:
5121 Record.push_back(Name.getCXXOverloadedOperator());
5122 break;
5123
Alexis Hunt3d221f22009-11-29 07:34:05 +00005124 case DeclarationName::CXXLiteralOperatorName:
5125 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5126 break;
5127
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005128 case DeclarationName::CXXUsingDirective:
5129 // No extra data to emit
5130 break;
5131 }
5132}
Chris Lattnerca025db2010-05-07 21:43:38 +00005133
Richard Smithd08aeb62014-08-28 01:33:39 +00005134unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5135 assert(needsAnonymousDeclarationNumber(D) &&
5136 "expected an anonymous declaration");
5137
5138 // Number the anonymous declarations within this context, if we've not
5139 // already done so.
5140 auto It = AnonymousDeclarationNumbers.find(D);
5141 if (It == AnonymousDeclarationNumbers.end()) {
5142 unsigned Index = 0;
5143 for (Decl *LexicalD : D->getLexicalDeclContext()->decls()) {
5144 auto *ND = dyn_cast<NamedDecl>(LexicalD);
5145 if (!ND || !needsAnonymousDeclarationNumber(ND))
5146 continue;
5147 AnonymousDeclarationNumbers[ND] = Index++;
5148 }
5149
5150 It = AnonymousDeclarationNumbers.find(D);
5151 assert(It != AnonymousDeclarationNumbers.end() &&
5152 "declaration not found within its lexical context");
5153 }
5154
5155 return It->second;
5156}
5157
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005158void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005159 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005160 switch (Name.getNameKind()) {
5161 case DeclarationName::CXXConstructorName:
5162 case DeclarationName::CXXDestructorName:
5163 case DeclarationName::CXXConversionFunctionName:
5164 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5165 break;
5166
5167 case DeclarationName::CXXOperatorName:
5168 AddSourceLocation(
5169 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5170 Record);
5171 AddSourceLocation(
5172 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5173 Record);
5174 break;
5175
5176 case DeclarationName::CXXLiteralOperatorName:
5177 AddSourceLocation(
5178 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5179 Record);
5180 break;
5181
5182 case DeclarationName::Identifier:
5183 case DeclarationName::ObjCZeroArgSelector:
5184 case DeclarationName::ObjCOneArgSelector:
5185 case DeclarationName::ObjCMultiArgSelector:
5186 case DeclarationName::CXXUsingDirective:
5187 break;
5188 }
5189}
5190
5191void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005192 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005193 AddDeclarationName(NameInfo.getName(), Record);
5194 AddSourceLocation(NameInfo.getLoc(), Record);
5195 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5196}
5197
5198void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005199 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005200 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005201 Record.push_back(Info.NumTemplParamLists);
5202 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5203 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5204}
5205
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005206void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005207 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005208 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005209 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005210 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005211
5212 // Push each of the NNS's onto a stack for serialization in reverse order.
5213 while (NNS) {
5214 NestedNames.push_back(NNS);
5215 NNS = NNS->getPrefix();
5216 }
5217
5218 Record.push_back(NestedNames.size());
5219 while(!NestedNames.empty()) {
5220 NNS = NestedNames.pop_back_val();
5221 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5222 Record.push_back(Kind);
5223 switch (Kind) {
5224 case NestedNameSpecifier::Identifier:
5225 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5226 break;
5227
5228 case NestedNameSpecifier::Namespace:
5229 AddDeclRef(NNS->getAsNamespace(), Record);
5230 break;
5231
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005232 case NestedNameSpecifier::NamespaceAlias:
5233 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5234 break;
5235
Chris Lattnerca025db2010-05-07 21:43:38 +00005236 case NestedNameSpecifier::TypeSpec:
5237 case NestedNameSpecifier::TypeSpecWithTemplate:
5238 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5239 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5240 break;
5241
5242 case NestedNameSpecifier::Global:
5243 // Don't need to write an associated value.
5244 break;
5245 }
5246 }
5247}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005248
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005249void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5250 RecordDataImpl &Record) {
5251 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005252 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005253 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005254
5255 // Push each of the nested-name-specifiers's onto a stack for
5256 // serialization in reverse order.
5257 while (NNS) {
5258 NestedNames.push_back(NNS);
5259 NNS = NNS.getPrefix();
5260 }
5261
5262 Record.push_back(NestedNames.size());
5263 while(!NestedNames.empty()) {
5264 NNS = NestedNames.pop_back_val();
5265 NestedNameSpecifier::SpecifierKind Kind
5266 = NNS.getNestedNameSpecifier()->getKind();
5267 Record.push_back(Kind);
5268 switch (Kind) {
5269 case NestedNameSpecifier::Identifier:
5270 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5271 AddSourceRange(NNS.getLocalSourceRange(), Record);
5272 break;
5273
5274 case NestedNameSpecifier::Namespace:
5275 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5276 AddSourceRange(NNS.getLocalSourceRange(), Record);
5277 break;
5278
5279 case NestedNameSpecifier::NamespaceAlias:
5280 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5281 AddSourceRange(NNS.getLocalSourceRange(), Record);
5282 break;
5283
5284 case NestedNameSpecifier::TypeSpec:
5285 case NestedNameSpecifier::TypeSpecWithTemplate:
5286 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5287 AddTypeLoc(NNS.getTypeLoc(), Record);
5288 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5289 break;
5290
5291 case NestedNameSpecifier::Global:
5292 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5293 break;
5294 }
5295 }
5296}
5297
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005298void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005299 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005300 Record.push_back(Kind);
5301 switch (Kind) {
5302 case TemplateName::Template:
5303 AddDeclRef(Name.getAsTemplateDecl(), Record);
5304 break;
5305
5306 case TemplateName::OverloadedTemplate: {
5307 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5308 Record.push_back(OvT->size());
5309 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5310 I != E; ++I)
5311 AddDeclRef(*I, Record);
5312 break;
5313 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005314
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005315 case TemplateName::QualifiedTemplate: {
5316 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5317 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5318 Record.push_back(QualT->hasTemplateKeyword());
5319 AddDeclRef(QualT->getTemplateDecl(), Record);
5320 break;
5321 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005322
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005323 case TemplateName::DependentTemplate: {
5324 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5325 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5326 Record.push_back(DepT->isIdentifier());
5327 if (DepT->isIdentifier())
5328 AddIdentifierRef(DepT->getIdentifier(), Record);
5329 else
5330 Record.push_back(DepT->getOperator());
5331 break;
5332 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005333
5334 case TemplateName::SubstTemplateTemplateParm: {
5335 SubstTemplateTemplateParmStorage *subst
5336 = Name.getAsSubstTemplateTemplateParm();
5337 AddDeclRef(subst->getParameter(), Record);
5338 AddTemplateName(subst->getReplacement(), Record);
5339 break;
5340 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005341
5342 case TemplateName::SubstTemplateTemplateParmPack: {
5343 SubstTemplateTemplateParmPackStorage *SubstPack
5344 = Name.getAsSubstTemplateTemplateParmPack();
5345 AddDeclRef(SubstPack->getParameterPack(), Record);
5346 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5347 break;
5348 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005349 }
5350}
5351
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005352void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005353 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005354 Record.push_back(Arg.getKind());
5355 switch (Arg.getKind()) {
5356 case TemplateArgument::Null:
5357 break;
5358 case TemplateArgument::Type:
5359 AddTypeRef(Arg.getAsType(), Record);
5360 break;
5361 case TemplateArgument::Declaration:
5362 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005363 Record.push_back(Arg.isDeclForReferenceParam());
5364 break;
5365 case TemplateArgument::NullPtr:
5366 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005367 break;
5368 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005369 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005370 AddTypeRef(Arg.getIntegralType(), Record);
5371 break;
5372 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005373 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5374 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005375 case TemplateArgument::TemplateExpansion:
5376 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005377 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005378 Record.push_back(*NumExpansions + 1);
5379 else
5380 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005381 break;
5382 case TemplateArgument::Expression:
5383 AddStmt(Arg.getAsExpr());
5384 break;
5385 case TemplateArgument::Pack:
5386 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005387 for (const auto &P : Arg.pack_elements())
5388 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005389 break;
5390 }
5391}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005392
5393void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005394ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005395 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005396 assert(TemplateParams && "No TemplateParams!");
5397 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5398 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5399 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5400 Record.push_back(TemplateParams->size());
5401 for (TemplateParameterList::const_iterator
5402 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5403 P != PEnd; ++P)
5404 AddDeclRef(*P, Record);
5405}
5406
5407/// \brief Emit a template argument list.
5408void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005409ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005410 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005411 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005412 Record.push_back(TemplateArgs->size());
5413 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005414 AddTemplateArgument(TemplateArgs->get(i), Record);
5415}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005416
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005417void
5418ASTWriter::AddASTTemplateArgumentListInfo
5419(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5420 assert(ASTTemplArgList && "No ASTTemplArgList!");
5421 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5422 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5423 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5424 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5425 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5426 AddTemplateArgumentLoc(TemplArgs[i], Record);
5427}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005428
5429void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005430ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005431 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005432 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005433 I = Set.begin(), E = Set.end(); I != E; ++I) {
5434 AddDeclRef(I.getDecl(), Record);
5435 Record.push_back(I.getAccess());
5436 }
5437}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005438
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005439void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005440 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005441 Record.push_back(Base.isVirtual());
5442 Record.push_back(Base.isBaseOfClass());
5443 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005444 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005445 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005446 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005447 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5448 : SourceLocation(),
5449 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005450}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005451
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005452void ASTWriter::FlushCXXBaseSpecifiers() {
5453 RecordData Record;
5454 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5455 Record.clear();
5456
5457 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005458 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005459 if (Index == CXXBaseSpecifiersOffsets.size())
5460 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5461 else {
5462 if (Index > CXXBaseSpecifiersOffsets.size())
5463 CXXBaseSpecifiersOffsets.resize(Index + 1);
5464 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5465 }
5466
5467 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5468 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5469 Record.push_back(BEnd - B);
5470 for (; B != BEnd; ++B)
5471 AddCXXBaseSpecifier(*B, Record);
5472 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005473
5474 // Flush any expressions that were written as part of the base specifiers.
5475 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005476 }
5477
5478 CXXBaseSpecifiersToWrite.clear();
5479}
5480
Alexis Hunt1d792652011-01-08 20:30:50 +00005481void ASTWriter::AddCXXCtorInitializers(
5482 const CXXCtorInitializer * const *CtorInitializers,
5483 unsigned NumCtorInitializers,
5484 RecordDataImpl &Record) {
5485 Record.push_back(NumCtorInitializers);
5486 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5487 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005488
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005489 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005490 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005491 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005492 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005493 } else if (Init->isDelegatingInitializer()) {
5494 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005495 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005496 } else if (Init->isMemberInitializer()){
5497 Record.push_back(CTOR_INITIALIZER_MEMBER);
5498 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005499 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005500 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5501 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005502 }
Francois Pichetd583da02010-12-04 09:14:42 +00005503
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005504 AddSourceLocation(Init->getMemberLocation(), Record);
5505 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005506 AddSourceLocation(Init->getLParenLoc(), Record);
5507 AddSourceLocation(Init->getRParenLoc(), Record);
5508 Record.push_back(Init->isWritten());
5509 if (Init->isWritten()) {
5510 Record.push_back(Init->getSourceOrder());
5511 } else {
5512 Record.push_back(Init->getNumArrayIndices());
5513 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5514 AddDeclRef(Init->getArrayIndex(i), Record);
5515 }
5516 }
5517}
5518
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005519void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005520 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005521 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005522 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005523 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005524 Record.push_back(Data.Aggregate);
5525 Record.push_back(Data.PlainOldData);
5526 Record.push_back(Data.Empty);
5527 Record.push_back(Data.Polymorphic);
5528 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005529 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005530 Record.push_back(Data.HasNoNonEmptyBases);
5531 Record.push_back(Data.HasPrivateFields);
5532 Record.push_back(Data.HasProtectedFields);
5533 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005534 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005535 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005536 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005537 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005538 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005539 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5540 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5541 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5542 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5543 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5544 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005545 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005546 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005547 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005548 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005549 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005550 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005551 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005552 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005553 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005554 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005555 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5556 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5557 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5558 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005559 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005560
5561 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005562 if (Data.NumBases > 0)
5563 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5564 Record);
5565
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005566 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5567 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005568 if (Data.NumVBases > 0)
5569 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5570 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005571
Richard Smitha4ba74c2013-08-30 04:46:40 +00005572 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5573 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005574 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005575 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005576
5577 // Add lambda-specific data.
5578 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005579 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005580 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005581 Record.push_back(Lambda.IsGenericLambda);
5582 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005583 Record.push_back(Lambda.NumCaptures);
5584 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005585 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005586 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005587 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005588 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005589 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005590 AddSourceLocation(Capture.getLocation(), Record);
5591 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005592 Record.push_back(Capture.getCaptureKind());
5593 switch (Capture.getCaptureKind()) {
5594 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005595 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005596 break;
5597 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005598 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005599 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005600 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005601 AddDeclRef(Var, Record);
5602 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5603 : SourceLocation(),
5604 Record);
5605 break;
5606 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005607 }
5608 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005609}
5610
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005611void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005612 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005613 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005614 assert(FirstDeclID == NextDeclID &&
5615 FirstTypeID == NextTypeID &&
5616 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005617 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005618 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005619 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005620 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005621
Sebastian Redl07a89a82010-07-30 00:29:29 +00005622 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005623
Douglas Gregordf0c1512011-08-18 04:12:04 +00005624 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5625 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5626 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005627 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005628 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005629 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005630 NextDeclID = FirstDeclID;
5631 NextTypeID = FirstTypeID;
5632 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005633 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005634 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005635 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005636}
5637
Sebastian Redl539c5062010-08-18 23:57:32 +00005638void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005639 // Always keep the highest ID. See \p TypeRead() for more information.
5640 IdentID &StoredID = IdentifierIDs[II];
5641 if (ID > StoredID)
5642 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005643}
5644
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005645void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005646 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005647 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005648 if (ID > StoredID)
5649 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005650}
5651
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005652void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005653 // Always take the highest-numbered type index. This copes with an interesting
5654 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005655 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005656 // keep the higher-numbered entry so that we can properly write it out to
5657 // the AST file.
5658 TypeIdx &StoredIdx = TypeIdxs[T];
5659 if (Idx.getIndex() >= StoredIdx.getIndex())
5660 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005661}
5662
Sebastian Redl539c5062010-08-18 23:57:32 +00005663void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005664 // Always keep the highest ID. See \p TypeRead() for more information.
5665 SelectorID &StoredID = SelectorIDs[S];
5666 if (ID > StoredID)
5667 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005668}
Douglas Gregor91096292010-10-02 19:29:26 +00005669
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005670void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005671 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005672 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005673 MacroDefinitions[MD] = ID;
5674}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005675
Douglas Gregore37a85a2011-12-02 17:30:13 +00005676void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5677 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5678 SubmoduleIDs[Mod] = ID;
5679}
5680
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005681void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005682 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005683 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005684 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5685 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005686 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005687 // A forward reference was mutated into a definition. Rewrite it.
5688 // FIXME: This happens during template instantiation, should we
5689 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005690 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5691 "completed a tag from another module but not by instantiation?");
5692 DeclUpdates[RD].push_back(
5693 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005694 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005695 }
5696}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005697
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005698void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005699 assert(!WritingAST && "Already writing the AST!");
5700
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005701 // TU and namespaces are handled elsewhere.
5702 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5703 return;
5704
Douglas Gregorb3722e22011-09-09 23:01:35 +00005705 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005706 return; // Not a source decl added to a DeclContext from PCH.
5707
Douglas Gregor9f782892013-01-21 15:25:38 +00005708 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005709 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005710 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005711}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005712
5713void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005714 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005715 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005716 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005717 return; // Not a source member added to a class from PCH.
5718 if (!isa<CXXMethodDecl>(D))
5719 return; // We are interested in lazily declared implicit methods.
5720
5721 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005722 assert(RD->isCompleteDefinition());
Aaron Ballman4f45b712014-03-21 15:22:56 +00005723 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005724}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005725
5726void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5727 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005728 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005729 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005730 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005731 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005732 return; // Not a source specialization added to a template from PCH.
5733
Aaron Ballman4f45b712014-03-21 15:22:56 +00005734 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5735 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005736}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005737
Larisse Voufo39a1e502013-08-06 01:03:05 +00005738void ASTWriter::AddedCXXTemplateSpecialization(
5739 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5740 // The specializations set is kept in the canonical template.
5741 assert(!WritingAST && "Already writing the AST!");
5742 TD = TD->getCanonicalDecl();
5743 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5744 return; // Not a source specialization added to a template from PCH.
5745
Aaron Ballman4f45b712014-03-21 15:22:56 +00005746 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5747 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005748}
5749
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005750void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5751 const FunctionDecl *D) {
5752 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005753 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005754 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005755 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005756 return; // Not a source specialization added to a template from PCH.
5757
Aaron Ballman4f45b712014-03-21 15:22:56 +00005758 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5759 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005760}
5761
Richard Smith564417a2014-03-20 21:47:22 +00005762void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5763 assert(!WritingAST && "Already writing the AST!");
5764 FD = FD->getCanonicalDecl();
5765 if (!FD->isFromASTFile())
5766 return; // Not a function declared in PCH and defined outside.
5767
5768 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5769}
5770
Richard Smith1fa5d642013-05-11 05:45:24 +00005771void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5772 assert(!WritingAST && "Already writing the AST!");
5773 FD = FD->getCanonicalDecl();
5774 if (!FD->isFromASTFile())
5775 return; // Not a function declared in PCH and defined outside.
5776
Aaron Ballman4f45b712014-03-21 15:22:56 +00005777 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith1fa5d642013-05-11 05:45:24 +00005778}
5779
Sebastian Redlab238a72011-04-24 16:28:06 +00005780void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005781 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005782 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005783 return; // Declaration not imported from PCH.
5784
Richard Smith4d235792014-08-07 18:53:08 +00005785 // Implicit function decl from a PCH was defined.
5786 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005787}
5788
Richard Smithd28ac5b2014-03-22 23:33:22 +00005789void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5790 assert(!WritingAST && "Already writing the AST!");
5791 if (!D->isFromASTFile())
5792 return;
5793
Richard Smithd28ac5b2014-03-22 23:33:22 +00005794 DeclUpdates[D].push_back(
Richard Smith4d235792014-08-07 18:53:08 +00005795 DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005796}
5797
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005798void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005799 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005800 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005801 return;
5802
5803 // Since the actual instantiation is delayed, this really means that we need
5804 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005805 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005806 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5807 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005808}
5809
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005810void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5811 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005812 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005813 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005814 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005815
5816 assert(IFD->getDefinition() && "Category on a class without a definition?");
5817 ObjCClassesWithCategories.insert(
5818 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005819}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005820
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005821
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005822void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5823 const ObjCPropertyDecl *OrigProp,
5824 const ObjCCategoryDecl *ClassExt) {
5825 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5826 if (!D)
5827 return;
5828
5829 assert(!WritingAST && "Already writing the AST!");
5830 if (!D->isFromASTFile())
5831 return; // Declaration not imported from PCH.
5832
5833 RewriteDecl(D);
5834}
Eli Friedman276dd182013-09-05 00:02:25 +00005835
5836void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5837 assert(!WritingAST && "Already writing the AST!");
5838 if (!D->isFromASTFile())
5839 return;
5840
Aaron Ballman4f45b712014-03-21 15:22:56 +00005841 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005842}