blob: 0681e3975d263e1b3f1509dafdf0ceade00edc24 [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();
Richard Smith306d8922014-10-22 23:50:56 +00002408 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2410 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2411
2412 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002413 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2415 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2416
2417 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002418 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2421 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2422
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002423 Abbrev = new BitCodeAbbrev();
2424 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2426 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2427
Douglas Gregorfb912652013-03-20 21:10:35 +00002428 Abbrev = new BitCodeAbbrev();
2429 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2432 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2433
Douglas Gregor253eefe2011-12-01 00:59:36 +00002434 // Write the submodule metadata block.
2435 RecordData Record;
2436 Record.push_back(getNumberOfModules(WritingModule));
2437 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2438 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2439
Douglas Gregor69021972011-11-30 17:33:56 +00002440 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002441 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002442 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002443 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002444 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002445 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002446 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002447
2448 // Emit the definition of the block.
2449 Record.clear();
2450 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002451 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002452 if (Mod->Parent) {
2453 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2454 Record.push_back(SubmoduleIDs[Mod->Parent]);
2455 } else {
2456 Record.push_back(0);
2457 }
2458 Record.push_back(Mod->IsFramework);
2459 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002460 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002461 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002462 Record.push_back(Mod->InferSubmodules);
2463 Record.push_back(Mod->InferExplicitSubmodules);
2464 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002465 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002466 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2467
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002468 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002469 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002470 Record.clear();
2471 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002472 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002473 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002474 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002475 }
2476
Douglas Gregor69021972011-11-30 17:33:56 +00002477 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002478 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002479 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002480 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002481 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002482 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002483 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2484 Record.clear();
2485 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2486 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2487 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002488 }
Richard Smith306d8922014-10-22 23:50:56 +00002489
Douglas Gregor69021972011-11-30 17:33:56 +00002490 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002491 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002492 Record.clear();
2493 Record.push_back(SUBMODULE_HEADER);
2494 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002495 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002496 }
Douglas Gregor59527662012-10-15 06:28:11 +00002497 // Emit the excluded headers.
2498 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2499 Record.clear();
2500 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2501 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2502 Mod->ExcludedHeaders[I]->getName());
2503 }
Richard Smith306d8922014-10-22 23:50:56 +00002504 // Emit the textual headers.
2505 for (unsigned I = 0, N = Mod->TextualHeaders.size(); I != N; ++I) {
2506 Record.clear();
2507 Record.push_back(SUBMODULE_TEXTUAL_HEADER);
2508 Stream.EmitRecordWithBlob(TextualHeaderAbbrev, Record,
2509 Mod->TextualHeaders[I]->getName());
2510 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002511 // Emit the private headers.
2512 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2513 Record.clear();
2514 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2515 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2516 Mod->PrivateHeaders[I]->getName());
2517 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002518 ArrayRef<const FileEntry *>
2519 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2520 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002521 Record.clear();
2522 Record.push_back(SUBMODULE_TOPHEADER);
2523 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002524 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002525 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002526
2527 // Emit the imports.
2528 if (!Mod->Imports.empty()) {
2529 Record.clear();
2530 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002531 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002532 assert(ImportedID && "Unknown submodule!");
2533 Record.push_back(ImportedID);
2534 }
2535 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2536 }
2537
Douglas Gregor24bb9232011-12-02 18:58:38 +00002538 // Emit the exports.
2539 if (!Mod->Exports.empty()) {
2540 Record.clear();
2541 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002542 if (Module *Exported = Mod->Exports[I].getPointer()) {
2543 unsigned ExportedID = SubmoduleIDs[Exported];
2544 assert(ExportedID > 0 && "Unknown submodule ID?");
2545 Record.push_back(ExportedID);
2546 } else {
2547 Record.push_back(0);
2548 }
2549
Douglas Gregor24bb9232011-12-02 18:58:38 +00002550 Record.push_back(Mod->Exports[I].getInt());
2551 }
2552 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2553 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002554
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002555 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2556 // Might be unnecessary as use declarations are only used to build the
2557 // module itself.
2558
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002559 // Emit the link libraries.
2560 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2561 Record.clear();
2562 Record.push_back(SUBMODULE_LINK_LIBRARY);
2563 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2564 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2565 Mod->LinkLibraries[I].Library);
2566 }
2567
Douglas Gregorfb912652013-03-20 21:10:35 +00002568 // Emit the conflicts.
2569 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2570 Record.clear();
2571 Record.push_back(SUBMODULE_CONFLICT);
2572 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2573 assert(OtherID && "Unknown submodule!");
2574 Record.push_back(OtherID);
2575 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2576 Mod->Conflicts[I].Message);
2577 }
2578
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002579 // Emit the configuration macros.
2580 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2581 Record.clear();
2582 Record.push_back(SUBMODULE_CONFIG_MACRO);
2583 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2584 Mod->ConfigMacros[I]);
2585 }
2586
Douglas Gregor69021972011-11-30 17:33:56 +00002587 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002588 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2589 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002590 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002591 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002592 }
2593
2594 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002595
2596 assert((NextSubmoduleID - FirstSubmoduleID
2597 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002598}
2599
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002600serialization::SubmoduleID
2601ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002602 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002603 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002604
2605 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002606 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002607 Module *OwningMod
2608 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002609 if (!OwningMod)
2610 return 0;
2611
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002612 // Check whether this submodule is part of our own module.
2613 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002614 return 0;
2615
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002616 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002617}
2618
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002619void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2620 bool isModule) {
2621 // Make sure set diagnostic pragmas don't affect the translation unit that
2622 // imports the module.
2623 // FIXME: Make diagnostic pragma sections work properly with modules.
2624 if (isModule)
2625 return;
2626
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002627 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2628 DiagStateIDMap;
2629 unsigned CurrID = 0;
2630 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002631 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002632 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002633 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2634 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002635 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002636 if (point.Loc.isInvalid())
2637 continue;
2638
2639 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002640 unsigned &DiagStateID = DiagStateIDMap[point.State];
2641 Record.push_back(DiagStateID);
2642
2643 if (DiagStateID == 0) {
2644 DiagStateID = ++CurrID;
2645 for (DiagnosticsEngine::DiagState::const_iterator
2646 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2647 if (I->second.isPragma()) {
2648 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002649 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002650 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002651 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002652 Record.push_back(-1); // mark the end of the diag/map pairs for this
2653 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002654 }
2655 }
2656
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002657 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002658 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002659}
2660
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002661void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2662 if (CXXBaseSpecifiersOffsets.empty())
2663 return;
2664
2665 RecordData Record;
2666
2667 // Create a blob abbreviation for the C++ base specifiers offsets.
2668 using namespace llvm;
2669
2670 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2671 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2672 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2673 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2674 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2675
Douglas Gregorc27b2872011-08-04 00:01:48 +00002676 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002677 Record.clear();
2678 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2679 Record.push_back(CXXBaseSpecifiersOffsets.size());
2680 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002681 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002682}
2683
Douglas Gregorc5046832009-04-27 18:38:38 +00002684//===----------------------------------------------------------------------===//
2685// Type Serialization
2686//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002687
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002688/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002689void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002690 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002691 if (Idx.getIndex() == 0) // we haven't seen this type before.
2692 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002693
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002694 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002695
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002696 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002697 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002698 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002699 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002700 else if (TypeOffsets.size() < Index) {
2701 TypeOffsets.resize(Index + 1);
2702 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002703 }
2704
2705 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002706
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002707 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002708 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002709 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002710
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002711 if (T.hasLocalNonFastQualifiers()) {
2712 Qualifiers Qs = T.getLocalQualifiers();
2713 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002714 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002715 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002716 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002717 } else {
2718 switch (T->getTypeClass()) {
2719 // For all of the concrete, non-dependent types, call the
2720 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002721#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002722 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002723#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002724#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002725 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002726 }
2727
2728 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002729 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002730
2731 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002732 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002733}
2734
Douglas Gregorc5046832009-04-27 18:38:38 +00002735//===----------------------------------------------------------------------===//
2736// Declaration Serialization
2737//===----------------------------------------------------------------------===//
2738
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002739/// \brief Write the block containing all of the declaration IDs
2740/// lexically declared within the given DeclContext.
2741///
2742/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2743/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002744uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002745 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002746 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002747 return 0;
2748
Douglas Gregor8f45df52009-04-16 22:23:12 +00002749 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002750 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002751 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002752 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002753 for (const auto *D : DC->decls())
2754 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002755
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002756 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002757 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002758 return Offset;
2759}
2760
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002761void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002762 using namespace llvm;
2763 RecordData Record;
2764
2765 // Write the type offsets array
2766 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002767 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2771 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2772 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002773 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002774 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002775 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002776 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002777
2778 // Write the declaration offsets array
2779 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002780 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002781 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2784 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2785 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002786 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002787 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002788 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002789 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002790}
2791
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002792void ASTWriter::WriteFileDeclIDsMap() {
2793 using namespace llvm;
2794 RecordData Record;
2795
2796 // Join the vectors of DeclIDs from all files.
2797 SmallVector<DeclID, 256> FileSortedIDs;
2798 for (FileDeclIDsTy::iterator
2799 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2800 DeclIDInFileInfo &Info = *FI->second;
2801 Info.FirstDeclIndex = FileSortedIDs.size();
2802 for (LocDeclIDsTy::iterator
2803 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2804 FileSortedIDs.push_back(DI->second);
2805 }
2806
2807 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2808 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2811 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2812 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002813 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002814 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2815}
2816
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002817void ASTWriter::WriteComments() {
2818 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002819 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002820 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002821 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2822 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002823 I != E; ++I) {
2824 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002825 AddSourceRange((*I)->getSourceRange(), Record);
2826 Record.push_back((*I)->getKind());
2827 Record.push_back((*I)->isTrailingComment());
2828 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002829 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2830 }
2831 Stream.ExitBlock();
2832}
2833
Douglas Gregorc5046832009-04-27 18:38:38 +00002834//===----------------------------------------------------------------------===//
2835// Global Method Pool and Selector Serialization
2836//===----------------------------------------------------------------------===//
2837
Douglas Gregore84a9da2009-04-20 20:36:09 +00002838namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002839// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002840class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002841 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002842
2843public:
2844 typedef Selector key_type;
2845 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002846
Sebastian Redl834bb972010-08-04 17:20:04 +00002847 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002848 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002849 ObjCMethodList Instance, Factory;
2850 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002851 typedef const data_type& data_type_ref;
2852
Justin Bogner25463f12014-04-18 20:27:24 +00002853 typedef unsigned hash_value_type;
2854 typedef unsigned offset_type;
2855
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002856 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002857
Justin Bogner25463f12014-04-18 20:27:24 +00002858 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002859 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002860 }
Mike Stump11289f42009-09-09 15:08:12 +00002861
2862 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002863 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002864 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002865 using namespace llvm::support;
2866 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002867 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002868 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002869 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2870 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002871 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002872 if (Method->Method)
2873 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002874 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002875 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002876 if (Method->Method)
2877 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002878 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002879 return std::make_pair(KeyLen, DataLen);
2880 }
Mike Stump11289f42009-09-09 15:08:12 +00002881
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002882 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002883 using namespace llvm::support;
2884 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002885 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002886 assert((Start >> 32) == 0 && "Selector key offset too large");
2887 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002888 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002889 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002890 if (N == 0)
2891 N = 1;
2892 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002893 LE.write<uint32_t>(
2894 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002895 }
Mike Stump11289f42009-09-09 15:08:12 +00002896
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002897 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002898 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002899 using namespace llvm::support;
2900 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002901 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002902 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002903 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002904 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002905 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002906 if (Method->Method)
2907 ++NumInstanceMethods;
2908
2909 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002910 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002911 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002912 if (Method->Method)
2913 ++NumFactoryMethods;
2914
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002915 unsigned InstanceBits = Methods.Instance.getBits();
2916 assert(InstanceBits < 4);
2917 unsigned NumInstanceMethodsAndBits =
2918 (NumInstanceMethods << 2) | InstanceBits;
2919 unsigned FactoryBits = Methods.Factory.getBits();
2920 assert(FactoryBits < 4);
2921 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Justin Bognere1c147c2014-03-28 22:03:19 +00002922 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2923 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002924 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002925 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002926 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002927 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002928 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002929 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002930 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002931 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002932
2933 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002934 }
2935};
2936} // end anonymous namespace
2937
Sebastian Redla19a67f2010-08-03 21:58:15 +00002938/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002939///
2940/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002941/// in an on-disk hash table indexed by the selector. The hash table also
2942/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002943void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002944 using namespace llvm;
2945
Sebastian Redla19a67f2010-08-03 21:58:15 +00002946 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002947 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002948 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002949 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002950 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002951 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002952 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002953 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002954
Sebastian Redla19a67f2010-08-03 21:58:15 +00002955 // Create the on-disk hash table representation. We walk through every
2956 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002957 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002958 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002959 I = SelectorIDs.begin(), E = SelectorIDs.end();
2960 I != E; ++I) {
2961 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002962 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002963 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002964 I->second,
2965 ObjCMethodList(),
2966 ObjCMethodList()
2967 };
2968 if (F != SemaRef.MethodPool.end()) {
2969 Data.Instance = F->second.first;
2970 Data.Factory = F->second.second;
2971 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002972 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002973 // changed.
2974 if (Chain && I->second < FirstSelectorID) {
2975 // Selector already exists. Did it change?
2976 bool changed = false;
2977 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002978 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002979 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002980 changed = true;
2981 }
2982 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002983 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002984 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002985 changed = true;
2986 }
2987 if (!changed)
2988 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002989 } else if (Data.Instance.Method || Data.Factory.Method) {
2990 // A new method pool entry.
2991 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002992 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002993 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002994 }
2995
Douglas Gregorc78d3462009-04-24 21:10:55 +00002996 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002997 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002998 uint32_t BucketOffset;
2999 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003000 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003001 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003002 llvm::raw_svector_ostream Out(MethodPool);
3003 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003004 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003005 BucketOffset = Generator.Emit(Out, Trait);
3006 }
3007
3008 // Create a blob abbreviation
3009 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003010 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3014 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3015
Douglas Gregor95c13f52009-04-25 17:48:32 +00003016 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003017 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003018 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003019 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003020 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003021 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00003022
3023 // Create a blob abbreviation for the selector table offsets.
3024 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003025 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3029 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3030
3031 // Write the selector offsets table.
3032 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003033 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003034 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003035 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003036 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003037 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003038 }
3039}
3040
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003041/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003042void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003043 using namespace llvm;
3044 if (SemaRef.ReferencedSelectors.empty())
3045 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003046
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003047 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003048
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003049 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003050 // very tricky to fix, and given that @selector shouldn't really appear in
3051 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003052 for (DenseMap<Selector, SourceLocation>::iterator S =
3053 SemaRef.ReferencedSelectors.begin(),
3054 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3055 Selector Sel = (*S).first;
3056 SourceLocation Loc = (*S).second;
3057 AddSelectorRef(Sel, Record);
3058 AddSourceLocation(Loc, Record);
3059 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003060 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003061}
3062
Douglas Gregorc5046832009-04-27 18:38:38 +00003063//===----------------------------------------------------------------------===//
3064// Identifier Table Serialization
3065//===----------------------------------------------------------------------===//
3066
Douglas Gregorc78d3462009-04-24 21:10:55 +00003067namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003068class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003069 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00003070 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003071 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003072 bool IsModule;
3073
Douglas Gregor1d583f22009-04-28 21:18:29 +00003074 /// \brief Determines whether this is an "interesting" identifier
3075 /// that needs a full IdentifierInfo structure written into the hash
3076 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003077 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003078 if (II->isPoisoned() ||
3079 II->isExtensionToken() ||
3080 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003081 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003082 II->getFETokenInfo<void>())
3083 return true;
3084
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003085 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00003086 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003087
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003088 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003089 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003090 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003091
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003092 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3093 if (!IsModule)
3094 return !shouldIgnoreMacro(Macro, IsModule, PP);
Richard Smithdaa69e02014-07-25 04:40:03 +00003095
3096 MacroState State;
3097 if (getFirstPublicSubmoduleMacro(Macro, State))
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003098 return true;
3099 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003100
3101 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003102 }
3103
Richard Smithdaa69e02014-07-25 04:40:03 +00003104 enum class SubmoduleMacroState {
3105 /// We've seen nothing about this macro.
3106 None,
3107 /// We've seen a public visibility directive.
3108 Public,
3109 /// We've either exported a macro for this module or found that the
3110 /// module's definition of this macro is private.
3111 Done
3112 };
3113 typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
Richard Smith49f906a2014-03-01 00:08:04 +00003114
3115 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003116 getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3117 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3118 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003119 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003120 }
3121
Richard Smith49f906a2014-03-01 00:08:04 +00003122 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003123 getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
Richard Smith49f906a2014-03-01 00:08:04 +00003124 if (MacroDirective *NextMD =
Richard Smithdaa69e02014-07-25 04:40:03 +00003125 getPublicSubmoduleMacro(MD->getPrevious(), State))
3126 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003127 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003128 }
3129
Richard Smithdaa69e02014-07-25 04:40:03 +00003130 /// \brief Traverses the macro directives history and returns the next
3131 /// public macro definition or undefinition that has not been found so far.
3132 ///
Richard Smith49f906a2014-03-01 00:08:04 +00003133 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003134 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00003135 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
Richard Smithdaa69e02014-07-25 04:40:03 +00003136 MacroState &State) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003137 if (!MD)
Craig Toppera13603a2014-05-22 05:54:18 +00003138 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003139
Richard Smith49f906a2014-03-01 00:08:04 +00003140 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003141 for (; MD; MD = MD->getPrevious()) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003142 // Once we hit an ignored macro, we're done: the rest of the chain
3143 // will all be ignored macros.
3144 if (shouldIgnoreMacro(MD, IsModule, PP))
3145 break;
Richard Smith57721ac2014-07-21 04:10:40 +00003146
Richard Smithdaa69e02014-07-25 04:40:03 +00003147 // If this macro was imported, re-export it.
3148 if (MD->isImported())
3149 return MD;
Richard Smith57721ac2014-07-21 04:10:40 +00003150
Richard Smithdaa69e02014-07-25 04:40:03 +00003151 SubmoduleID ModID = getSubmoduleID(MD);
3152 auto &S = State[ModID];
3153 assert(ModID && "found macro in no submodule");
Richard Smith49f906a2014-03-01 00:08:04 +00003154
Richard Smithdaa69e02014-07-25 04:40:03 +00003155 if (S == SubmoduleMacroState::Done)
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003156 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003157
Richard Smithdaa69e02014-07-25 04:40:03 +00003158 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3159 // The latest visibility directive for a name in a submodule affects all
3160 // the directives that come before it.
3161 if (S == SubmoduleMacroState::None)
3162 S = VisMD->isPublic() ? SubmoduleMacroState::Public
3163 : SubmoduleMacroState::Done;
3164 } else {
3165 S = SubmoduleMacroState::Done;
Richard Smith49f906a2014-03-01 00:08:04 +00003166 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003167 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003168 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003169
Craig Toppera13603a2014-05-22 05:54:18 +00003170 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003171 }
3172
Richard Smithdaa69e02014-07-25 04:40:03 +00003173 ArrayRef<SubmoduleID>
3174 getOverriddenSubmodules(MacroDirective *MD,
3175 SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3176 assert(!isa<VisibilityMacroDirective>(MD) &&
3177 "only #define and #undef can override");
3178 if (MD->isImported())
3179 return MD->getOverriddenModules();
3180
3181 ScratchSpace.clear();
3182 SubmoduleID ModID = getSubmoduleID(MD);
3183 for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3184 if (shouldIgnoreMacro(MD, IsModule, PP))
3185 break;
3186
3187 // If this is a definition from a submodule import, that submodule's
3188 // definition is overridden by the definition or undefinition that we
3189 // started with.
3190 if (MD->isImported()) {
3191 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3192 SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3193 assert(DefModuleID && "imported macro has no owning module");
3194 ScratchSpace.push_back(DefModuleID);
3195 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3196 // If we override a #undef, we override anything that #undef overrides.
3197 // We don't need to override it, since an active #undef doesn't affect
3198 // the meaning of a macro.
3199 auto Overrides = UndefMD->getOverriddenModules();
3200 ScratchSpace.insert(ScratchSpace.end(),
3201 Overrides.begin(), Overrides.end());
3202 }
3203 }
3204
3205 // Stop once we leave the original macro's submodule.
3206 //
3207 // Either this submodule #included another submodule of the same
3208 // module or it just happened to be built after the other module.
3209 // In the former case, we override the submodule's macro.
3210 //
3211 // FIXME: In the latter case, we shouldn't do so, but we can't tell
3212 // these cases apart.
3213 //
3214 // FIXME: We can leave this submodule and re-enter it if it #includes a
3215 // header within a different submodule of the same module. In such cases
3216 // the overrides list will be incomplete.
3217 SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3218 if (DirectiveModuleID != ModID) {
3219 if (DirectiveModuleID && !MD->isImported())
3220 ScratchSpace.push_back(DirectiveModuleID);
3221 break;
3222 }
3223 }
3224
3225 std::sort(ScratchSpace.begin(), ScratchSpace.end());
3226 ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3227 ScratchSpace.end());
3228 return ScratchSpace;
3229 }
3230
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003231 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Richard Smith57721ac2014-07-21 04:10:40 +00003232 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003233 }
3234
Douglas Gregore84a9da2009-04-20 20:36:09 +00003235public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003236 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003237 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003238
Sebastian Redl539c5062010-08-18 23:57:32 +00003239 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003240 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003241
Justin Bogner25463f12014-04-18 20:27:24 +00003242 typedef unsigned hash_value_type;
3243 typedef unsigned offset_type;
3244
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003245 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3246 IdentifierResolver &IdResolver, bool IsModule)
3247 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003248
Justin Bogner25463f12014-04-18 20:27:24 +00003249 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003250 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003251 }
Mike Stump11289f42009-09-09 15:08:12 +00003252
3253 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003254 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003255 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003256 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Craig Toppera13603a2014-05-22 05:54:18 +00003257 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003258 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003259 DataLen += 2; // 2 bytes for builtin ID
3260 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003261 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003262 DataLen += 4; // MacroDirectives offset.
3263 if (IsModule) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003264 MacroState State;
3265 SmallVector<SubmoduleID, 16> Scratch;
3266 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3267 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003268 DataLen += 4; // MacroInfo ID or ModuleID.
Richard Smithdaa69e02014-07-25 04:40:03 +00003269 if (unsigned NumOverrides =
3270 getOverriddenSubmodules(MD, Scratch).size())
3271 DataLen += 4 * (1 + NumOverrides);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003272 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003273 DataLen += 4; // 0 terminator.
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003274 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003275 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003276
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003277 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3278 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003279 D != DEnd; ++D)
Richard Smithdaa69e02014-07-25 04:40:03 +00003280 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003281 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003282 using namespace llvm::support;
3283 endian::Writer<little> LE(Out);
3284
3285 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003286 // We emit the key length after the data length so that every
3287 // string is preceded by a 16-bit length. This matches the PTH
3288 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003289 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003290 return std::make_pair(KeyLen, DataLen);
3291 }
Mike Stump11289f42009-09-09 15:08:12 +00003292
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003293 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003294 unsigned KeyLen) {
3295 // Record the location of the key data. This is used when generating
3296 // the mapping from persistent IDs to strings.
3297 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003298 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003299 }
Mike Stump11289f42009-09-09 15:08:12 +00003300
Richard Smith49f906a2014-03-01 00:08:04 +00003301 static void emitMacroOverrides(raw_ostream &Out,
Craig Topper00bbdcf2014-06-28 23:22:23 +00003302 ArrayRef<SubmoduleID> Overridden) {
Richard Smith49f906a2014-03-01 00:08:04 +00003303 if (!Overridden.empty()) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003304 using namespace llvm::support;
3305 endian::Writer<little> LE(Out);
3306 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Richard Smithdaa69e02014-07-25 04:40:03 +00003307 for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3308 assert(Overridden[I] && "zero module ID for override");
Justin Bognere1c147c2014-03-28 22:03:19 +00003309 LE.write<uint32_t>(Overridden[I]);
Richard Smithdaa69e02014-07-25 04:40:03 +00003310 }
Richard Smith49f906a2014-03-01 00:08:04 +00003311 }
3312 }
3313
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003314 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003315 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003316 using namespace llvm::support;
3317 endian::Writer<little> LE(Out);
Craig Toppera13603a2014-05-22 05:54:18 +00003318 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003319 if (!isInterestingIdentifier(II, Macro)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003320 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003321 return;
3322 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003323
Justin Bognere1c147c2014-03-28 22:03:19 +00003324 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003325 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3326 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003327 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003328 Bits = 0;
3329 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003330 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003331 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003332 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3333 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003334 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003335 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003336 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003337
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003338 if (HadMacroDefinition) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003339 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003340 if (IsModule) {
3341 // Write the IDs of macros coming from different submodules.
Richard Smithdaa69e02014-07-25 04:40:03 +00003342 MacroState State;
3343 SmallVector<SubmoduleID, 16> Scratch;
3344 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3345 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003346 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003347 // FIXME: If this macro directive was created by #pragma pop_macros,
3348 // or if it was created implicitly by resolving conflicting macros,
3349 // it may be for a different submodule from the one in the MacroInfo
3350 // object. If so, we should write out its owning ModuleID.
3351 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Richard Smith49f906a2014-03-01 00:08:04 +00003352 assert(InfoID);
Justin Bognere1c147c2014-03-28 22:03:19 +00003353 LE.write<uint32_t>(InfoID << 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003354 } else {
Richard Smithdaa69e02014-07-25 04:40:03 +00003355 auto *UndefMD = cast<UndefMacroDirective>(MD);
3356 SubmoduleID Mod = UndefMD->isImported()
3357 ? UndefMD->getOwningModuleID()
3358 : getSubmoduleID(UndefMD);
3359 LE.write<uint32_t>((Mod << 1) | 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003360 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003361 emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003362 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003363 LE.write<uint32_t>(0xdeadbeef);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003364 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003365 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003366
Douglas Gregora868bbd2009-04-21 22:25:48 +00003367 // Emit the declaration IDs in reverse order, because the
3368 // IdentifierResolver provides the declarations as they would be
3369 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003370 // "stat"), but the ASTReader adds declarations to the end of the list
3371 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003372 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003373 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3374 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003375 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003376 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003377 D != DEnd; ++D)
Justin Bognere1c147c2014-03-28 22:03:19 +00003378 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003379 }
3380
3381 /// \brief Returns the most recent local decl or the given decl if there are
3382 /// no local ones. The given decl is assumed to be the most recent one.
3383 Decl *getMostRecentLocalDecl(Decl *Orig) {
3384 // The only way a "from AST file" decl would be more recent from a local one
3385 // is if it came from a module.
3386 if (!PP.getLangOpts().Modules)
3387 return Orig;
3388
3389 // Look for a local in the decl chain.
3390 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3391 if (!D->isFromASTFile())
3392 return D;
3393 // If we come up a decl from a (chained-)PCH stop since we won't find a
3394 // local one.
3395 if (D->getOwningModuleID() == 0)
3396 break;
3397 }
3398
3399 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003400 }
3401};
3402} // end anonymous namespace
3403
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003404/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003405///
3406/// The identifier table consists of a blob containing string data
3407/// (the actual identifiers themselves) and a separate "offsets" index
3408/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003409void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3410 IdentifierResolver &IdResolver,
3411 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003412 using namespace llvm;
3413
3414 // Create and write out the blob that contains the identifier
3415 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003416 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003417 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003418 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003419
Douglas Gregore6648fb2009-04-28 20:33:11 +00003420 // Look for any identifiers that were named while processing the
3421 // headers, but are otherwise not needed. We add these to the hash
3422 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003423 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003424 // file.
3425 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3426 IDEnd = PP.getIdentifierTable().end();
3427 ID != IDEnd; ++ID)
3428 getIdentifierRef(ID->second);
3429
Sebastian Redlff4a2952010-07-23 23:49:55 +00003430 // Create the on-disk hash table representation. We only store offsets
3431 // for identifiers that appear here for the first time.
3432 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003433 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003434 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3435 ID != IDEnd; ++ID) {
3436 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003437 if (!Chain || !ID->first->isFromAST() ||
3438 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003439 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003440 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003441 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003442
Douglas Gregore84a9da2009-04-20 20:36:09 +00003443 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003444 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003445 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003446 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003447 using namespace llvm::support;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003448 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003449 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003450 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003451 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003452 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003453 }
3454
3455 // Create a blob abbreviation
3456 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003457 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003460 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003461
3462 // Write the identifier table
3463 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003464 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003465 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003466 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003467 }
3468
3469 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003470 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003471 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003473 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003474 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3475 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3476
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003477#ifndef NDEBUG
3478 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3479 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3480#endif
3481
Douglas Gregor0e149972009-04-25 19:10:14 +00003482 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003483 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003484 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003485 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003486 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003487 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003488}
3489
Douglas Gregorc5046832009-04-27 18:38:38 +00003490//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003491// DeclContext's Name Lookup Table Serialization
3492//===----------------------------------------------------------------------===//
3493
Richard Smithbb853c72014-08-13 01:23:33 +00003494/// Determine the declaration that should be put into the name lookup table to
3495/// represent the given declaration in this module. This is usually D itself,
3496/// but if D was imported and merged into a local declaration, we want the most
3497/// recent local declaration instead. The chosen declaration will be the most
3498/// recent declaration in any module that imports this one.
3499static NamedDecl *getDeclForLocalLookup(NamedDecl *D) {
Richard Smith8c913ec2014-08-14 02:21:01 +00003500 if (!D->isFromASTFile())
3501 return D;
3502
3503 if (Decl *Redecl = D->getPreviousDecl()) {
3504 // For Redeclarable decls, a prior declaration might be local.
3505 for (; Redecl; Redecl = Redecl->getPreviousDecl())
3506 if (!Redecl->isFromASTFile())
3507 return cast<NamedDecl>(Redecl);
3508 } else if (Decl *First = D->getCanonicalDecl()) {
3509 // For Mergeable decls, the first decl might be local.
3510 if (!First->isFromASTFile())
3511 return cast<NamedDecl>(First);
3512 }
3513
3514 // All declarations are imported. Our most recent declaration will also be
3515 // the most recent one in anyone who imports us.
Richard Smithbb853c72014-08-13 01:23:33 +00003516 return D;
3517}
3518
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003519namespace {
3520// Trait used for the on-disk hash table used in the method pool.
3521class ASTDeclContextNameLookupTrait {
3522 ASTWriter &Writer;
3523
3524public:
3525 typedef DeclarationName key_type;
3526 typedef key_type key_type_ref;
3527
3528 typedef DeclContext::lookup_result data_type;
3529 typedef const data_type& data_type_ref;
3530
Justin Bogner25463f12014-04-18 20:27:24 +00003531 typedef unsigned hash_value_type;
3532 typedef unsigned offset_type;
3533
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003534 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3535
Justin Bogner25463f12014-04-18 20:27:24 +00003536 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003537 llvm::FoldingSetNodeID ID;
3538 ID.AddInteger(Name.getNameKind());
3539
3540 switch (Name.getNameKind()) {
3541 case DeclarationName::Identifier:
3542 ID.AddString(Name.getAsIdentifierInfo()->getName());
3543 break;
3544 case DeclarationName::ObjCZeroArgSelector:
3545 case DeclarationName::ObjCOneArgSelector:
3546 case DeclarationName::ObjCMultiArgSelector:
3547 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3548 break;
3549 case DeclarationName::CXXConstructorName:
3550 case DeclarationName::CXXDestructorName:
3551 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003552 break;
3553 case DeclarationName::CXXOperatorName:
3554 ID.AddInteger(Name.getCXXOverloadedOperator());
3555 break;
3556 case DeclarationName::CXXLiteralOperatorName:
3557 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3558 case DeclarationName::CXXUsingDirective:
3559 break;
3560 }
3561
3562 return ID.ComputeHash();
3563 }
3564
3565 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003566 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003567 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003568 using namespace llvm::support;
3569 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003570 unsigned KeyLen = 1;
3571 switch (Name.getNameKind()) {
3572 case DeclarationName::Identifier:
3573 case DeclarationName::ObjCZeroArgSelector:
3574 case DeclarationName::ObjCOneArgSelector:
3575 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003576 case DeclarationName::CXXLiteralOperatorName:
3577 KeyLen += 4;
3578 break;
3579 case DeclarationName::CXXOperatorName:
3580 KeyLen += 1;
3581 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003582 case DeclarationName::CXXConstructorName:
3583 case DeclarationName::CXXDestructorName:
3584 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003585 case DeclarationName::CXXUsingDirective:
3586 break;
3587 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003588 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003589
3590 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003591 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003592 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003593
3594 return std::make_pair(KeyLen, DataLen);
3595 }
3596
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003597 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003598 using namespace llvm::support;
3599 endian::Writer<little> LE(Out);
3600 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003601 switch (Name.getNameKind()) {
3602 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003603 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003604 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003605 case DeclarationName::ObjCZeroArgSelector:
3606 case DeclarationName::ObjCOneArgSelector:
3607 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003608 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003609 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003610 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003611 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3612 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003613 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003614 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003615 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003616 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003617 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003618 case DeclarationName::CXXConstructorName:
3619 case DeclarationName::CXXDestructorName:
3620 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003621 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003622 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003623 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003624
3625 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003626 }
3627
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003628 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003629 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003630 using namespace llvm::support;
3631 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003632 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003633 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003634 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3635 I != E; ++I)
Richard Smithbb853c72014-08-13 01:23:33 +00003636 LE.write<uint32_t>(Writer.GetDeclRef(getDeclForLocalLookup(*I)));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003637
3638 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3639 }
3640};
3641} // end anonymous namespace
3642
Richard Smithcd45dbc2014-04-19 03:48:30 +00003643template<typename Visitor>
3644static void visitLocalLookupResults(const DeclContext *ConstDC,
3645 bool NeedToReconcileExternalVisibleStorage,
3646 Visitor AddLookupResult) {
3647 // FIXME: We need to build the lookups table, which is logically const.
3648 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Richard Smith961eae52014-03-25 01:14:22 +00003649 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3650
Richard Smith961eae52014-03-25 01:14:22 +00003651 SmallVector<DeclarationName, 16> ExternalNames;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003652 for (auto &Lookup : *DC->buildLookup()) {
Richard Smith961eae52014-03-25 01:14:22 +00003653 if (Lookup.second.hasExternalDecls() ||
Richard Smithcd45dbc2014-04-19 03:48:30 +00003654 NeedToReconcileExternalVisibleStorage) {
Richard Smith961eae52014-03-25 01:14:22 +00003655 // We don't know for sure what declarations are found by this name,
3656 // because the external source might have a different set from the set
3657 // that are in the lookup map, and we can't update it now without
3658 // risking invalidating our lookup iterator. So add it to a queue to
3659 // deal with later.
3660 ExternalNames.push_back(Lookup.first);
3661 continue;
3662 }
3663
3664 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3665 }
3666
3667 // Add the names we needed to defer. Note, this shouldn't add any new decls
3668 // to the list we need to serialize: any new declarations we find here should
3669 // be imported from an external source.
3670 // FIXME: What if the external source isn't an ASTReader?
3671 for (const auto &Name : ExternalNames)
Richard Smithcd45dbc2014-04-19 03:48:30 +00003672 AddLookupResult(Name, DC->lookup(Name));
3673}
3674
3675void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
3676 if (UpdatedDeclContexts.insert(DC) && WritingAST) {
3677 // Ensure we emit all the visible declarations.
3678 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3679 [&](DeclarationName Name,
3680 DeclContext::lookup_const_result Result) {
3681 for (auto *Decl : Result)
Richard Smithbb853c72014-08-13 01:23:33 +00003682 GetDeclRef(getDeclForLocalLookup(Decl));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003683 });
3684 }
3685}
3686
3687uint32_t
3688ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3689 llvm::SmallVectorImpl<char> &LookupTable) {
3690 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3691
3692 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3693 Generator;
3694 ASTDeclContextNameLookupTrait Trait(*this);
3695
3696 // Create the on-disk hash table representation.
3697 DeclarationName ConstructorName;
3698 DeclarationName ConversionName;
3699 SmallVector<NamedDecl *, 8> ConstructorDecls;
3700 SmallVector<NamedDecl *, 4> ConversionDecls;
3701
3702 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3703 [&](DeclarationName Name,
3704 DeclContext::lookup_result Result) {
3705 if (Result.empty())
3706 return;
3707
3708 // Different DeclarationName values of certain kinds are mapped to
3709 // identical serialized keys, because we don't want to use type
3710 // identifiers in the keys (since type ids are local to the module).
3711 switch (Name.getNameKind()) {
3712 case DeclarationName::CXXConstructorName:
3713 // There may be different CXXConstructorName DeclarationName values
3714 // in a DeclContext because a UsingDecl that inherits constructors
3715 // has the DeclarationName of the inherited constructors.
3716 if (!ConstructorName)
3717 ConstructorName = Name;
3718 ConstructorDecls.append(Result.begin(), Result.end());
3719 return;
3720
3721 case DeclarationName::CXXConversionFunctionName:
3722 if (!ConversionName)
3723 ConversionName = Name;
3724 ConversionDecls.append(Result.begin(), Result.end());
3725 return;
3726
3727 default:
3728 break;
3729 }
3730
3731 Generator.insert(Name, Result, Trait);
3732 });
Richard Smith961eae52014-03-25 01:14:22 +00003733
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003734 // Add the constructors.
3735 if (!ConstructorDecls.empty()) {
3736 Generator.insert(ConstructorName,
3737 DeclContext::lookup_result(ConstructorDecls.begin(),
3738 ConstructorDecls.end()),
3739 Trait);
3740 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003741
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003742 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003743 if (!ConversionDecls.empty()) {
3744 Generator.insert(ConversionName,
3745 DeclContext::lookup_result(ConversionDecls.begin(),
3746 ConversionDecls.end()),
3747 Trait);
3748 }
3749
3750 // Create the on-disk hash table in a buffer.
3751 llvm::raw_svector_ostream Out(LookupTable);
3752 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003753 using namespace llvm::support;
3754 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003755 return Generator.Emit(Out, Trait);
3756}
3757
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003758/// \brief Write the block containing all of the declaration IDs
3759/// visible from the given DeclContext.
3760///
3761/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003762/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003763uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3764 DeclContext *DC) {
3765 if (DC->getPrimaryContext() != DC)
3766 return 0;
3767
3768 // Since there is no name lookup into functions or methods, don't bother to
3769 // build a visible-declarations table for these entities.
3770 if (DC->isFunctionOrMethod())
3771 return 0;
3772
3773 // If not in C++, we perform name lookup for the translation unit via the
3774 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003775 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003776 return 0;
3777
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003778 // Serialize the contents of the mapping used for lookup. Note that,
3779 // although we have two very different code paths, the serialized
3780 // representation is the same for both cases: a declaration name,
3781 // followed by a size, followed by references to the visible
3782 // declarations that have that name.
3783 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003784 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003785 if (!Map || Map->empty())
3786 return 0;
3787
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003788 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003789 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003790 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003791
3792 // Write the lookup table
3793 RecordData Record;
3794 Record.push_back(DECL_CONTEXT_VISIBLE);
3795 Record.push_back(BucketOffset);
3796 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3797 LookupTable.str());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003798 ++NumVisibleDeclContexts;
3799 return Offset;
3800}
3801
Sebastian Redla4071b42010-08-24 00:50:09 +00003802/// \brief Write an UPDATE_VISIBLE block for the given context.
3803///
3804/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3805/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003806/// (in C++), for namespaces, and for classes with forward-declared unscoped
3807/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003808void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003809 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003810 if (!Map || Map->empty())
3811 return;
3812
Sebastian Redla4071b42010-08-24 00:50:09 +00003813 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003814 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003815 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003816
3817 // Write the lookup table
3818 RecordData Record;
3819 Record.push_back(UPDATE_VISIBLE);
3820 Record.push_back(getDeclID(cast<Decl>(DC)));
3821 Record.push_back(BucketOffset);
3822 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3823}
3824
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003825/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3826void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3827 RecordData Record;
3828 Record.push_back(Opts.fp_contract);
3829 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3830}
3831
3832/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3833void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003834 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003835 return;
3836
3837 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3838 RecordData Record;
3839#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3840#include "clang/Basic/OpenCLExtensions.def"
3841 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3842}
3843
Douglas Gregor358cd442012-01-15 16:58:34 +00003844void ASTWriter::WriteRedeclarations() {
3845 RecordData LocalRedeclChains;
3846 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3847
3848 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3849 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003850 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003851
3852 Decl *MostRecent = First->getMostRecentDecl();
3853
3854 // If we only have a single declaration, there is no point in storing
3855 // a redeclaration chain.
3856 if (First == MostRecent)
3857 continue;
3858
3859 unsigned Offset = LocalRedeclChains.size();
3860 unsigned Size = 0;
3861 LocalRedeclChains.push_back(0); // Placeholder for the size.
3862
3863 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003864 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003865 Prev = Prev->getPreviousDecl()) {
3866 if (!Prev->isFromASTFile()) {
3867 AddDeclRef(Prev, LocalRedeclChains);
3868 ++Size;
3869 }
3870 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003871
3872 if (!First->isFromASTFile() && Chain) {
3873 Decl *FirstFromAST = MostRecent;
3874 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3875 if (Prev->isFromASTFile())
3876 FirstFromAST = Prev;
3877 }
3878
Richard Smith2516ba22014-08-11 18:35:44 +00003879 // FIXME: Do we need to do this for the first declaration from each
3880 // redeclaration chain that was merged into this one?
Douglas Gregor6168bd22013-02-18 15:53:43 +00003881 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3882 }
3883
Douglas Gregor358cd442012-01-15 16:58:34 +00003884 LocalRedeclChains[Offset] = Size;
3885
3886 // Reverse the set of local redeclarations, so that we store them in
3887 // order (since we found them in reverse order).
3888 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3889
Douglas Gregor6168bd22013-02-18 15:53:43 +00003890 // Add the mapping from the first ID from the AST to the set of local
3891 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003892 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3893 LocalRedeclsMap.push_back(Info);
3894
3895 assert(N == Redeclarations.size() &&
3896 "Deserialized a declaration we shouldn't have");
3897 }
3898
3899 if (LocalRedeclChains.empty())
3900 return;
3901
3902 // Sort the local redeclarations map by the first declaration ID,
3903 // since the reader will be performing binary searches on this information.
3904 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3905
3906 // Emit the local redeclarations map.
3907 using namespace llvm;
3908 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3909 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3912 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3913
3914 RecordData Record;
3915 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3916 Record.push_back(LocalRedeclsMap.size());
3917 Stream.EmitRecordWithBlob(AbbrevID, Record,
3918 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3919 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3920
3921 // Emit the redeclaration chains.
3922 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3923}
3924
Douglas Gregor404cdde2012-01-27 01:47:08 +00003925void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003926 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003927 RecordData Categories;
3928
3929 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3930 unsigned Size = 0;
3931 unsigned StartIndex = Categories.size();
3932
3933 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3934
3935 // Allocate space for the size.
3936 Categories.push_back(0);
3937
3938 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003939 for (ObjCInterfaceDecl::known_categories_iterator
3940 Cat = Class->known_categories_begin(),
3941 CatEnd = Class->known_categories_end();
3942 Cat != CatEnd; ++Cat, ++Size) {
3943 assert(getDeclID(*Cat) != 0 && "Bogus category");
3944 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003945 }
3946
3947 // Update the size.
3948 Categories[StartIndex] = Size;
3949
3950 // Record this interface -> category map.
3951 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3952 CategoriesMap.push_back(CatInfo);
3953 }
3954
3955 // Sort the categories map by the definition ID, since the reader will be
3956 // performing binary searches on this information.
3957 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3958
3959 // Emit the categories map.
3960 using namespace llvm;
3961 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3962 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3964 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3965 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3966
3967 RecordData Record;
3968 Record.push_back(OBJC_CATEGORIES_MAP);
3969 Record.push_back(CategoriesMap.size());
3970 Stream.EmitRecordWithBlob(AbbrevID, Record,
3971 reinterpret_cast<char*>(CategoriesMap.data()),
3972 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3973
3974 // Emit the category lists.
3975 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3976}
3977
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003978void ASTWriter::WriteMergedDecls() {
3979 if (!Chain || Chain->MergedDecls.empty())
3980 return;
3981
3982 RecordData Record;
3983 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3984 IEnd = Chain->MergedDecls.end();
3985 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003986 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Richard Smithcd45dbc2014-04-19 03:48:30 +00003987 : GetDeclRef(I->first);
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003988 assert(CanonID && "Merged declaration not known?");
3989
3990 Record.push_back(CanonID);
3991 Record.push_back(I->second.size());
3992 Record.append(I->second.begin(), I->second.end());
3993 }
3994 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3995}
3996
Richard Smithe40f2ba2013-08-07 21:41:30 +00003997void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3998 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3999
4000 if (LPTMap.empty())
4001 return;
4002
4003 RecordData Record;
4004 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4005 ItEnd = LPTMap.end();
4006 It != ItEnd; ++It) {
4007 LateParsedTemplate *LPT = It->second;
4008 AddDeclRef(It->first, Record);
4009 AddDeclRef(LPT->D, Record);
4010 Record.push_back(LPT->Toks.size());
4011
4012 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4013 TokEnd = LPT->Toks.end();
4014 TokIt != TokEnd; ++TokIt) {
4015 AddToken(*TokIt, Record);
4016 }
4017 }
4018 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4019}
4020
Dario Domizioli13a0a382014-05-23 12:13:25 +00004021/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4022void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4023 RecordData Record;
4024 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4025 AddSourceLocation(PragmaLoc, Record);
4026 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4027}
4028
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004029//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004030// General Serialization Routines
4031//===----------------------------------------------------------------------===//
4032
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004033/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004034void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4035 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004036 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004037 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4038 e = Attrs.end(); i != e; ++i){
4039 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004040 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004041 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004042
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004043#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004044
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004045 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004046}
4047
John McCallf413f5e2013-05-03 00:10:13 +00004048void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4049 AddSourceLocation(Tok.getLocation(), Record);
4050 Record.push_back(Tok.getLength());
4051
4052 // FIXME: When reading literal tokens, reconstruct the literal pointer
4053 // if it is needed.
4054 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4055 // FIXME: Should translate token kind to a stable encoding.
4056 Record.push_back(Tok.getKind());
4057 // FIXME: Should translate token flags to a stable encoding.
4058 Record.push_back(Tok.getFlags());
4059}
4060
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004061void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004062 Record.push_back(Str.size());
4063 Record.insert(Record.end(), Str.begin(), Str.end());
4064}
4065
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004066void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4067 RecordDataImpl &Record) {
4068 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004069 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004070 Record.push_back(*Minor + 1);
4071 else
4072 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004073 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004074 Record.push_back(*Subminor + 1);
4075 else
4076 Record.push_back(0);
4077}
4078
Douglas Gregore84a9da2009-04-20 20:36:09 +00004079/// \brief Note that the identifier II occurs at the given offset
4080/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004081void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004082 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004083 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004084 // up earlier in the chain and thus don't need an offset.
4085 if (ID >= FirstIdentID)
4086 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004087}
4088
Douglas Gregor95c13f52009-04-25 17:48:32 +00004089/// \brief Note that the selector Sel occurs at the given offset
4090/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004091void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004092 unsigned ID = SelectorIDs[Sel];
4093 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004094 // Don't record offsets for selectors that are also available in a different
4095 // file.
4096 if (ID < FirstSelectorID)
4097 return;
4098 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004099}
4100
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004101ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Richard Smith01b2cb42014-07-26 06:37:51 +00004102 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4103 WritingModule(nullptr), WritingAST(false),
4104 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4105 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4106 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4107 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4108 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4109 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4110 NextSubmoduleID(FirstSubmoduleID),
4111 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4112 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4113 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4114 NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4115 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4116 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004117 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004118 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004119 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4120 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4121 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004122
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004123ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004124 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004125}
4126
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004127void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004128 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004129 Module *WritingModule, StringRef isysroot,
4130 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004131 WritingAST = true;
4132
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004133 ASTHasCompilerErrors = hasErrors;
4134
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004135 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004136 Stream.Emit((unsigned)'C', 8);
4137 Stream.Emit((unsigned)'P', 8);
4138 Stream.Emit((unsigned)'C', 8);
4139 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004140
Chris Lattner28fa4e62009-04-26 22:26:21 +00004141 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004142
Douglas Gregoreda8e122011-08-09 15:13:55 +00004143 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004144 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004145 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004146 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004147 Context = nullptr;
4148 PP = nullptr;
4149 this->WritingModule = nullptr;
4150
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004151 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004152}
4153
Douglas Gregora94a1542011-07-27 21:45:57 +00004154template<typename Vector>
4155static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4156 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004157 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4158 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004159 Writer.AddDeclRef(*I, Record);
4160 }
4161}
4162
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004163void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004164 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004165 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004166 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004167 using namespace llvm;
4168
Craig Toppera13603a2014-05-22 05:54:18 +00004169 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004170
Douglas Gregorcf68c582011-12-01 22:20:10 +00004171 // Make sure that the AST reader knows to finalize itself.
4172 if (Chain)
4173 Chain->finalizeForWriting();
4174
Sebastian Redl143413f2010-07-12 22:02:52 +00004175 ASTContext &Context = SemaRef.Context;
4176 Preprocessor &PP = SemaRef.PP;
4177
Douglas Gregordab42432011-08-12 00:15:20 +00004178 // Set up predefined declaration IDs.
4179 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00004180 if (Context.ObjCIdDecl)
4181 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00004182 if (Context.ObjCSelDecl)
4183 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00004184 if (Context.ObjCClassDecl)
4185 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00004186 if (Context.ObjCProtocolClassDecl)
4187 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00004188 if (Context.Int128Decl)
4189 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4190 if (Context.UInt128Decl)
4191 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00004192 if (Context.ObjCInstanceTypeDecl)
4193 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004194 if (Context.BuiltinVaListDecl)
4195 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4196
Douglas Gregor851443c2011-08-12 01:39:19 +00004197 if (!Chain) {
4198 // Make sure that we emit IdentifierInfos (and any attached
4199 // declarations) for builtins. We don't need to do this when we're
4200 // emitting chained PCH files, because all of the builtins will be
4201 // in the original PCH file.
4202 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004203 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004204 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00004205 if (!Context.getLangOpts().NoBuiltin) {
4206 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4207 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004208 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4209 getIdentifierRef(&Table.get(BuiltinNames[I]));
4210 }
4211
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004212 // If there are any out-of-date identifiers, bring them up to date.
4213 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00004214 // Find out-of-date identifiers.
4215 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004216 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4217 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00004218 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004219 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00004220 OutOfDate.push_back(ID->second);
4221 }
4222
4223 // Update the out-of-date identifiers.
4224 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4225 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4226 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004227 }
4228
Richard Smithcd45dbc2014-04-19 03:48:30 +00004229 // If we saw any DeclContext updates before we started writing the AST file,
4230 // make sure all visible decls in those DeclContexts are written out.
4231 if (!UpdatedDeclContexts.empty()) {
4232 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4233 UpdatedDeclContexts.clear();
4234 for (auto *DC : OldUpdatedDeclContexts)
4235 AddUpdatedDeclContext(DC);
4236 }
4237
Chris Lattner0c797362009-09-08 18:19:27 +00004238 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004239 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004240 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004241 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004242 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004243
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004244 // Build a record containing all of the file scoped decls in this file.
4245 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004246 if (!isModule)
4247 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4248 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004249
Douglas Gregor851443c2011-08-12 01:39:19 +00004250 // Build a record containing all of the delegating constructors we still need
4251 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004252 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004253 if (!isModule)
4254 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004255
Douglas Gregor851443c2011-08-12 01:39:19 +00004256 // Write the set of weak, undeclared identifiers. We always write the
4257 // entire table, since later PCH files in a PCH chain are only interested in
4258 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004259 RecordData WeakUndeclaredIdentifiers;
4260 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004261 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004262 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4263 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4264 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4265 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4266 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4267 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4268 }
4269 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004270
Richard Smith78165b52013-01-10 23:43:47 +00004271 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004272 // declarations in this header file. Generally, this record will be
4273 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00004274 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004275 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00004276 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00004277 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00004278 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4279 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00004280 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004281 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00004282 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00004283 }
4284
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004285 // Build a record containing all of the ext_vector declarations.
4286 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004287 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004288
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004289 // Build a record containing all of the VTable uses information.
4290 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004291 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004292 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4293 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4294 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4295 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4296 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004297 }
4298
Nico Weber72889432014-09-06 01:25:55 +00004299 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4300 RecordData UnusedLocalTypedefNameCandidates;
4301 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4302 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4303
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004304 // Build a record containing all of dynamic classes declarations.
4305 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004306 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004307
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004308 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004309 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004310 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004311 I = SemaRef.PendingInstantiations.begin(),
4312 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4313 AddDeclRef(I->first, PendingInstantiations);
4314 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004315 }
4316 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4317 "There are local ones at end of translation unit!");
4318
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004319 // Build a record containing some declaration references.
4320 RecordData SemaDeclRefs;
4321 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4322 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4323 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4324 }
4325
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004326 RecordData CUDASpecialDeclRefs;
4327 if (Context.getcudaConfigureCallDecl()) {
4328 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4329 }
4330
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004331 // Build a record containing all of the known namespaces.
4332 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004333 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004334 I = SemaRef.KnownNamespaces.begin(),
4335 IEnd = SemaRef.KnownNamespaces.end();
4336 I != IEnd; ++I) {
4337 if (!I->second)
4338 AddDeclRef(I->first, KnownNamespaces);
4339 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004340
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004341 // Build a record of all used, undefined objects that require definitions.
4342 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004343
4344 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004345 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004346 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4347 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004348 AddDeclRef(I->first, UndefinedButUsed);
4349 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004350 }
4351
Douglas Gregor112b9072012-10-18 05:31:06 +00004352 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004353 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004354
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004355 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004356 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004357 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004358
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004359 // This is so that older clang versions, before the introduction
4360 // of the control block, can read and reject the newer PCH format.
4361 Record.clear();
4362 Record.push_back(VERSION_MAJOR);
4363 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4364
Douglas Gregor851443c2011-08-12 01:39:19 +00004365 // Create a lexical update block containing all of the declarations in the
4366 // translation unit that do not come from other AST files.
4367 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4368 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004369 for (const auto *I : TU->noload_decls()) {
4370 if (!I->isFromASTFile())
4371 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004372 }
4373
4374 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4375 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4376 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4377 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4378 Record.clear();
4379 Record.push_back(TU_UPDATE_LEXICAL);
4380 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4381 data(NewGlobalDecls));
4382
4383 // And a visible updates block for the translation unit.
4384 Abv = new llvm::BitCodeAbbrev();
4385 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4386 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4387 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4388 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4389 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4390 WriteDeclContextVisibleUpdate(TU);
4391
4392 // If the translation unit has an anonymous namespace, and we don't already
4393 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004394 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004395 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4396 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004397 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004398 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004399 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004400
Richard Smith5652c0f2014-03-21 01:48:23 +00004401 // Add update records for all mangling numbers and static local numbers.
4402 // These aren't really update records, but this is a convenient way of
4403 // tagging this rare extra data onto the declarations.
4404 for (const auto &Number : Context.MangleNumbers)
4405 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004406 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4407 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004408 for (const auto &Number : Context.StaticLocalNumbers)
4409 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004410 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4411 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004412
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004413 // Make sure visible decls, added to DeclContexts previously loaded from
4414 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004415 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004416 I = UpdatingVisibleDecls.begin(),
4417 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4418 GetDeclRef(*I);
4419 }
4420
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004421 // Make sure all decls associated with an identifier are registered for
4422 // serialization.
4423 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4424 IDEnd = PP.getIdentifierTable().end();
4425 ID != IDEnd; ++ID) {
4426 const IdentifierInfo *II = ID->second;
4427 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4428 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4429 DEnd = SemaRef.IdResolver.end();
4430 D != DEnd; ++D) {
4431 GetDeclRef(*D);
4432 }
4433 }
4434 }
4435
Douglas Gregor5204bde2011-08-02 16:26:37 +00004436 // Form the record of special types.
4437 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004438 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004439 AddTypeRef(Context.getFILEType(), SpecialTypes);
4440 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4441 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4442 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4443 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004444 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004445 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004446
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004447 if (Chain) {
4448 // Write the mapping information describing our module dependencies and how
4449 // each of those modules were mapped into our own offset/ID space, so that
4450 // the reader can build the appropriate mapping to its own offset/ID space.
4451 // The map consists solely of a blob with the following format:
4452 // *(module-name-len:i16 module-name:len*i8
4453 // source-location-offset:i32
4454 // identifier-id:i32
4455 // preprocessed-entity-id:i32
4456 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004457 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004458 // selector-id:i32
4459 // declaration-id:i32
4460 // c++-base-specifiers-id:i32
4461 // type-id:i32)
4462 //
4463 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4464 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4466 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004467 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004468 {
4469 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004470 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004471 using namespace llvm::support;
4472 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004473 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004474 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004475 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004476
Ben Langmuir785180e2014-10-20 16:27:30 +00004477 // Note: if a base ID was uint max, it would not be possible to load
4478 // another module after it or have more than one entity inside it.
4479 uint32_t None = std::numeric_limits<uint32_t>::max();
4480
4481 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4482 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4483 if (ShouldWrite)
4484 LE.write<uint32_t>(BaseID);
4485 else
4486 LE.write<uint32_t>(None);
4487 };
4488
Ben Langmuirfe971d92014-08-16 04:54:18 +00004489 // These values should be unique within a chain, since they will be read
4490 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004491 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4492 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4493 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4494 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4495 M->NumPreprocessedEntities);
4496 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4497 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4498 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4499 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004500 }
4501 }
4502 Record.clear();
4503 Record.push_back(MODULE_OFFSET_MAP);
4504 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4505 Buffer.data(), Buffer.size());
4506 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004507
Richard Smithb9eab6d2014-03-20 19:44:17 +00004508 RecordData DeclUpdatesOffsetsRecord;
4509
Richard Smith59442b42014-03-20 20:07:19 +00004510 // Keep writing types, declarations, and declaration update records
4511 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004512 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4513 WriteTypeAbbrevs();
4514 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004515 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4516 E = DeclsToRewrite.end();
4517 I != E; ++I)
4518 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004519 do {
4520 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4521 while (!DeclTypesToEmit.empty()) {
4522 DeclOrType DOT = DeclTypesToEmit.front();
4523 DeclTypesToEmit.pop();
4524 if (DOT.isType())
4525 WriteType(DOT.getType());
4526 else
4527 WriteDecl(Context, DOT.getDecl());
4528 }
4529 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004530 Stream.ExitBlock();
4531
Richard Smithb9eab6d2014-03-20 19:44:17 +00004532 DoneWritingDeclsAndTypes = true;
4533
4534 // These things can only be done once we've written out decls and types.
4535 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004536 if (!DeclUpdatesOffsetsRecord.empty())
4537 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004538 WriteCXXBaseSpecifiersOffsets();
4539 WriteFileDeclIDsMap();
4540 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4541
4542 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004543 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004544 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004545 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004546 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004547 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004548 WriteFPPragmaOptions(SemaRef.getFPOptions());
4549 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004550 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004551
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004552 // If we're emitting a module, write out the submodule information.
4553 if (WritingModule)
4554 WriteSubmodules(WritingModule);
4555
Douglas Gregor5204bde2011-08-02 16:26:37 +00004556 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4557
Douglas Gregord4df8652009-04-22 22:02:47 +00004558 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004559 if (!EagerlyDeserializedDecls.empty())
4560 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004561
4562 // Write the record containing tentative definitions.
4563 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004564 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004565
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004566 // Write the record containing unused file scoped decls.
4567 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004568 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004569
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004570 // Write the record containing weak undeclared identifiers.
4571 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004572 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004573 WeakUndeclaredIdentifiers);
4574
Richard Smith78165b52013-01-10 23:43:47 +00004575 // Write the record containing locally-scoped extern "C" definitions.
4576 if (!LocallyScopedExternCDecls.empty())
4577 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4578 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004579
4580 // Write the record containing ext_vector type names.
4581 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004582 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004583
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004584 // Write the record containing VTable uses information.
4585 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004586 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004587
4588 // Write the record containing dynamic classes declarations.
4589 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004590 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004591
Nico Weber72889432014-09-06 01:25:55 +00004592 // Write the record containing potentially unused local typedefs.
4593 if (!UnusedLocalTypedefNameCandidates.empty())
4594 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4595 UnusedLocalTypedefNameCandidates);
4596
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004597 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004598 if (!PendingInstantiations.empty())
4599 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004600
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004601 // Write the record containing declaration references of Sema.
4602 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004603 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004604
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004605 // Write the record containing CUDA-specific declaration references.
4606 if (!CUDASpecialDeclRefs.empty())
4607 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004608
4609 // Write the delegating constructors.
4610 if (!DelegatingCtorDecls.empty())
4611 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004612
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004613 // Write the known namespaces.
4614 if (!KnownNamespaces.empty())
4615 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004616
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004617 // Write the undefined internal functions and variables, and inline functions.
4618 if (!UndefinedButUsed.empty())
4619 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004620
Douglas Gregor851443c2011-08-12 01:39:19 +00004621 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004622 for (auto *DC : UpdatedDeclContexts)
4623 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004624
Douglas Gregor959bb062011-12-03 01:15:29 +00004625 if (!WritingModule) {
4626 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004627 struct ModuleInfo {
4628 uint64_t ID;
4629 Module *M;
4630 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4631 };
Richard Smith56be7542014-03-21 00:33:59 +00004632 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004633 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004634 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004635 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4636 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004637 }
Richard Smith56be7542014-03-21 00:33:59 +00004638
4639 if (!Imports.empty()) {
4640 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4641 return A.ID < B.ID;
4642 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004643 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4644 return A.ID == B.ID;
4645 };
Richard Smith56be7542014-03-21 00:33:59 +00004646
4647 // Sort and deduplicate module IDs.
4648 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004649 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004650 Imports.end());
4651
4652 RecordData ImportedModules;
4653 for (const auto &Import : Imports) {
4654 ImportedModules.push_back(Import.ID);
4655 // FIXME: If the module has macros imported then later has declarations
4656 // imported, this location won't be the right one as a location for the
4657 // declaration imports.
4658 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4659 }
4660
Douglas Gregor959bb062011-12-03 01:15:29 +00004661 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4662 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004663 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004664
Douglas Gregor851443c2011-08-12 01:39:19 +00004665 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004666 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004667 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004668 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004669 WriteLateParsedTemplates(SemaRef);
Dario Domizioli13a0a382014-05-23 12:13:25 +00004670 if(!WritingModule)
4671 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004672
Douglas Gregor08f01292009-04-17 22:13:46 +00004673 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004674 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004675 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004676 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004677 Record.push_back(NumLexicalDeclContexts);
4678 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004679 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004680 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004681}
4682
Richard Smithb9eab6d2014-03-20 19:44:17 +00004683void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004684 if (DeclUpdates.empty())
4685 return;
4686
Richard Smith59442b42014-03-20 20:07:19 +00004687 DeclUpdateMap LocalUpdates;
4688 LocalUpdates.swap(DeclUpdates);
4689
Richard Smith6ef42932014-03-20 21:02:00 +00004690 for (auto &DeclUpdate : LocalUpdates) {
4691 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004692 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004693 continue; // The decl will be written completely,no need to store updates.
4694
Richard Smithd28ac5b2014-03-22 23:33:22 +00004695 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004696 RecordData Record;
4697 for (auto &Update : DeclUpdate.second) {
4698 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4699
4700 Record.push_back(Kind);
4701 switch (Kind) {
4702 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4703 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4704 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004705 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004706 Record.push_back(GetDeclRef(Update.getDecl()));
4707 break;
4708
Richard Smith4d235792014-08-07 18:53:08 +00004709 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004710 // An updated body is emitted last, so that the reader doesn't need
4711 // to skip over the lazy body to reach statements for other records.
4712 Record.pop_back();
4713 HasUpdatedBody = true;
4714 break;
4715
Richard Smith4d235792014-08-07 18:53:08 +00004716 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4717 AddSourceLocation(Update.getLoc(), Record);
4718 break;
4719
Richard Smithcd45dbc2014-04-19 03:48:30 +00004720 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4721 auto *RD = cast<CXXRecordDecl>(D);
4722 AddUpdatedDeclContext(RD->getPrimaryContext());
4723 AddCXXDefinitionData(RD, Record);
4724 Record.push_back(WriteDeclContextLexicalBlock(
4725 *Context, const_cast<CXXRecordDecl *>(RD)));
4726
4727 // This state is sometimes updated by template instantiation, when we
4728 // switch from the specialization referring to the template declaration
4729 // to it referring to the template definition.
4730 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4731 Record.push_back(MSInfo->getTemplateSpecializationKind());
4732 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4733 } else {
4734 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4735 Record.push_back(Spec->getTemplateSpecializationKind());
4736 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004737
4738 // The instantiation might have been resolved to a partial
4739 // specialization. If so, record which one.
4740 auto From = Spec->getInstantiatedFrom();
4741 if (auto PartialSpec =
4742 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4743 Record.push_back(true);
4744 AddDeclRef(PartialSpec, Record);
4745 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4746 Record);
4747 } else {
4748 Record.push_back(false);
4749 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004750 }
4751 Record.push_back(RD->getTagKind());
4752 AddSourceLocation(RD->getLocation(), Record);
4753 AddSourceLocation(RD->getLocStart(), Record);
4754 AddSourceLocation(RD->getRBraceLoc(), Record);
4755
4756 // Instantiation may change attributes; write them all out afresh.
4757 Record.push_back(D->hasAttrs());
4758 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004759 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4760 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004761
4762 // FIXME: Ensure we don't get here for explicit instantiations.
4763 break;
4764 }
4765
Richard Smith564417a2014-03-20 21:47:22 +00004766 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4767 addExceptionSpec(
4768 *this,
4769 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4770 Record);
4771 break;
4772
Richard Smith6ef42932014-03-20 21:02:00 +00004773 case UPD_CXX_DEDUCED_RETURN_TYPE:
4774 Record.push_back(GetOrCreateTypeID(Update.getType()));
4775 break;
4776
4777 case UPD_DECL_MARKED_USED:
4778 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004779
4780 case UPD_MANGLING_NUMBER:
4781 case UPD_STATIC_LOCAL_NUMBER:
4782 Record.push_back(Update.getNumber());
4783 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004784 }
4785 }
4786
Richard Smithd28ac5b2014-03-22 23:33:22 +00004787 if (HasUpdatedBody) {
4788 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004789 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004790 Record.push_back(Def->isInlined());
4791 AddSourceLocation(Def->getInnerLocStart(), Record);
4792 AddFunctionDefinition(Def, Record);
Richard Smith4d235792014-08-07 18:53:08 +00004793 if (auto *DD = dyn_cast<CXXDestructorDecl>(Def))
4794 Record.push_back(GetDeclRef(DD->getOperatorDelete()));
Richard Smithd28ac5b2014-03-22 23:33:22 +00004795 }
4796
Richard Smithcd45dbc2014-04-19 03:48:30 +00004797 OffsetsRecord.push_back(GetDeclRef(D));
4798 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4799
Richard Smith6ef42932014-03-20 21:02:00 +00004800 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004801
Richard Smithb9eab6d2014-03-20 19:44:17 +00004802 // Flush any statements that were written as part of this update record.
4803 FlushStmts();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004804
4805 // Flush C++ base specifiers, if there are any.
4806 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004807 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004808}
4809
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004810void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004811 if (ReplacedDecls.empty())
4812 return;
4813
4814 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004815 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4816 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004817 Record.push_back(I->ID);
4818 Record.push_back(I->Offset);
4819 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004820 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004821 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004822}
4823
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004824void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004825 Record.push_back(Loc.getRawEncoding());
4826}
4827
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004828void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004829 AddSourceLocation(Range.getBegin(), Record);
4830 AddSourceLocation(Range.getEnd(), Record);
4831}
4832
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004833void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004834 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004835 const uint64_t *Words = Value.getRawData();
4836 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004837}
4838
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004839void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004840 Record.push_back(Value.isUnsigned());
4841 AddAPInt(Value, Record);
4842}
4843
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004844void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004845 AddAPInt(Value.bitcastToAPInt(), Record);
4846}
4847
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004848void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004849 Record.push_back(getIdentifierRef(II));
4850}
4851
Sebastian Redl539c5062010-08-18 23:57:32 +00004852IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004853 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004854 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004855
Sebastian Redl539c5062010-08-18 23:57:32 +00004856 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004857 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004858 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004859 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004860}
4861
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004862MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004863 // Don't emit builtin macros like __LINE__ to the AST file unless they
4864 // have been redefined by the header (in which case they are not
4865 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004866 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004867 return 0;
4868
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004869 MacroID &ID = MacroIDs[MI];
4870 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004871 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004872 MacroInfoToEmitData Info = { Name, MI, ID };
4873 MacroInfosToEmit.push_back(Info);
4874 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004875 return ID;
4876}
4877
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004878MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004879 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004880 return 0;
4881
4882 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4883 return MacroIDs[MI];
4884}
4885
4886uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4887 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4888 return IdentMacroDirectivesOffsetMap[Name];
4889}
4890
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004891void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004892 Record.push_back(getSelectorRef(SelRef));
4893}
4894
Sebastian Redl539c5062010-08-18 23:57:32 +00004895SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004896 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004897 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004898 }
4899
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004900 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004901 if (SID == 0 && Chain) {
4902 // This might trigger a ReadSelector callback, which will set the ID for
4903 // this selector.
4904 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004905 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004906 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004907 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004908 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004909 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004910 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004911 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004912}
4913
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004914void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004915 AddDeclRef(Temp->getDestructor(), Record);
4916}
4917
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004918void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4919 CXXBaseSpecifier const *BasesEnd,
4920 RecordDataImpl &Record) {
4921 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4922 CXXBaseSpecifiersToWrite.push_back(
4923 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4924 Bases, BasesEnd));
4925 Record.push_back(NextCXXBaseSpecifiersID++);
4926}
4927
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004928void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004929 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004930 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004931 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004932 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004933 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004934 break;
4935 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004936 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004937 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004938 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004939 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004940 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004941 break;
4942 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004943 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004944 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004945 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004946 break;
John McCall0ad16662009-10-29 08:12:44 +00004947 case TemplateArgument::Null:
4948 case TemplateArgument::Integral:
4949 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004950 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004951 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004952 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004953 break;
4954 }
4955}
4956
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004957void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004958 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004959 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004960
4961 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4962 bool InfoHasSameExpr
4963 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4964 Record.push_back(InfoHasSameExpr);
4965 if (InfoHasSameExpr)
4966 return; // Avoid storing the same expr twice.
4967 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004968 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4969 Record);
4970}
4971
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004972void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4973 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004974 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004975 AddTypeRef(QualType(), Record);
4976 return;
4977 }
4978
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004979 AddTypeLoc(TInfo->getTypeLoc(), Record);
4980}
4981
4982void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4983 AddTypeRef(TL.getType(), Record);
4984
John McCall8f115c62009-10-16 21:56:05 +00004985 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004986 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004987 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004988}
4989
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004990void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004991 Record.push_back(GetOrCreateTypeID(T));
4992}
4993
Douglas Gregoreda8e122011-08-09 15:13:55 +00004994TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004995 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004996 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004997 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4998}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004999
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005000TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005001 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00005002 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005003 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005004}
5005
5006TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
5007 if (T.isNull())
5008 return TypeIdx();
5009 assert(!T.getLocalFastQualifiers());
5010
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00005011 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005012 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005013 if (DoneWritingDeclsAndTypes) {
5014 assert(0 && "New type seen after serializing all the types to emit!");
5015 return TypeIdx();
5016 }
5017
Douglas Gregor1970d882009-04-26 03:49:13 +00005018 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00005019 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005020 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00005021 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00005022 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005023 return Idx;
5024}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005025
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005026TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005027 if (T.isNull())
5028 return TypeIdx();
5029 assert(!T.getLocalFastQualifiers());
5030
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005031 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5032 assert(I != TypeIdxs.end() && "Type not emitted!");
5033 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005034}
5035
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005036void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005037 Record.push_back(GetDeclRef(D));
5038}
5039
Sebastian Redl539c5062010-08-18 23:57:32 +00005040DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005041 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005042
5043 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005044 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005045 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005046
5047 // 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 Gregor9b3932c2010-10-05 18:37:06 +00005052 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005053 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005054 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005055 if (DoneWritingDeclsAndTypes) {
5056 assert(0 && "New decl seen after serializing all the decls to emit!");
5057 return 0;
5058 }
5059
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005060 // We haven't seen this declaration before. Give it a new ID and
5061 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005062 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005063 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005064 }
5065
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005066 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005067}
5068
Sebastian Redl539c5062010-08-18 23:57:32 +00005069DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005070 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005071 return 0;
5072
Douglas Gregorb3163e52012-01-05 22:33:30 +00005073 // If D comes from an AST file, its declaration ID is already known and
5074 // fixed.
5075 if (D->isFromASTFile())
5076 return D->getGlobalID();
5077
Douglas Gregore84a9da2009-04-20 20:36:09 +00005078 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5079 return DeclIDs[D];
5080}
5081
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005082void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005083 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005084 assert(D);
5085
5086 SourceLocation Loc = D->getLocation();
5087 if (Loc.isInvalid())
5088 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005089
5090 // We only keep track of the file-level declarations of each file.
5091 if (!D->getLexicalDeclContext()->isFileContext())
5092 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005093 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5094 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005095 if (isa<ParmVarDecl>(D))
5096 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005097
5098 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005099 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005100 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005101 FileID FID;
5102 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005103 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005104 if (FID.isInvalid())
5105 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005106 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005107
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005108 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005109 if (!Info)
5110 Info = new DeclIDInFileInfo();
5111
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005112 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005113 LocDeclIDsTy &Decls = Info->DeclIDs;
5114
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005115 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005116 Decls.push_back(LocDecl);
5117 return;
5118 }
5119
Benjamin Kramer45025c02013-08-24 13:22:59 +00005120 LocDeclIDsTy::iterator I =
5121 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005122
5123 Decls.insert(I, LocDecl);
5124}
5125
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005126void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005127 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005128 Record.push_back(Name.getNameKind());
5129 switch (Name.getNameKind()) {
5130 case DeclarationName::Identifier:
5131 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5132 break;
5133
5134 case DeclarationName::ObjCZeroArgSelector:
5135 case DeclarationName::ObjCOneArgSelector:
5136 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005137 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005138 break;
5139
5140 case DeclarationName::CXXConstructorName:
5141 case DeclarationName::CXXDestructorName:
5142 case DeclarationName::CXXConversionFunctionName:
5143 AddTypeRef(Name.getCXXNameType(), Record);
5144 break;
5145
5146 case DeclarationName::CXXOperatorName:
5147 Record.push_back(Name.getCXXOverloadedOperator());
5148 break;
5149
Alexis Hunt3d221f22009-11-29 07:34:05 +00005150 case DeclarationName::CXXLiteralOperatorName:
5151 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5152 break;
5153
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005154 case DeclarationName::CXXUsingDirective:
5155 // No extra data to emit
5156 break;
5157 }
5158}
Chris Lattnerca025db2010-05-07 21:43:38 +00005159
Richard Smithd08aeb62014-08-28 01:33:39 +00005160unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5161 assert(needsAnonymousDeclarationNumber(D) &&
5162 "expected an anonymous declaration");
5163
5164 // Number the anonymous declarations within this context, if we've not
5165 // already done so.
5166 auto It = AnonymousDeclarationNumbers.find(D);
5167 if (It == AnonymousDeclarationNumbers.end()) {
5168 unsigned Index = 0;
5169 for (Decl *LexicalD : D->getLexicalDeclContext()->decls()) {
5170 auto *ND = dyn_cast<NamedDecl>(LexicalD);
5171 if (!ND || !needsAnonymousDeclarationNumber(ND))
5172 continue;
5173 AnonymousDeclarationNumbers[ND] = Index++;
5174 }
5175
5176 It = AnonymousDeclarationNumbers.find(D);
5177 assert(It != AnonymousDeclarationNumbers.end() &&
5178 "declaration not found within its lexical context");
5179 }
5180
5181 return It->second;
5182}
5183
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005184void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005185 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005186 switch (Name.getNameKind()) {
5187 case DeclarationName::CXXConstructorName:
5188 case DeclarationName::CXXDestructorName:
5189 case DeclarationName::CXXConversionFunctionName:
5190 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5191 break;
5192
5193 case DeclarationName::CXXOperatorName:
5194 AddSourceLocation(
5195 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5196 Record);
5197 AddSourceLocation(
5198 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5199 Record);
5200 break;
5201
5202 case DeclarationName::CXXLiteralOperatorName:
5203 AddSourceLocation(
5204 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5205 Record);
5206 break;
5207
5208 case DeclarationName::Identifier:
5209 case DeclarationName::ObjCZeroArgSelector:
5210 case DeclarationName::ObjCOneArgSelector:
5211 case DeclarationName::ObjCMultiArgSelector:
5212 case DeclarationName::CXXUsingDirective:
5213 break;
5214 }
5215}
5216
5217void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005218 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005219 AddDeclarationName(NameInfo.getName(), Record);
5220 AddSourceLocation(NameInfo.getLoc(), Record);
5221 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5222}
5223
5224void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005225 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005226 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005227 Record.push_back(Info.NumTemplParamLists);
5228 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5229 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5230}
5231
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005232void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005233 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005234 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005235 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005236 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005237
5238 // Push each of the NNS's onto a stack for serialization in reverse order.
5239 while (NNS) {
5240 NestedNames.push_back(NNS);
5241 NNS = NNS->getPrefix();
5242 }
5243
5244 Record.push_back(NestedNames.size());
5245 while(!NestedNames.empty()) {
5246 NNS = NestedNames.pop_back_val();
5247 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5248 Record.push_back(Kind);
5249 switch (Kind) {
5250 case NestedNameSpecifier::Identifier:
5251 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5252 break;
5253
5254 case NestedNameSpecifier::Namespace:
5255 AddDeclRef(NNS->getAsNamespace(), Record);
5256 break;
5257
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005258 case NestedNameSpecifier::NamespaceAlias:
5259 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5260 break;
5261
Chris Lattnerca025db2010-05-07 21:43:38 +00005262 case NestedNameSpecifier::TypeSpec:
5263 case NestedNameSpecifier::TypeSpecWithTemplate:
5264 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5265 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5266 break;
5267
5268 case NestedNameSpecifier::Global:
5269 // Don't need to write an associated value.
5270 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005271
5272 case NestedNameSpecifier::Super:
5273 AddDeclRef(NNS->getAsRecordDecl(), Record);
5274 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005275 }
5276 }
5277}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005278
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005279void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5280 RecordDataImpl &Record) {
5281 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005282 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005283 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005284
5285 // Push each of the nested-name-specifiers's onto a stack for
5286 // serialization in reverse order.
5287 while (NNS) {
5288 NestedNames.push_back(NNS);
5289 NNS = NNS.getPrefix();
5290 }
5291
5292 Record.push_back(NestedNames.size());
5293 while(!NestedNames.empty()) {
5294 NNS = NestedNames.pop_back_val();
5295 NestedNameSpecifier::SpecifierKind Kind
5296 = NNS.getNestedNameSpecifier()->getKind();
5297 Record.push_back(Kind);
5298 switch (Kind) {
5299 case NestedNameSpecifier::Identifier:
5300 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5301 AddSourceRange(NNS.getLocalSourceRange(), Record);
5302 break;
5303
5304 case NestedNameSpecifier::Namespace:
5305 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5306 AddSourceRange(NNS.getLocalSourceRange(), Record);
5307 break;
5308
5309 case NestedNameSpecifier::NamespaceAlias:
5310 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5311 AddSourceRange(NNS.getLocalSourceRange(), Record);
5312 break;
5313
5314 case NestedNameSpecifier::TypeSpec:
5315 case NestedNameSpecifier::TypeSpecWithTemplate:
5316 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5317 AddTypeLoc(NNS.getTypeLoc(), Record);
5318 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5319 break;
5320
5321 case NestedNameSpecifier::Global:
5322 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5323 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005324
5325 case NestedNameSpecifier::Super:
5326 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5327 AddSourceRange(NNS.getLocalSourceRange(), Record);
5328 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005329 }
5330 }
5331}
5332
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005333void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005334 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005335 Record.push_back(Kind);
5336 switch (Kind) {
5337 case TemplateName::Template:
5338 AddDeclRef(Name.getAsTemplateDecl(), Record);
5339 break;
5340
5341 case TemplateName::OverloadedTemplate: {
5342 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5343 Record.push_back(OvT->size());
5344 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5345 I != E; ++I)
5346 AddDeclRef(*I, Record);
5347 break;
5348 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005349
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005350 case TemplateName::QualifiedTemplate: {
5351 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5352 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5353 Record.push_back(QualT->hasTemplateKeyword());
5354 AddDeclRef(QualT->getTemplateDecl(), Record);
5355 break;
5356 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005357
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005358 case TemplateName::DependentTemplate: {
5359 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5360 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5361 Record.push_back(DepT->isIdentifier());
5362 if (DepT->isIdentifier())
5363 AddIdentifierRef(DepT->getIdentifier(), Record);
5364 else
5365 Record.push_back(DepT->getOperator());
5366 break;
5367 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005368
5369 case TemplateName::SubstTemplateTemplateParm: {
5370 SubstTemplateTemplateParmStorage *subst
5371 = Name.getAsSubstTemplateTemplateParm();
5372 AddDeclRef(subst->getParameter(), Record);
5373 AddTemplateName(subst->getReplacement(), Record);
5374 break;
5375 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005376
5377 case TemplateName::SubstTemplateTemplateParmPack: {
5378 SubstTemplateTemplateParmPackStorage *SubstPack
5379 = Name.getAsSubstTemplateTemplateParmPack();
5380 AddDeclRef(SubstPack->getParameterPack(), Record);
5381 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5382 break;
5383 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005384 }
5385}
5386
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005387void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005388 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005389 Record.push_back(Arg.getKind());
5390 switch (Arg.getKind()) {
5391 case TemplateArgument::Null:
5392 break;
5393 case TemplateArgument::Type:
5394 AddTypeRef(Arg.getAsType(), Record);
5395 break;
5396 case TemplateArgument::Declaration:
5397 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005398 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005399 break;
5400 case TemplateArgument::NullPtr:
5401 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005402 break;
5403 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005404 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005405 AddTypeRef(Arg.getIntegralType(), Record);
5406 break;
5407 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005408 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5409 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005410 case TemplateArgument::TemplateExpansion:
5411 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005412 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005413 Record.push_back(*NumExpansions + 1);
5414 else
5415 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005416 break;
5417 case TemplateArgument::Expression:
5418 AddStmt(Arg.getAsExpr());
5419 break;
5420 case TemplateArgument::Pack:
5421 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005422 for (const auto &P : Arg.pack_elements())
5423 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005424 break;
5425 }
5426}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005427
5428void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005429ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005430 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005431 assert(TemplateParams && "No TemplateParams!");
5432 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5433 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5434 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5435 Record.push_back(TemplateParams->size());
5436 for (TemplateParameterList::const_iterator
5437 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5438 P != PEnd; ++P)
5439 AddDeclRef(*P, Record);
5440}
5441
5442/// \brief Emit a template argument list.
5443void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005444ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005445 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005446 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005447 Record.push_back(TemplateArgs->size());
5448 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005449 AddTemplateArgument(TemplateArgs->get(i), Record);
5450}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005451
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005452void
5453ASTWriter::AddASTTemplateArgumentListInfo
5454(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5455 assert(ASTTemplArgList && "No ASTTemplArgList!");
5456 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5457 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5458 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5459 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5460 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5461 AddTemplateArgumentLoc(TemplArgs[i], Record);
5462}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005463
5464void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005465ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005466 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005467 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005468 I = Set.begin(), E = Set.end(); I != E; ++I) {
5469 AddDeclRef(I.getDecl(), Record);
5470 Record.push_back(I.getAccess());
5471 }
5472}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005473
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005474void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005475 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005476 Record.push_back(Base.isVirtual());
5477 Record.push_back(Base.isBaseOfClass());
5478 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005479 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005480 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005481 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005482 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5483 : SourceLocation(),
5484 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005485}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005486
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005487void ASTWriter::FlushCXXBaseSpecifiers() {
5488 RecordData Record;
5489 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5490 Record.clear();
5491
5492 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005493 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005494 if (Index == CXXBaseSpecifiersOffsets.size())
5495 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5496 else {
5497 if (Index > CXXBaseSpecifiersOffsets.size())
5498 CXXBaseSpecifiersOffsets.resize(Index + 1);
5499 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5500 }
5501
5502 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5503 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5504 Record.push_back(BEnd - B);
5505 for (; B != BEnd; ++B)
5506 AddCXXBaseSpecifier(*B, Record);
5507 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005508
5509 // Flush any expressions that were written as part of the base specifiers.
5510 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005511 }
5512
5513 CXXBaseSpecifiersToWrite.clear();
5514}
5515
Alexis Hunt1d792652011-01-08 20:30:50 +00005516void ASTWriter::AddCXXCtorInitializers(
5517 const CXXCtorInitializer * const *CtorInitializers,
5518 unsigned NumCtorInitializers,
5519 RecordDataImpl &Record) {
5520 Record.push_back(NumCtorInitializers);
5521 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5522 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005523
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005524 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005525 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005526 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005527 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005528 } else if (Init->isDelegatingInitializer()) {
5529 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005530 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005531 } else if (Init->isMemberInitializer()){
5532 Record.push_back(CTOR_INITIALIZER_MEMBER);
5533 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005534 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005535 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5536 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005537 }
Francois Pichetd583da02010-12-04 09:14:42 +00005538
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005539 AddSourceLocation(Init->getMemberLocation(), Record);
5540 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005541 AddSourceLocation(Init->getLParenLoc(), Record);
5542 AddSourceLocation(Init->getRParenLoc(), Record);
5543 Record.push_back(Init->isWritten());
5544 if (Init->isWritten()) {
5545 Record.push_back(Init->getSourceOrder());
5546 } else {
5547 Record.push_back(Init->getNumArrayIndices());
5548 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5549 AddDeclRef(Init->getArrayIndex(i), Record);
5550 }
5551 }
5552}
5553
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005554void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005555 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005556 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005557 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005558 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005559 Record.push_back(Data.Aggregate);
5560 Record.push_back(Data.PlainOldData);
5561 Record.push_back(Data.Empty);
5562 Record.push_back(Data.Polymorphic);
5563 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005564 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005565 Record.push_back(Data.HasNoNonEmptyBases);
5566 Record.push_back(Data.HasPrivateFields);
5567 Record.push_back(Data.HasProtectedFields);
5568 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005569 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005570 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005571 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005572 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005573 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005574 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5575 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5576 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5577 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5578 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5579 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005580 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005581 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005582 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005583 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005584 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005585 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005586 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005587 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005588 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005589 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005590 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5591 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5592 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5593 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005594 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005595
5596 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005597 if (Data.NumBases > 0)
5598 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5599 Record);
5600
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005601 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5602 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005603 if (Data.NumVBases > 0)
5604 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5605 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005606
Richard Smitha4ba74c2013-08-30 04:46:40 +00005607 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5608 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005609 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005610 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005611
5612 // Add lambda-specific data.
5613 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005614 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005615 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005616 Record.push_back(Lambda.IsGenericLambda);
5617 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005618 Record.push_back(Lambda.NumCaptures);
5619 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005620 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005621 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005622 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005623 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005624 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005625 AddSourceLocation(Capture.getLocation(), Record);
5626 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005627 Record.push_back(Capture.getCaptureKind());
5628 switch (Capture.getCaptureKind()) {
5629 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005630 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005631 break;
5632 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005633 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005634 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005635 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005636 AddDeclRef(Var, Record);
5637 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5638 : SourceLocation(),
5639 Record);
5640 break;
5641 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005642 }
5643 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005644}
5645
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005646void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005647 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005648 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005649 assert(FirstDeclID == NextDeclID &&
5650 FirstTypeID == NextTypeID &&
5651 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005652 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005653 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005654 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005655 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005656
Sebastian Redl07a89a82010-07-30 00:29:29 +00005657 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005658
Douglas Gregordf0c1512011-08-18 04:12:04 +00005659 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5660 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5661 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005662 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005663 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005664 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005665 NextDeclID = FirstDeclID;
5666 NextTypeID = FirstTypeID;
5667 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005668 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005669 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005670 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005671}
5672
Sebastian Redl539c5062010-08-18 23:57:32 +00005673void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005674 // Always keep the highest ID. See \p TypeRead() for more information.
5675 IdentID &StoredID = IdentifierIDs[II];
5676 if (ID > StoredID)
5677 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005678}
5679
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005680void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005681 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005682 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005683 if (ID > StoredID)
5684 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005685}
5686
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005687void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005688 // Always take the highest-numbered type index. This copes with an interesting
5689 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005690 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005691 // keep the higher-numbered entry so that we can properly write it out to
5692 // the AST file.
5693 TypeIdx &StoredIdx = TypeIdxs[T];
5694 if (Idx.getIndex() >= StoredIdx.getIndex())
5695 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005696}
5697
Sebastian Redl539c5062010-08-18 23:57:32 +00005698void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005699 // Always keep the highest ID. See \p TypeRead() for more information.
5700 SelectorID &StoredID = SelectorIDs[S];
5701 if (ID > StoredID)
5702 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005703}
Douglas Gregor91096292010-10-02 19:29:26 +00005704
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005705void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005706 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005707 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005708 MacroDefinitions[MD] = ID;
5709}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005710
Douglas Gregore37a85a2011-12-02 17:30:13 +00005711void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5712 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5713 SubmoduleIDs[Mod] = ID;
5714}
5715
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005716void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005717 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005718 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005719 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5720 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005721 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005722 // A forward reference was mutated into a definition. Rewrite it.
5723 // FIXME: This happens during template instantiation, should we
5724 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005725 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5726 "completed a tag from another module but not by instantiation?");
5727 DeclUpdates[RD].push_back(
5728 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005729 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005730 }
5731}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005732
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005733void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5734 // TU and namespaces are handled elsewhere.
5735 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5736 return;
5737
Douglas Gregorb3722e22011-09-09 23:01:35 +00005738 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005739 return; // Not a source decl added to a DeclContext from PCH.
5740
Douglas Gregor9f782892013-01-21 15:25:38 +00005741 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005742 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005743 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005744 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005745}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005746
5747void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5748 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005749 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005750 return; // Not a source member added to a class from PCH.
5751 if (!isa<CXXMethodDecl>(D))
5752 return; // We are interested in lazily declared implicit methods.
5753
5754 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005755 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005756 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005757 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005758}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005759
5760void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5761 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005762 // The specializations set is kept in the canonical template.
5763 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005764 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005765 return; // Not a source specialization added to a template from PCH.
5766
Richard Smithe9a8bc32014-09-30 00:45:29 +00005767 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005768 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5769 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005770}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005771
Larisse Voufo39a1e502013-08-06 01:03:05 +00005772void ASTWriter::AddedCXXTemplateSpecialization(
5773 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5774 // The specializations set is kept in the canonical template.
Larisse Voufo39a1e502013-08-06 01:03:05 +00005775 TD = TD->getCanonicalDecl();
5776 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5777 return; // Not a source specialization added to a template from PCH.
5778
Richard Smithe9a8bc32014-09-30 00:45:29 +00005779 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005780 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5781 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005782}
5783
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005784void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5785 const FunctionDecl *D) {
5786 // The specializations set is kept in the canonical template.
5787 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005788 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005789 return; // Not a source specialization added to a template from PCH.
5790
Richard Smithe9a8bc32014-09-30 00:45:29 +00005791 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005792 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5793 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005794}
5795
Richard Smith564417a2014-03-20 21:47:22 +00005796void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5797 assert(!WritingAST && "Already writing the AST!");
5798 FD = FD->getCanonicalDecl();
5799 if (!FD->isFromASTFile())
5800 return; // Not a function declared in PCH and defined outside.
5801
5802 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5803}
5804
Richard Smith1fa5d642013-05-11 05:45:24 +00005805void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5806 assert(!WritingAST && "Already writing the AST!");
5807 FD = FD->getCanonicalDecl();
5808 if (!FD->isFromASTFile())
5809 return; // Not a function declared in PCH and defined outside.
5810
Aaron Ballman4f45b712014-03-21 15:22:56 +00005811 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith1fa5d642013-05-11 05:45:24 +00005812}
5813
Sebastian Redlab238a72011-04-24 16:28:06 +00005814void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005815 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005816 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005817 return; // Declaration not imported from PCH.
5818
Richard Smith4d235792014-08-07 18:53:08 +00005819 // Implicit function decl from a PCH was defined.
5820 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005821}
5822
Richard Smithd28ac5b2014-03-22 23:33:22 +00005823void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5824 assert(!WritingAST && "Already writing the AST!");
5825 if (!D->isFromASTFile())
5826 return;
5827
Richard Smithd28ac5b2014-03-22 23:33:22 +00005828 DeclUpdates[D].push_back(
Richard Smith4d235792014-08-07 18:53:08 +00005829 DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005830}
5831
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005832void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005833 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005834 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005835 return;
5836
5837 // Since the actual instantiation is delayed, this really means that we need
5838 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005839 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005840 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5841 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005842}
5843
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005844void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5845 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005846 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005847 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005848 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005849
5850 assert(IFD->getDefinition() && "Category on a class without a definition?");
5851 ObjCClassesWithCategories.insert(
5852 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005853}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005854
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005855
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005856void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5857 const ObjCPropertyDecl *OrigProp,
5858 const ObjCCategoryDecl *ClassExt) {
5859 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5860 if (!D)
5861 return;
5862
5863 assert(!WritingAST && "Already writing the AST!");
5864 if (!D->isFromASTFile())
5865 return; // Declaration not imported from PCH.
5866
5867 RewriteDecl(D);
5868}
Eli Friedman276dd182013-09-05 00:02:25 +00005869
5870void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5871 assert(!WritingAST && "Already writing the AST!");
5872 if (!D->isFromASTFile())
5873 return;
5874
Aaron Ballman4f45b712014-03-21 15:22:56 +00005875 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005876}