blob: 82932426032d3d0b4e84bec9e81eb34edcb2f2ed [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;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000086
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000087 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000088 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089
90 void VisitArrayType(const ArrayType *T);
91 void VisitFunctionType(const FunctionType *T);
92 void VisitTagType(const TagType *T);
93
94#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
95#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096#include "clang/AST/TypeNodes.def"
97 };
98}
99
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000101 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102}
103
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000104void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000106 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107}
108
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000109void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000111 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112}
113
Reid Kleckner8a365022013-06-24 17:51:48 +0000114void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
115 Writer.AddTypeRef(T->getOriginalType(), Record);
116 Code = TYPE_DECAYED;
117}
118
Reid Kleckner0503a872013-12-05 01:23:43 +0000119void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
120 Writer.AddTypeRef(T->getOriginalType(), Record);
121 Writer.AddTypeRef(T->getAdjustedType(), Record);
122 Code = TYPE_ADJUSTED;
123}
124
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000125void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000126 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000127 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128}
129
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000131 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
132 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000133 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134}
135
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000137 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000138 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139}
140
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000142 Writer.AddTypeRef(T->getPointeeType(), Record);
143 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 Writer.AddTypeRef(T->getElementType(), Record);
149 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000150 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 VisitArrayType(T);
155 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000156 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157}
158
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000161 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162}
163
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000166 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
167 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000168 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000169 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170}
171
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173 Writer.AddTypeRef(T->getElementType(), Record);
174 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000175 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000176 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177}
178
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000179void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000181 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000182}
183
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000184void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000185 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000186 FunctionType::ExtInfo C = T->getExtInfo();
187 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000188 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000189 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000190 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000191 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000192 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193}
194
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000195void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000197 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198}
199
Richard Smith564417a2014-03-20 21:47:22 +0000200static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
201 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000202 Record.push_back(T->getExceptionSpecType());
203 if (T->getExceptionSpecType() == EST_Dynamic) {
204 Record.push_back(T->getNumExceptions());
205 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
206 Writer.AddTypeRef(T->getExceptionType(I), Record);
207 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
208 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000209 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
210 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
211 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000212 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
213 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000214 }
Richard Smith564417a2014-03-20 21:47:22 +0000215}
216
217void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
218 VisitFunctionType(T);
219 Record.push_back(T->getNumParams());
220 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
221 Writer.AddTypeRef(T->getParamType(I), Record);
222 Record.push_back(T->isVariadic());
223 Record.push_back(T->hasTrailingReturn());
224 Record.push_back(T->getTypeQuals());
225 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
226 addExceptionSpec(Writer, T, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000227 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228}
229
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000230void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000231 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000232 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000233}
John McCallb96ec562009-12-04 22:46:56 +0000234
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000235void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000237 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
238 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000239 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240}
241
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000242void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000243 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000244 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245}
246
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000249 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250}
251
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000253 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000254 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000255 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000256}
257
Alexis Hunte852b102011-05-24 22:41:36 +0000258void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
259 Writer.AddTypeRef(T->getBaseType(), Record);
260 Writer.AddTypeRef(T->getUnderlyingType(), Record);
261 Record.push_back(T->getUTTKind());
262 Code = TYPE_UNARY_TRANSFORM;
263}
264
Richard Smith30482bc2011-02-20 03:19:35 +0000265void ASTTypeWriter::VisitAutoType(const AutoType *T) {
266 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000267 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000268 if (T->getDeducedType().isNull())
269 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000270 Code = TYPE_AUTO;
271}
272
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000273void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000274 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000275 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000276 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000277 "Cannot serialize in the middle of a type definition");
278}
279
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000280void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000281 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000282 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000283}
284
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000285void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000286 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000287 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000288}
289
John McCall81904512011-01-06 01:58:22 +0000290void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
291 Writer.AddTypeRef(T->getModifiedType(), Record);
292 Writer.AddTypeRef(T->getEquivalentType(), Record);
293 Record.push_back(T->getAttrKind());
294 Code = TYPE_ATTRIBUTED;
295}
296
Mike Stump11289f42009-09-09 15:08:12 +0000297void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000298ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000299 const SubstTemplateTypeParmType *T) {
300 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
301 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000302 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000303}
304
305void
Douglas Gregorada4b792011-01-14 02:55:32 +0000306ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
307 const SubstTemplateTypeParmPackType *T) {
308 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
309 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
310 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
311}
312
313void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000314ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000315 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000316 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000317 Writer.AddTemplateName(T->getTemplateName(), Record);
318 Record.push_back(T->getNumArgs());
319 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
320 ArgI != ArgE; ++ArgI)
321 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000322 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
323 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000324 : T->getCanonicalTypeInternal(),
325 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000326 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000327}
328
329void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000331 VisitArrayType(T);
332 Writer.AddStmt(T->getSizeExpr());
333 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000334 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000335}
336
337void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000338ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000339 const DependentSizedExtVectorType *T) {
340 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000341 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000342}
343
344void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000345ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000346 Record.push_back(T->getDepth());
347 Record.push_back(T->getIndex());
348 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000349 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000350 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000351}
352
353void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000354ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000355 Record.push_back(T->getKeyword());
356 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
357 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000358 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
359 : T->getCanonicalTypeInternal(),
360 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000361 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000362}
363
364void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000365ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000366 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000367 Record.push_back(T->getKeyword());
368 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
369 Writer.AddIdentifierRef(T->getIdentifier(), Record);
370 Record.push_back(T->getNumArgs());
371 for (DependentTemplateSpecializationType::iterator
372 I = T->begin(), E = T->end(); I != E; ++I)
373 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000374 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000375}
376
Douglas Gregord2fa7662010-12-20 02:24:11 +0000377void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
378 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000379 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000380 Record.push_back(*NumExpansions + 1);
381 else
382 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000383 Code = TYPE_PACK_EXPANSION;
384}
385
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000386void ASTTypeWriter::VisitParenType(const ParenType *T) {
387 Writer.AddTypeRef(T->getInnerType(), Record);
388 Code = TYPE_PAREN;
389}
390
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000391void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000392 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000393 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
394 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000395 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000396}
397
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000398void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000399 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000400 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000401 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000402}
403
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000404void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000405 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000406 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000407}
408
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000409void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000410 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000411 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000412 for (const auto *I : T->quals())
413 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000414 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000415}
416
Steve Narofffb4330f2009-06-17 22:40:22 +0000417void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000418ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000419 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000420 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000421}
422
Eli Friedman0dfb8892011-10-06 23:00:33 +0000423void
424ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
425 Writer.AddTypeRef(T->getValueType(), Record);
426 Code = TYPE_ATOMIC;
427}
428
John McCall8f115c62009-10-16 21:56:05 +0000429namespace {
430
431class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000432 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000433 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000434
435public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000436 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000437 : Writer(Writer), Record(Record) { }
438
John McCall17001972009-10-18 01:05:36 +0000439#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000440#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000441 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000442#include "clang/AST/TypeLocNodes.def"
443
John McCall17001972009-10-18 01:05:36 +0000444 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
445 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000446};
447
448}
449
John McCall17001972009-10-18 01:05:36 +0000450void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
451 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000452}
John McCall17001972009-10-18 01:05:36 +0000453void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000454 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
455 if (TL.needsExtraLocalData()) {
456 Record.push_back(TL.getWrittenTypeSpec());
457 Record.push_back(TL.getWrittenSignSpec());
458 Record.push_back(TL.getWrittenWidthSpec());
459 Record.push_back(TL.hasModeAttr());
460 }
John McCall8f115c62009-10-16 21:56:05 +0000461}
John McCall17001972009-10-18 01:05:36 +0000462void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000464}
John McCall17001972009-10-18 01:05:36 +0000465void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000467}
Reid Kleckner8a365022013-06-24 17:51:48 +0000468void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
469 // nothing to do
470}
Reid Kleckner0503a872013-12-05 01:23:43 +0000471void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
472 // nothing to do
473}
John McCall17001972009-10-18 01:05:36 +0000474void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
475 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000476}
John McCall17001972009-10-18 01:05:36 +0000477void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000479}
John McCall17001972009-10-18 01:05:36 +0000480void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000482}
John McCall17001972009-10-18 01:05:36 +0000483void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000485 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
489 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
490 Record.push_back(TL.getSizeExpr() ? 1 : 0);
491 if (TL.getSizeExpr())
492 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000493}
John McCall17001972009-10-18 01:05:36 +0000494void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
495 VisitArrayTypeLoc(TL);
496}
497void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
498 VisitArrayTypeLoc(TL);
499}
500void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
501 VisitArrayTypeLoc(TL);
502}
503void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
504 DependentSizedArrayTypeLoc TL) {
505 VisitArrayTypeLoc(TL);
506}
507void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
508 DependentSizedExtVectorTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getNameLoc(), Record);
510}
511void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
513}
514void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getNameLoc(), Record);
516}
517void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000518 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000519 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
520 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000521 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000522 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
523 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000524}
525void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
526 VisitFunctionTypeLoc(TL);
527}
528void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
529 VisitFunctionTypeLoc(TL);
530}
John McCallb96ec562009-12-04 22:46:56 +0000531void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
532 Writer.AddSourceLocation(TL.getNameLoc(), Record);
533}
John McCall17001972009-10-18 01:05:36 +0000534void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
537void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000538 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
539 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
540 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000541}
542void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000543 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
544 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
545 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
546 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000547}
548void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
Alexis Hunte852b102011-05-24 22:41:36 +0000551void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getKWLoc(), Record);
553 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
554 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
555 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
556}
Richard Smith30482bc2011-02-20 03:19:35 +0000557void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
John McCall17001972009-10-18 01:05:36 +0000560void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
562}
563void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
564 Writer.AddSourceLocation(TL.getNameLoc(), Record);
565}
John McCall81904512011-01-06 01:58:22 +0000566void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
567 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
568 if (TL.hasAttrOperand()) {
569 SourceRange range = TL.getAttrOperandParensRange();
570 Writer.AddSourceLocation(range.getBegin(), Record);
571 Writer.AddSourceLocation(range.getEnd(), Record);
572 }
573 if (TL.hasAttrExprOperand()) {
574 Expr *operand = TL.getAttrExprOperand();
575 Record.push_back(operand ? 1 : 0);
576 if (operand) Writer.AddStmt(operand);
577 } else if (TL.hasAttrEnumOperand()) {
578 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
579 }
580}
John McCall17001972009-10-18 01:05:36 +0000581void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583}
John McCallcebee162009-10-18 09:09:24 +0000584void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
585 SubstTemplateTypeParmTypeLoc TL) {
586 Writer.AddSourceLocation(TL.getNameLoc(), Record);
587}
Douglas Gregorada4b792011-01-14 02:55:32 +0000588void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
589 SubstTemplateTypeParmPackTypeLoc TL) {
590 Writer.AddSourceLocation(TL.getNameLoc(), Record);
591}
John McCall17001972009-10-18 01:05:36 +0000592void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
593 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000594 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000595 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
596 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
597 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
598 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000599 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
600 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000601}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000602void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
604 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
605}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000606void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000607 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000608 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000609}
John McCalle78aac42010-03-10 03:28:59 +0000610void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
611 Writer.AddSourceLocation(TL.getNameLoc(), Record);
612}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000613void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000614 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000615 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000616 Writer.AddSourceLocation(TL.getNameLoc(), Record);
617}
John McCallc392f372010-06-11 00:33:02 +0000618void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
619 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000620 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000621 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000622 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000623 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000624 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
625 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
626 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000627 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
628 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000629}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000630void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
631 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
632}
John McCall17001972009-10-18 01:05:36 +0000633void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
634 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000635}
636void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
637 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000638 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
639 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
640 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
641 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000642}
John McCallfc93cf92009-10-22 22:37:11 +0000643void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
644 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000645}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000646void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
647 Writer.AddSourceLocation(TL.getKWLoc(), Record);
648 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
649 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
650}
John McCall8f115c62009-10-16 21:56:05 +0000651
Chris Lattner19cea4e2009-04-22 05:57:30 +0000652//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000653// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000654//===----------------------------------------------------------------------===//
655
Chris Lattner28fa4e62009-04-26 22:26:21 +0000656static void EmitBlockID(unsigned ID, const char *Name,
657 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000658 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000659 Record.clear();
660 Record.push_back(ID);
661 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
662
663 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000664 if (!Name || Name[0] == 0)
665 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000666 Record.clear();
667 while (*Name)
668 Record.push_back(*Name++);
669 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
670}
671
672static void EmitRecordID(unsigned ID, const char *Name,
673 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000674 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000675 Record.clear();
676 Record.push_back(ID);
677 while (*Name)
678 Record.push_back(*Name++);
679 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000680}
681
682static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000683 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000684#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000685 RECORD(STMT_STOP);
686 RECORD(STMT_NULL_PTR);
687 RECORD(STMT_NULL);
688 RECORD(STMT_COMPOUND);
689 RECORD(STMT_CASE);
690 RECORD(STMT_DEFAULT);
691 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000692 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000693 RECORD(STMT_IF);
694 RECORD(STMT_SWITCH);
695 RECORD(STMT_WHILE);
696 RECORD(STMT_DO);
697 RECORD(STMT_FOR);
698 RECORD(STMT_GOTO);
699 RECORD(STMT_INDIRECT_GOTO);
700 RECORD(STMT_CONTINUE);
701 RECORD(STMT_BREAK);
702 RECORD(STMT_RETURN);
703 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000704 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000705 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000706 RECORD(EXPR_PREDEFINED);
707 RECORD(EXPR_DECL_REF);
708 RECORD(EXPR_INTEGER_LITERAL);
709 RECORD(EXPR_FLOATING_LITERAL);
710 RECORD(EXPR_IMAGINARY_LITERAL);
711 RECORD(EXPR_STRING_LITERAL);
712 RECORD(EXPR_CHARACTER_LITERAL);
713 RECORD(EXPR_PAREN);
714 RECORD(EXPR_UNARY_OPERATOR);
715 RECORD(EXPR_SIZEOF_ALIGN_OF);
716 RECORD(EXPR_ARRAY_SUBSCRIPT);
717 RECORD(EXPR_CALL);
718 RECORD(EXPR_MEMBER);
719 RECORD(EXPR_BINARY_OPERATOR);
720 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
721 RECORD(EXPR_CONDITIONAL_OPERATOR);
722 RECORD(EXPR_IMPLICIT_CAST);
723 RECORD(EXPR_CSTYLE_CAST);
724 RECORD(EXPR_COMPOUND_LITERAL);
725 RECORD(EXPR_EXT_VECTOR_ELEMENT);
726 RECORD(EXPR_INIT_LIST);
727 RECORD(EXPR_DESIGNATED_INIT);
728 RECORD(EXPR_IMPLICIT_VALUE_INIT);
729 RECORD(EXPR_VA_ARG);
730 RECORD(EXPR_ADDR_LABEL);
731 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000732 RECORD(EXPR_CHOOSE);
733 RECORD(EXPR_GNU_NULL);
734 RECORD(EXPR_SHUFFLE_VECTOR);
735 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000736 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000737 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000738 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000739 RECORD(EXPR_OBJC_ARRAY_LITERAL);
740 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000741 RECORD(EXPR_OBJC_ENCODE);
742 RECORD(EXPR_OBJC_SELECTOR_EXPR);
743 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
744 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
745 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
746 RECORD(EXPR_OBJC_KVC_REF_EXPR);
747 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000748 RECORD(STMT_OBJC_FOR_COLLECTION);
749 RECORD(STMT_OBJC_CATCH);
750 RECORD(STMT_OBJC_FINALLY);
751 RECORD(STMT_OBJC_AT_TRY);
752 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
753 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000754 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000755 RECORD(EXPR_CXX_OPERATOR_CALL);
756 RECORD(EXPR_CXX_CONSTRUCT);
757 RECORD(EXPR_CXX_STATIC_CAST);
758 RECORD(EXPR_CXX_DYNAMIC_CAST);
759 RECORD(EXPR_CXX_REINTERPRET_CAST);
760 RECORD(EXPR_CXX_CONST_CAST);
761 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000762 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000763 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000764 RECORD(EXPR_CXX_BOOL_LITERAL);
765 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000766 RECORD(EXPR_CXX_TYPEID_EXPR);
767 RECORD(EXPR_CXX_TYPEID_TYPE);
768 RECORD(EXPR_CXX_UUIDOF_EXPR);
769 RECORD(EXPR_CXX_UUIDOF_TYPE);
770 RECORD(EXPR_CXX_THIS);
771 RECORD(EXPR_CXX_THROW);
772 RECORD(EXPR_CXX_DEFAULT_ARG);
773 RECORD(EXPR_CXX_BIND_TEMPORARY);
774 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
775 RECORD(EXPR_CXX_NEW);
776 RECORD(EXPR_CXX_DELETE);
777 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
778 RECORD(EXPR_EXPR_WITH_CLEANUPS);
779 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
780 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
781 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
782 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
783 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000784 RECORD(EXPR_CXX_NOEXCEPT);
785 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000786 RECORD(EXPR_PACK_EXPANSION);
787 RECORD(EXPR_SIZEOF_PACK);
788 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000789 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000790#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000791}
Mike Stump11289f42009-09-09 15:08:12 +0000792
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000793void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000794 RecordData Record;
795 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000796
Sebastian Redl539c5062010-08-18 23:57:32 +0000797#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
798#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000799
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000800 // Control Block.
801 BLOCK(CONTROL_BLOCK);
802 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000803 RECORD(MODULE_NAME);
804 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000805 RECORD(IMPORTS);
806 RECORD(LANGUAGE_OPTIONS);
807 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000808 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000809 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000810 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000811 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000812 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000813 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000814 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000815 RECORD(PREPROCESSOR_OPTIONS);
816
Douglas Gregor108cb222012-10-19 00:45:00 +0000817 BLOCK(INPUT_FILES_BLOCK);
818 RECORD(INPUT_FILE);
819
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000820 // AST Top-Level Block.
821 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000822 RECORD(TYPE_OFFSET);
823 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000824 RECORD(IDENTIFIER_OFFSET);
825 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000826 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000827 RECORD(SPECIAL_TYPES);
828 RECORD(STATISTICS);
829 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000830 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000831 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000832 RECORD(SELECTOR_OFFSETS);
833 RECORD(METHOD_POOL);
834 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000835 RECORD(SOURCE_LOCATION_OFFSETS);
836 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000837 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000838 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000839 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000840 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000841 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000842 RECORD(SEMA_DECL_REFS);
843 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
844 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
845 RECORD(DECL_REPLACEMENTS);
846 RECORD(UPDATE_VISIBLE);
847 RECORD(DECL_UPDATE_OFFSETS);
848 RECORD(DECL_UPDATES);
849 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
850 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000851 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000852 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000853 RECORD(FP_PRAGMA_OPTIONS);
854 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000855 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000856 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000857 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000858 RECORD(MODULE_OFFSET_MAP);
859 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000860 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000861 RECORD(FILE_SORTED_DECLS);
862 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000863 RECORD(MERGED_DECLARATIONS);
864 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000865 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000866 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000867 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000868 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000869 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Douglas Gregor358cd442012-01-15 16:58:34 +0000870
Chris Lattner28fa4e62009-04-26 22:26:21 +0000871 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000872 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000873 RECORD(SM_SLOC_FILE_ENTRY);
874 RECORD(SM_SLOC_BUFFER_ENTRY);
875 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000876 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000877
Chris Lattner28fa4e62009-04-26 22:26:21 +0000878 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000879 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000880 RECORD(PP_MACRO_OBJECT_LIKE);
881 RECORD(PP_MACRO_FUNCTION_LIKE);
882 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000883
Douglas Gregor12bfa382009-10-17 00:13:19 +0000884 // Decls and Types block.
885 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000886 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000887 RECORD(TYPE_COMPLEX);
888 RECORD(TYPE_POINTER);
889 RECORD(TYPE_BLOCK_POINTER);
890 RECORD(TYPE_LVALUE_REFERENCE);
891 RECORD(TYPE_RVALUE_REFERENCE);
892 RECORD(TYPE_MEMBER_POINTER);
893 RECORD(TYPE_CONSTANT_ARRAY);
894 RECORD(TYPE_INCOMPLETE_ARRAY);
895 RECORD(TYPE_VARIABLE_ARRAY);
896 RECORD(TYPE_VECTOR);
897 RECORD(TYPE_EXT_VECTOR);
898 RECORD(TYPE_FUNCTION_PROTO);
899 RECORD(TYPE_FUNCTION_NO_PROTO);
900 RECORD(TYPE_TYPEDEF);
901 RECORD(TYPE_TYPEOF_EXPR);
902 RECORD(TYPE_TYPEOF);
903 RECORD(TYPE_RECORD);
904 RECORD(TYPE_ENUM);
905 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000906 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000907 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000908 RECORD(TYPE_DECLTYPE);
909 RECORD(TYPE_ELABORATED);
910 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
911 RECORD(TYPE_UNRESOLVED_USING);
912 RECORD(TYPE_INJECTED_CLASS_NAME);
913 RECORD(TYPE_OBJC_OBJECT);
914 RECORD(TYPE_TEMPLATE_TYPE_PARM);
915 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
916 RECORD(TYPE_DEPENDENT_NAME);
917 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
918 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
919 RECORD(TYPE_PAREN);
920 RECORD(TYPE_PACK_EXPANSION);
921 RECORD(TYPE_ATTRIBUTED);
922 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000923 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000924 RECORD(DECL_TYPEDEF);
925 RECORD(DECL_ENUM);
926 RECORD(DECL_RECORD);
927 RECORD(DECL_ENUM_CONSTANT);
928 RECORD(DECL_FUNCTION);
929 RECORD(DECL_OBJC_METHOD);
930 RECORD(DECL_OBJC_INTERFACE);
931 RECORD(DECL_OBJC_PROTOCOL);
932 RECORD(DECL_OBJC_IVAR);
933 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000934 RECORD(DECL_OBJC_CATEGORY);
935 RECORD(DECL_OBJC_CATEGORY_IMPL);
936 RECORD(DECL_OBJC_IMPLEMENTATION);
937 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
938 RECORD(DECL_OBJC_PROPERTY);
939 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000940 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000941 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000942 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000943 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000944 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000945 RECORD(DECL_FILE_SCOPE_ASM);
946 RECORD(DECL_BLOCK);
947 RECORD(DECL_CONTEXT_LEXICAL);
948 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000949 RECORD(DECL_NAMESPACE);
950 RECORD(DECL_NAMESPACE_ALIAS);
951 RECORD(DECL_USING);
952 RECORD(DECL_USING_SHADOW);
953 RECORD(DECL_USING_DIRECTIVE);
954 RECORD(DECL_UNRESOLVED_USING_VALUE);
955 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
956 RECORD(DECL_LINKAGE_SPEC);
957 RECORD(DECL_CXX_RECORD);
958 RECORD(DECL_CXX_METHOD);
959 RECORD(DECL_CXX_CONSTRUCTOR);
960 RECORD(DECL_CXX_DESTRUCTOR);
961 RECORD(DECL_CXX_CONVERSION);
962 RECORD(DECL_ACCESS_SPEC);
963 RECORD(DECL_FRIEND);
964 RECORD(DECL_FRIEND_TEMPLATE);
965 RECORD(DECL_CLASS_TEMPLATE);
966 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
967 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000968 RECORD(DECL_VAR_TEMPLATE);
969 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
970 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000971 RECORD(DECL_FUNCTION_TEMPLATE);
972 RECORD(DECL_TEMPLATE_TYPE_PARM);
973 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
974 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
975 RECORD(DECL_STATIC_ASSERT);
976 RECORD(DECL_CXX_BASE_SPECIFIERS);
977 RECORD(DECL_INDIRECTFIELD);
978 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
979
Douglas Gregor03412ba2011-06-03 02:27:19 +0000980 // Statements and Exprs can occur in the Decls and Types block.
981 AddStmtsExprs(Stream, Record);
982
Douglas Gregor92a96f52011-02-08 21:58:10 +0000983 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000984 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000985 RECORD(PPD_MACRO_DEFINITION);
986 RECORD(PPD_INCLUSION_DIRECTIVE);
987
Chris Lattner28fa4e62009-04-26 22:26:21 +0000988#undef RECORD
989#undef BLOCK
990 Stream.ExitBlock();
991}
992
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000993/// \brief Adjusts the given filename to only write out the portion of the
994/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000995///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000996/// \param Filename the file name to adjust.
997///
998/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
999/// the returned filename will be adjusted by this system root.
1000///
1001/// \returns either the original filename (if it needs no adjustment) or the
1002/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001003static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +00001004adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001005 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001006
Douglas Gregorc567ba22011-07-22 16:35:34 +00001007 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001008 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001009
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001010 // Verify that the filename and the system root have the same prefix.
1011 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001012 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001013 if (Filename[Pos] != isysroot[Pos])
1014 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001015
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001016 // We hit the end of the filename before we hit the end of the system root.
1017 if (!Filename[Pos])
1018 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001019
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001020 // If the file name has a '/' at the current position, skip over the '/'.
1021 // We distinguish sysroot-based includes from absolute includes by the
1022 // absence of '/' at the beginning of sysroot-based includes.
1023 if (Filename[Pos] == '/')
1024 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001025
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001026 return Filename + Pos;
1027}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001028
Douglas Gregor112b9072012-10-18 05:31:06 +00001029/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001030void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1031 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001032 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001033 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001034 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1035 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001036
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001037 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001038 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1039 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1040 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1041 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1042 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1043 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1044 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1045 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1046 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1047 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1048 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001049 Record.push_back(VERSION_MAJOR);
1050 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001051 Record.push_back(CLANG_VERSION_MAJOR);
1052 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001053 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001054 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001055 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1056 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001057
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001058 // Module name
1059 if (WritingModule) {
1060 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1061 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1063 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1064 RecordData Record;
1065 Record.push_back(MODULE_NAME);
1066 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1067 }
1068
1069 // Module map file
1070 if (WritingModule) {
1071 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1072 Abbrev->Add(BitCodeAbbrevOp(MODULE_MAP_FILE));
1073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Filename
1074 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1075
1076 assert(WritingModule->ModuleMap && "missing module map");
1077 SmallString<128> ModuleMap(WritingModule->ModuleMap->getName());
1078 llvm::sys::fs::make_absolute(ModuleMap);
1079 RecordData Record;
1080 Record.push_back(MODULE_MAP_FILE);
1081 Stream.EmitRecordWithBlob(AbbrevCode, Record, ModuleMap.str());
1082 }
1083
Douglas Gregor112b9072012-10-18 05:31:06 +00001084 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001085 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001086 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001087 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001088
1089 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1090 M != MEnd; ++M) {
1091 // Skip modules that weren't directly imported.
1092 if (!(*M)->isDirectlyImported())
1093 continue;
1094
1095 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001096 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001097 Record.push_back((*M)->File->getSize());
1098 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001099 const std::string &FileName = (*M)->FileName;
1100 Record.push_back(FileName.size());
1101 Record.append(FileName.begin(), FileName.end());
1102 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001103 Stream.EmitRecord(IMPORTS, Record);
1104 }
Mike Stump11289f42009-09-09 15:08:12 +00001105
Douglas Gregor112b9072012-10-18 05:31:06 +00001106 // Language options.
1107 Record.clear();
1108 const LangOptions &LangOpts = Context.getLangOpts();
1109#define LANGOPT(Name, Bits, Default, Description) \
1110 Record.push_back(LangOpts.Name);
1111#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1112 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1113#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001114#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1115#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001116
1117 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1118 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1119
1120 Record.push_back(LangOpts.CurrentModule.size());
1121 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001122
1123 // Comment options.
1124 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1125 for (CommentOptions::BlockCommandNamesTy::const_iterator
1126 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1127 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1128 I != IEnd; ++I) {
1129 AddString(*I, Record);
1130 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001131 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001132
Douglas Gregor112b9072012-10-18 05:31:06 +00001133 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1134
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001135 // Target options.
1136 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001137 const TargetInfo &Target = Context.getTargetInfo();
1138 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001139 AddString(TargetOpts.Triple, Record);
1140 AddString(TargetOpts.CPU, Record);
1141 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001142 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1143 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1144 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1145 }
1146 Record.push_back(TargetOpts.Features.size());
1147 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1148 AddString(TargetOpts.Features[I], Record);
1149 }
1150 Stream.EmitRecord(TARGET_OPTIONS, Record);
1151
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001152 // Diagnostic options.
1153 Record.clear();
1154 const DiagnosticOptions &DiagOpts
1155 = Context.getDiagnostics().getDiagnosticOptions();
1156#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1157#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1158 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1159#include "clang/Basic/DiagnosticOptions.def"
1160 Record.push_back(DiagOpts.Warnings.size());
1161 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1162 AddString(DiagOpts.Warnings[I], Record);
1163 // Note: we don't serialize the log or serialization file names, because they
1164 // are generally transient files and will almost always be overridden.
1165 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1166
Douglas Gregorc6317db2012-10-24 15:49:58 +00001167 // File system options.
1168 Record.clear();
1169 const FileSystemOptions &FSOpts
1170 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1171 AddString(FSOpts.WorkingDir, Record);
1172 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1173
Douglas Gregor2d302362012-10-24 16:50:34 +00001174 // Header search options.
1175 Record.clear();
1176 const HeaderSearchOptions &HSOpts
1177 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1178 AddString(HSOpts.Sysroot, Record);
1179
1180 // Include entries.
1181 Record.push_back(HSOpts.UserEntries.size());
1182 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1183 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1184 AddString(Entry.Path, Record);
1185 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001186 Record.push_back(Entry.IsFramework);
1187 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001188 }
1189
1190 // System header prefixes.
1191 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1192 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1193 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1194 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1195 }
1196
1197 AddString(HSOpts.ResourceDir, Record);
1198 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001199 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001200 Record.push_back(HSOpts.DisableModuleHash);
1201 Record.push_back(HSOpts.UseBuiltinIncludes);
1202 Record.push_back(HSOpts.UseStandardSystemIncludes);
1203 Record.push_back(HSOpts.UseStandardCXXIncludes);
1204 Record.push_back(HSOpts.UseLibcxx);
1205 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1206
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001207 // Preprocessor options.
1208 Record.clear();
1209 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1210
1211 // Macro definitions.
1212 Record.push_back(PPOpts.Macros.size());
1213 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1214 AddString(PPOpts.Macros[I].first, Record);
1215 Record.push_back(PPOpts.Macros[I].second);
1216 }
1217
1218 // Includes
1219 Record.push_back(PPOpts.Includes.size());
1220 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1221 AddString(PPOpts.Includes[I], Record);
1222
1223 // Macro includes
1224 Record.push_back(PPOpts.MacroIncludes.size());
1225 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1226 AddString(PPOpts.MacroIncludes[I], Record);
1227
Douglas Gregorb6368752012-10-24 23:41:50 +00001228 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001229 // Detailed record is important since it is used for the module cache hash.
1230 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001231 AddString(PPOpts.ImplicitPCHInclude, Record);
1232 AddString(PPOpts.ImplicitPTHInclude, Record);
1233 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1234 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1235
Douglas Gregora3b20262011-05-06 21:43:30 +00001236 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001237 SourceManager &SM = Context.getSourceManager();
1238 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1239 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001240 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1241 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001242 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1243 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1244
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001245 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001246
Michael J. Spencer740857f2010-12-21 16:45:57 +00001247 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001248
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001249 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001250 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001251 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001252 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001253 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001254 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001255 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001256 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001257
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001258 Record.clear();
1259 Record.push_back(SM.getMainFileID().getOpaqueValue());
1260 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1261
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001262 // Original PCH directory
1263 if (!OutputFile.empty() && OutputFile != "-") {
1264 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1265 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1267 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1268
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001269 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001270
1271 llvm::sys::fs::make_absolute(OutputPath);
1272 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1273
1274 RecordData Record;
1275 Record.push_back(ORIGINAL_PCH_DIR);
1276 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1277 }
1278
Douglas Gregor49491f72013-03-15 22:15:07 +00001279 WriteInputFiles(Context.SourceMgr,
1280 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001281 isysroot,
1282 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001283 Stream.ExitBlock();
1284}
1285
Douglas Gregor49491f72013-03-15 22:15:07 +00001286namespace {
1287 /// \brief An input file.
1288 struct InputFileEntry {
1289 const FileEntry *File;
1290 bool IsSystemFile;
1291 bool BufferOverridden;
1292 };
1293}
1294
1295void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1296 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001297 StringRef isysroot,
1298 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001299 using namespace llvm;
1300 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1301 RecordData Record;
1302
1303 // Create input-file abbreviation.
1304 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1305 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001306 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001307 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1308 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001309 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001310 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1311 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1312
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001313 // Get all ContentCache objects for files, sorted by whether the file is a
1314 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001315 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001316 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1317 // Get this source location entry.
1318 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001319 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001320
1321 // We only care about file entries that were not overridden.
1322 if (!SLoc->isFile())
1323 continue;
1324 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001325 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001326 continue;
1327
Douglas Gregor49491f72013-03-15 22:15:07 +00001328 InputFileEntry Entry;
1329 Entry.File = Cache->OrigEntry;
1330 Entry.IsSystemFile = Cache->IsSystemFile;
1331 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001332 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001333 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001334 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001335 SortedFiles.push_front(Entry);
1336 }
1337
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001338 unsigned UserFilesNum = 0;
1339 // Write out all of the input files.
1340 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001341 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001342 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001343 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001344
Douglas Gregor49491f72013-03-15 22:15:07 +00001345 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001346 if (InputFileID != 0)
1347 continue; // already recorded this file.
1348
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001349 // Record this entry's offset.
1350 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001351
1352 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001353
Douglas Gregor49491f72013-03-15 22:15:07 +00001354 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001355 ++UserFilesNum;
1356
Douglas Gregor72be3902012-10-19 00:38:02 +00001357 Record.clear();
1358 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001359 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001360
1361 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001362 Record.push_back(Entry.File->getSize());
1363 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001364
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001365 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001366 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001367
Douglas Gregor72be3902012-10-19 00:38:02 +00001368 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001369 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001370 SmallString<128> FilePath(Filename);
1371
1372 // Ask the file manager to fixup the relative path for us. This will
1373 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001374 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001375
1376 // FIXME: This call to make_absolute shouldn't be necessary, the
1377 // call to FixupRelativePath should always return an absolute path.
1378 llvm::sys::fs::make_absolute(FilePath);
1379 Filename = FilePath.c_str();
1380
1381 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1382
1383 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1384 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001385
Douglas Gregor112b9072012-10-18 05:31:06 +00001386 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001387
1388 // Create input file offsets abbreviation.
1389 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1390 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1391 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001392 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1393 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001394 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1395 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1396
1397 // Write input file offsets.
1398 Record.clear();
1399 Record.push_back(INPUT_FILE_OFFSETS);
1400 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001401 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001402 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001403}
1404
Douglas Gregora7f71a92009-04-10 03:52:48 +00001405//===----------------------------------------------------------------------===//
1406// Source Manager Serialization
1407//===----------------------------------------------------------------------===//
1408
1409/// \brief Create an abbreviation for the SLocEntry that refers to a
1410/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001411static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001412 using namespace llvm;
1413 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001414 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001419 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001421 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001424 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001425}
1426
1427/// \brief Create an abbreviation for the SLocEntry that refers to a
1428/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001429static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001430 using namespace llvm;
1431 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001432 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001433 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1434 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1435 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001438 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001439}
1440
1441/// \brief Create an abbreviation for the SLocEntry that refers to a
1442/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001443static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001444 using namespace llvm;
1445 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001446 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001448 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001449}
1450
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001451/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1452/// expansion.
1453static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001454 using namespace llvm;
1455 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001456 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001462 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001463}
1464
Douglas Gregor09b69892011-02-10 17:09:37 +00001465namespace {
1466 // Trait used for the on-disk hash table of header search information.
1467 class HeaderFileInfoTrait {
1468 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001469 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001470
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001471 // Keep track of the framework names we've used during serialization.
1472 SmallVector<char, 128> FrameworkStringData;
1473 llvm::StringMap<unsigned> FrameworkNameOffset;
1474
Douglas Gregor09b69892011-02-10 17:09:37 +00001475 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001476 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1477 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001478
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001479 struct key_type {
1480 const FileEntry *FE;
1481 const char *Filename;
1482 };
1483 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001484
1485 typedef HeaderFileInfo data_type;
1486 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001487 typedef unsigned hash_value_type;
1488 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001489
Justin Bogner25463f12014-04-18 20:27:24 +00001490 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001491 // The hash is based only on size/time of the file, so that the reader can
1492 // match even when symlinking or excess path elements ("foo/../", "../")
1493 // change the form of the name. However, complete path is still the key.
1494 return llvm::hash_combine(key.FE->getSize(),
1495 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001496 }
1497
1498 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001499 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001500 using namespace llvm::support;
1501 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001502 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001503 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001504 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001505 if (Data.isModuleHeader)
1506 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001507 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001508 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001509 }
1510
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001511 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001512 using namespace llvm::support;
1513 endian::Writer<little> LE(Out);
1514 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001515 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001516 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001517 KeyLen -= 8;
1518 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001519 }
1520
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001521 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001522 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001523 using namespace llvm::support;
1524 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001525 uint64_t Start = Out.tell(); (void)Start;
1526
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001527 unsigned char Flags = (Data.HeaderRole << 6)
1528 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001529 | (Data.isPragmaOnce << 4)
1530 | (Data.DirInfo << 2)
1531 | (Data.Resolved << 1)
1532 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001533 LE.write<uint8_t>(Flags);
1534 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001535
1536 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001537 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001538 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001539 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001540
1541 unsigned Offset = 0;
1542 if (!Data.Framework.empty()) {
1543 // If this header refers into a framework, save the framework name.
1544 llvm::StringMap<unsigned>::iterator Pos
1545 = FrameworkNameOffset.find(Data.Framework);
1546 if (Pos == FrameworkNameOffset.end()) {
1547 Offset = FrameworkStringData.size() + 1;
1548 FrameworkStringData.append(Data.Framework.begin(),
1549 Data.Framework.end());
1550 FrameworkStringData.push_back(0);
1551
1552 FrameworkNameOffset[Data.Framework] = Offset;
1553 } else
1554 Offset = Pos->second;
1555 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001556 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001557
1558 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001559 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001560 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001561 }
1562
Douglas Gregor09b69892011-02-10 17:09:37 +00001563 assert(Out.tell() - Start == DataLen && "Wrong data length");
1564 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001565
1566 const char *strings_begin() const { return FrameworkStringData.begin(); }
1567 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001568 };
1569} // end anonymous namespace
1570
1571/// \brief Write the header search block for the list of files that
1572///
1573/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001574void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001575 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001576 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1577
1578 if (FilesByUID.size() > HS.header_file_size())
1579 FilesByUID.resize(HS.header_file_size());
1580
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001581 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001582 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001583 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001584 unsigned NumHeaderSearchEntries = 0;
1585 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1586 const FileEntry *File = FilesByUID[UID];
1587 if (!File)
1588 continue;
1589
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001590 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1591 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001592 HeaderFileInfo HFI;
1593 if (!HS.tryGetFileInfo(File, HFI) ||
1594 (HFI.External && Chain) ||
1595 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001596 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001597
1598 // Turn the file name into an absolute path, if it isn't already.
1599 const char *Filename = File->getName();
1600 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1601
1602 // If we performed any translation on the file name at all, we need to
1603 // save this string, since the generator will refer to it later.
1604 if (Filename != File->getName()) {
1605 Filename = strdup(Filename);
1606 SavedStrings.push_back(Filename);
1607 }
1608
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001609 HeaderFileInfoTrait::key_type key = { File, Filename };
1610 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001611 ++NumHeaderSearchEntries;
1612 }
1613
1614 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001615 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001616 uint32_t BucketOffset;
1617 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001618 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001619 llvm::raw_svector_ostream Out(TableData);
1620 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001621 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001622 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1623 }
1624
1625 // Create a blob abbreviation
1626 using namespace llvm;
1627 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1628 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1633 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1634
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001635 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001636 RecordData Record;
1637 Record.push_back(HEADER_SEARCH_TABLE);
1638 Record.push_back(BucketOffset);
1639 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001640 Record.push_back(TableData.size());
1641 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001642 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1643
1644 // Free all of the strings we had to duplicate.
1645 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001646 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001647}
1648
Douglas Gregora7f71a92009-04-10 03:52:48 +00001649/// \brief Writes the block containing the serialized form of the
1650/// source manager.
1651///
1652/// TODO: We should probably use an on-disk hash table (stored in a
1653/// blob), indexed based on the file name, so that we only create
1654/// entries for files that we actually need. In the common case (no
1655/// errors), we probably won't have to create file entries for any of
1656/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001657void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001658 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001659 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001660 RecordData Record;
1661
Chris Lattner0910e3b2009-04-10 17:16:57 +00001662 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001663 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001664
1665 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001666 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1667 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1668 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001669 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001670
Douglas Gregor258ae542009-04-27 06:38:32 +00001671 // Write out the source location entry table. We skip the first
1672 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001673 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001674 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001675 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1676 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001677 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001678 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001679 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001680 FileID FID = FileID::get(I);
1681 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001682
Douglas Gregor258ae542009-04-27 06:38:32 +00001683 // Record the offset of this source-location entry.
1684 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1685
1686 // Figure out which record code to use.
1687 unsigned Code;
1688 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001689 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1690 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001691 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001692 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001693 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001694 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001695 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001696 Record.clear();
1697 Record.push_back(Code);
1698
Douglas Gregor925296b2011-07-19 16:10:42 +00001699 // Starting offset of this entry within this module, so skip the dummy.
1700 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001701 if (SLoc->isFile()) {
1702 const SrcMgr::FileInfo &File = SLoc->getFile();
1703 Record.push_back(File.getIncludeLoc().getRawEncoding());
1704 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1705 Record.push_back(File.hasLineDirectives());
1706
1707 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001708 if (Content->OrigEntry) {
1709 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001710 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001711
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001712 // The source location entry is a file. Emit input file ID.
1713 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1714 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001715
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001716 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001717
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001718 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001719 if (FDI != FileDeclIDs.end()) {
1720 Record.push_back(FDI->second->FirstDeclIndex);
1721 Record.push_back(FDI->second->DeclIDs.size());
1722 } else {
1723 Record.push_back(0);
1724 Record.push_back(0);
1725 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001726
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001727 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001728
1729 if (Content->BufferOverridden) {
1730 Record.clear();
1731 Record.push_back(SM_SLOC_BUFFER_BLOB);
1732 const llvm::MemoryBuffer *Buffer
1733 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1734 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1735 StringRef(Buffer->getBufferStart(),
1736 Buffer->getBufferSize() + 1));
1737 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001738 } else {
1739 // The source location entry is a buffer. The blob associated
1740 // with this entry contains the contents of the buffer.
1741
1742 // We add one to the size so that we capture the trailing NULL
1743 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1744 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001745 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001746 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001747 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001748 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001749 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001750 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001751 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001752 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001753 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001754 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001755
Douglas Gregor925296b2011-07-19 16:10:42 +00001756 if (strcmp(Name, "<built-in>") == 0) {
1757 PreloadSLocs.push_back(SLocEntryOffsets.size());
1758 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001759 }
1760 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001761 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001762 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001763 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1764 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001765 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1766 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001767
1768 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001769 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001770 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001771 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001772 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001773 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001774 }
1775 }
1776
Douglas Gregor8f45df52009-04-16 22:23:12 +00001777 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001778
1779 if (SLocEntryOffsets.empty())
1780 return;
1781
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001782 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001783 // table is used for lazily loading source-location information.
1784 using namespace llvm;
1785 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001786 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1790 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001791
Douglas Gregor258ae542009-04-27 06:38:32 +00001792 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001793 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001794 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001795 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001796 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001797
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001798 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001799 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001800 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001801
1802 // Write the line table. It depends on remapping working, so it must come
1803 // after the source location offsets.
1804 if (SourceMgr.hasLineTable()) {
1805 LineTableInfo &LineTable = SourceMgr.getLineTable();
1806
1807 Record.clear();
1808 // Emit the file names
1809 Record.push_back(LineTable.getNumFilenames());
1810 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1811 // Emit the file name
1812 const char *Filename = LineTable.getFilename(I);
1813 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1814 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1815 Record.push_back(FilenameLen);
1816 if (FilenameLen)
1817 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1818 }
1819
1820 // Emit the line entries
1821 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1822 L != LEnd; ++L) {
1823 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001824 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001825 continue;
1826
1827 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001828 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001829
1830 // Emit the line entries
1831 Record.push_back(L->second.size());
1832 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1833 LEEnd = L->second.end();
1834 LE != LEEnd; ++LE) {
1835 Record.push_back(LE->FileOffset);
1836 Record.push_back(LE->LineNo);
1837 Record.push_back(LE->FilenameID);
1838 Record.push_back((unsigned)LE->FileKind);
1839 Record.push_back(LE->IncludeOffset);
1840 }
1841 }
1842 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1843 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001844}
1845
Douglas Gregorc5046832009-04-27 18:38:38 +00001846//===----------------------------------------------------------------------===//
1847// Preprocessor Serialization
1848//===----------------------------------------------------------------------===//
1849
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001850namespace {
1851class ASTMacroTableTrait {
1852public:
1853 typedef IdentID key_type;
1854 typedef key_type key_type_ref;
1855
1856 struct Data {
1857 uint32_t MacroDirectivesOffset;
1858 };
1859
1860 typedef Data data_type;
1861 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001862 typedef unsigned hash_value_type;
1863 typedef unsigned offset_type;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001864
Justin Bogner25463f12014-04-18 20:27:24 +00001865 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001866 return llvm::hash_value(IdID);
1867 }
1868
1869 std::pair<unsigned,unsigned>
1870 static EmitKeyDataLength(raw_ostream& Out,
1871 key_type_ref Key, data_type_ref Data) {
1872 unsigned KeyLen = 4; // IdentID.
1873 unsigned DataLen = 4; // MacroDirectivesOffset.
1874 return std::make_pair(KeyLen, DataLen);
1875 }
1876
1877 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001878 using namespace llvm::support;
1879 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001880 }
1881
1882 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1883 unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001884 using namespace llvm::support;
1885 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001886 }
1887};
1888} // end anonymous namespace
1889
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001890static int compareMacroDirectives(
1891 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1892 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1893 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001894}
1895
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001896static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1897 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001898 if (MacroInfo *MI = MD->getMacroInfo())
1899 if (MI->isBuiltinMacro())
1900 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001901
1902 if (IsModule) {
Richard Smithe657bbd2014-07-18 22:13:40 +00001903 // Re-export any imported directives.
1904 // FIXME: Also ensure we re-export imported #undef directives.
1905 if (auto *DMD = dyn_cast<DefMacroDirective>(MD))
1906 if (DMD->isImported())
1907 return false;
1908
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001909 SourceLocation Loc = MD->getLocation();
1910 if (Loc.isInvalid())
1911 return true;
1912 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1913 return true;
1914 }
1915
1916 return false;
1917}
1918
Chris Lattnereeffaef2009-04-10 17:15:23 +00001919/// \brief Writes the block containing the serialized form of the
1920/// preprocessor.
1921///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001922void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001923 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1924 if (PPRec)
1925 WritePreprocessorDetail(*PPRec);
1926
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001927 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001928
Chris Lattner0af3ba12009-04-13 01:29:17 +00001929 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1930 if (PP.getCounterValue() != 0) {
1931 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001932 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001933 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001934 }
1935
1936 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001937 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001938
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001939 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001940 // FIXME: use diagnostics subsystem for localization etc.
1941 if (PP.SawDateOrTime())
1942 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001943
Douglas Gregor796d76a2010-10-20 22:00:55 +00001944
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001945 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001946 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001947
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001948 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001949 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001950 MacroDirectives;
1951 for (Preprocessor::macro_iterator
1952 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1953 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001954 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001955 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001956 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001957
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001958 // Sort the set of macro definitions that need to be serialized by the
1959 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001960 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1961 &compareMacroDirectives);
1962
Justin Bognerbb094f02014-04-18 19:57:06 +00001963 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001964
1965 // Emit the macro directives as a list and associate the offset with the
1966 // identifier they belong to.
1967 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1968 const IdentifierInfo *Name = MacroDirectives[I].first;
1969 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1970 MacroDirective *MD = MacroDirectives[I].second;
1971
1972 // If the macro or identifier need no updates, don't write the macro history
1973 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001974 // FIXME: Chain the macro history instead of re-writing it.
1975 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001976 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1977 continue;
1978
1979 // Emit the macro directives in reverse source order.
1980 for (; MD; MD = MD->getPrevious()) {
1981 if (shouldIgnoreMacro(MD, IsModule, PP))
1982 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001983
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001984 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001985 Record.push_back(MD->getKind());
1986 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1987 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1988 Record.push_back(InfoID);
1989 Record.push_back(DefMD->isImported());
1990 Record.push_back(DefMD->isAmbiguous());
1991
1992 } else if (VisibilityMacroDirective *
1993 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1994 Record.push_back(VisMD->isPublic());
1995 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001996 }
1997 if (Record.empty())
1998 continue;
1999
2000 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2001 Record.clear();
2002
2003 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2004
2005 IdentID NameID = getIdentifierRef(Name);
2006 ASTMacroTableTrait::Data data;
2007 data.MacroDirectivesOffset = MacroDirectiveOffset;
2008 Generator.insert(NameID, data);
2009 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002010
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002011 /// \brief Offsets of each of the macros into the bitstream, indexed by
2012 /// the local macro ID
2013 ///
2014 /// For each identifier that is associated with a macro, this map
2015 /// provides the offset into the bitstream where that macro is
2016 /// defined.
2017 std::vector<uint32_t> MacroOffsets;
2018
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002019 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2020 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2021 MacroInfo *MI = MacroInfosToEmit[I].MI;
2022 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002023
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002024 if (ID < FirstMacroID) {
2025 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2026 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002027 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002028
2029 // Record the local offset of this macro.
2030 unsigned Index = ID - FirstMacroID;
2031 if (Index == MacroOffsets.size())
2032 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2033 else {
2034 if (Index > MacroOffsets.size())
2035 MacroOffsets.resize(Index + 1);
2036
2037 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2038 }
2039
2040 AddIdentifierRef(Name, Record);
2041 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2042 AddSourceLocation(MI->getDefinitionLoc(), Record);
2043 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2044 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002045 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002046 unsigned Code;
2047 if (MI->isObjectLike()) {
2048 Code = PP_MACRO_OBJECT_LIKE;
2049 } else {
2050 Code = PP_MACRO_FUNCTION_LIKE;
2051
2052 Record.push_back(MI->isC99Varargs());
2053 Record.push_back(MI->isGNUVarargs());
2054 Record.push_back(MI->hasCommaPasting());
2055 Record.push_back(MI->getNumArgs());
2056 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2057 I != E; ++I)
2058 AddIdentifierRef(*I, Record);
2059 }
2060
2061 // If we have a detailed preprocessing record, record the macro definition
2062 // ID that corresponds to this macro.
2063 if (PPRec)
2064 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2065
2066 Stream.EmitRecord(Code, Record);
2067 Record.clear();
2068
2069 // Emit the tokens array.
2070 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2071 // Note that we know that the preprocessor does not have any annotation
2072 // tokens in it because they are created by the parser, and thus can't
2073 // be in a macro definition.
2074 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002075 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002076 Stream.EmitRecord(PP_TOKEN, Record);
2077 Record.clear();
2078 }
2079 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002080 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002081
Douglas Gregor92a96f52011-02-08 21:58:10 +00002082 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002083
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002084 // Create the on-disk hash table in a buffer.
2085 SmallString<4096> MacroTable;
2086 uint32_t BucketOffset;
2087 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002088 using namespace llvm::support;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002089 llvm::raw_svector_ostream Out(MacroTable);
2090 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002091 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002092 BucketOffset = Generator.Emit(Out);
2093 }
2094
2095 // Write the macro table
2096 using namespace llvm;
2097 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2098 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2099 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2101 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2102
2103 Record.push_back(MACRO_TABLE);
2104 Record.push_back(BucketOffset);
2105 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2106 Record.clear();
2107
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002108 // Write the offsets table for macro IDs.
2109 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002110 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002111 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2115
2116 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2117 Record.clear();
2118 Record.push_back(MACRO_OFFSET);
2119 Record.push_back(MacroOffsets.size());
2120 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2121 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2122 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002123}
2124
2125void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002126 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002127 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002128
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002129 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002130
Douglas Gregor92a96f52011-02-08 21:58:10 +00002131 // Enter the preprocessor block.
2132 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002133
Douglas Gregoraae92242010-03-19 21:51:54 +00002134 // If the preprocessor has a preprocessing record, emit it.
2135 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002136 using namespace llvm;
2137
2138 // Set up the abbreviation for
2139 unsigned InclusionAbbrev = 0;
2140 {
2141 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2142 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2148 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2149 }
2150
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002151 unsigned FirstPreprocessorEntityID
2152 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2153 + NUM_PREDEF_PP_ENTITY_IDS;
2154 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002155 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002156 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2157 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002158 E != EEnd;
2159 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002160 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002161
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002162 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2163 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002164
Douglas Gregor92a96f52011-02-08 21:58:10 +00002165 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002166 // Record this macro definition's ID.
2167 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002168
Douglas Gregor92a96f52011-02-08 21:58:10 +00002169 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002170 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2171 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002172 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002173
Chandler Carrutha88a22182011-07-14 08:20:46 +00002174 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002175 Record.push_back(ME->isBuiltinMacro());
2176 if (ME->isBuiltinMacro())
2177 AddIdentifierRef(ME->getName(), Record);
2178 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002179 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002180 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002181 continue;
2182 }
2183
2184 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2185 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002186 Record.push_back(ID->getFileName().size());
2187 Record.push_back(ID->wasInQuotes());
2188 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002189 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002190 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002191 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002192 // Check that the FileEntry is not null because it was not resolved and
2193 // we create a PCH even with compiler errors.
2194 if (ID->getFile())
2195 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002196 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2197 continue;
2198 }
2199
2200 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2201 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002202 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002203
Douglas Gregoraae92242010-03-19 21:51:54 +00002204 // Write the offsets table for the preprocessing record.
2205 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002206 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2207
Douglas Gregoraae92242010-03-19 21:51:54 +00002208 // Write the offsets table for identifier IDs.
2209 using namespace llvm;
2210 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002211 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002214 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002215
Douglas Gregoraae92242010-03-19 21:51:54 +00002216 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002217 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002218 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002219 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2220 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002221 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002222}
2223
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002224unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2225 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2226 if (Known != SubmoduleIDs.end())
2227 return Known->second;
2228
2229 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2230}
2231
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002232unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2233 if (!Mod)
2234 return 0;
2235
2236 llvm::DenseMap<Module *, unsigned>::const_iterator
2237 Known = SubmoduleIDs.find(Mod);
2238 if (Known != SubmoduleIDs.end())
2239 return Known->second;
2240
2241 return 0;
2242}
2243
Douglas Gregor253eefe2011-12-01 00:59:36 +00002244/// \brief Compute the number of modules within the given tree (including the
2245/// given module).
2246static unsigned getNumberOfModules(Module *Mod) {
2247 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002248 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2249 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002250 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002251 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002252
2253 return ChildModules + 1;
2254}
2255
Douglas Gregorde3ef502011-11-30 23:21:26 +00002256void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002257 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002258 // FIXME: This feels like it belongs somewhere else, but there are no
2259 // other consumers of this information.
2260 SourceManager &SrcMgr = PP->getSourceManager();
2261 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002262 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002263 if (Module *ImportedFrom
2264 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2265 SrcMgr))) {
2266 ImportedFrom->Imports.push_back(I->getImportedModule());
2267 }
2268 }
2269
Douglas Gregor69021972011-11-30 17:33:56 +00002270 // Enter the submodule description block.
2271 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2272
2273 // Write the abbreviations needed for the submodules block.
2274 using namespace llvm;
2275 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2276 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2288 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2289
2290 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002291 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2293 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2294
2295 Abbrev = new BitCodeAbbrev();
2296 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2298 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002299
2300 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002301 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2303 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2304
2305 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002306 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2308 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2309
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002310 Abbrev = new BitCodeAbbrev();
2311 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002314 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2315
Douglas Gregor59527662012-10-15 06:28:11 +00002316 Abbrev = new BitCodeAbbrev();
2317 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2319 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2320
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002321 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002322 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2324 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2325
2326 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002327 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2330 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2331
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002332 Abbrev = new BitCodeAbbrev();
2333 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2335 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2336
Douglas Gregorfb912652013-03-20 21:10:35 +00002337 Abbrev = new BitCodeAbbrev();
2338 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2341 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2342
Douglas Gregor253eefe2011-12-01 00:59:36 +00002343 // Write the submodule metadata block.
2344 RecordData Record;
2345 Record.push_back(getNumberOfModules(WritingModule));
2346 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2347 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2348
Douglas Gregor69021972011-11-30 17:33:56 +00002349 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002350 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002351 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002352 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002353 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002354 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002355 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002356
2357 // Emit the definition of the block.
2358 Record.clear();
2359 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002360 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002361 if (Mod->Parent) {
2362 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2363 Record.push_back(SubmoduleIDs[Mod->Parent]);
2364 } else {
2365 Record.push_back(0);
2366 }
2367 Record.push_back(Mod->IsFramework);
2368 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002369 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002370 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002371 Record.push_back(Mod->InferSubmodules);
2372 Record.push_back(Mod->InferExplicitSubmodules);
2373 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002374 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002375 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2376
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002377 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002378 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002379 Record.clear();
2380 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002381 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002382 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002383 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002384 }
2385
Douglas Gregor69021972011-11-30 17:33:56 +00002386 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002387 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002388 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002389 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002390 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002391 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002392 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2393 Record.clear();
2394 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2395 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2396 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002397 }
2398
2399 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002400 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002401 Record.clear();
2402 Record.push_back(SUBMODULE_HEADER);
2403 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002404 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002405 }
Douglas Gregor59527662012-10-15 06:28:11 +00002406 // Emit the excluded headers.
2407 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2408 Record.clear();
2409 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2410 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2411 Mod->ExcludedHeaders[I]->getName());
2412 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002413 // Emit the private headers.
2414 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2415 Record.clear();
2416 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2417 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2418 Mod->PrivateHeaders[I]->getName());
2419 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002420 ArrayRef<const FileEntry *>
2421 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2422 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002423 Record.clear();
2424 Record.push_back(SUBMODULE_TOPHEADER);
2425 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002426 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002427 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002428
2429 // Emit the imports.
2430 if (!Mod->Imports.empty()) {
2431 Record.clear();
2432 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002433 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002434 assert(ImportedID && "Unknown submodule!");
2435 Record.push_back(ImportedID);
2436 }
2437 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2438 }
2439
Douglas Gregor24bb9232011-12-02 18:58:38 +00002440 // Emit the exports.
2441 if (!Mod->Exports.empty()) {
2442 Record.clear();
2443 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002444 if (Module *Exported = Mod->Exports[I].getPointer()) {
2445 unsigned ExportedID = SubmoduleIDs[Exported];
2446 assert(ExportedID > 0 && "Unknown submodule ID?");
2447 Record.push_back(ExportedID);
2448 } else {
2449 Record.push_back(0);
2450 }
2451
Douglas Gregor24bb9232011-12-02 18:58:38 +00002452 Record.push_back(Mod->Exports[I].getInt());
2453 }
2454 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2455 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002456
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002457 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2458 // Might be unnecessary as use declarations are only used to build the
2459 // module itself.
2460
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002461 // Emit the link libraries.
2462 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2463 Record.clear();
2464 Record.push_back(SUBMODULE_LINK_LIBRARY);
2465 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2466 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2467 Mod->LinkLibraries[I].Library);
2468 }
2469
Douglas Gregorfb912652013-03-20 21:10:35 +00002470 // Emit the conflicts.
2471 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2472 Record.clear();
2473 Record.push_back(SUBMODULE_CONFLICT);
2474 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2475 assert(OtherID && "Unknown submodule!");
2476 Record.push_back(OtherID);
2477 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2478 Mod->Conflicts[I].Message);
2479 }
2480
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002481 // Emit the configuration macros.
2482 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2483 Record.clear();
2484 Record.push_back(SUBMODULE_CONFIG_MACRO);
2485 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2486 Mod->ConfigMacros[I]);
2487 }
2488
Douglas Gregor69021972011-11-30 17:33:56 +00002489 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002490 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2491 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002492 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002493 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002494 }
2495
2496 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002497
2498 assert((NextSubmoduleID - FirstSubmoduleID
2499 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002500}
2501
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002502serialization::SubmoduleID
2503ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002504 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002505 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002506
2507 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002508 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002509 Module *OwningMod
2510 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002511 if (!OwningMod)
2512 return 0;
2513
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002514 // Check whether this submodule is part of our own module.
2515 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002516 return 0;
2517
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002518 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002519}
2520
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002521void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2522 bool isModule) {
2523 // Make sure set diagnostic pragmas don't affect the translation unit that
2524 // imports the module.
2525 // FIXME: Make diagnostic pragma sections work properly with modules.
2526 if (isModule)
2527 return;
2528
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002529 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2530 DiagStateIDMap;
2531 unsigned CurrID = 0;
2532 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002533 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002534 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002535 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2536 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002537 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002538 if (point.Loc.isInvalid())
2539 continue;
2540
2541 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002542 unsigned &DiagStateID = DiagStateIDMap[point.State];
2543 Record.push_back(DiagStateID);
2544
2545 if (DiagStateID == 0) {
2546 DiagStateID = ++CurrID;
2547 for (DiagnosticsEngine::DiagState::const_iterator
2548 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2549 if (I->second.isPragma()) {
2550 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002551 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002552 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002553 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002554 Record.push_back(-1); // mark the end of the diag/map pairs for this
2555 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002556 }
2557 }
2558
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002559 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002560 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002561}
2562
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002563void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2564 if (CXXBaseSpecifiersOffsets.empty())
2565 return;
2566
2567 RecordData Record;
2568
2569 // Create a blob abbreviation for the C++ base specifiers offsets.
2570 using namespace llvm;
2571
2572 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2573 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2576 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2577
Douglas Gregorc27b2872011-08-04 00:01:48 +00002578 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002579 Record.clear();
2580 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2581 Record.push_back(CXXBaseSpecifiersOffsets.size());
2582 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002583 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002584}
2585
Douglas Gregorc5046832009-04-27 18:38:38 +00002586//===----------------------------------------------------------------------===//
2587// Type Serialization
2588//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002589
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002590/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002591void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002592 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002593 if (Idx.getIndex() == 0) // we haven't seen this type before.
2594 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002595
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002596 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002597
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002598 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002599 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002600 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002601 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002602 else if (TypeOffsets.size() < Index) {
2603 TypeOffsets.resize(Index + 1);
2604 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002605 }
2606
2607 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002608
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002609 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002610 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002611
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002612 if (T.hasLocalNonFastQualifiers()) {
2613 Qualifiers Qs = T.getLocalQualifiers();
2614 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002615 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002616 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002617 } else {
2618 switch (T->getTypeClass()) {
2619 // For all of the concrete, non-dependent types, call the
2620 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002621#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002622 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002623#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002624#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002625 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002626 }
2627
2628 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002629 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002630
2631 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002632 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002633}
2634
Douglas Gregorc5046832009-04-27 18:38:38 +00002635//===----------------------------------------------------------------------===//
2636// Declaration Serialization
2637//===----------------------------------------------------------------------===//
2638
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002639/// \brief Write the block containing all of the declaration IDs
2640/// lexically declared within the given DeclContext.
2641///
2642/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2643/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002644uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002645 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002646 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002647 return 0;
2648
Douglas Gregor8f45df52009-04-16 22:23:12 +00002649 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002650 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002651 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002652 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002653 for (const auto *D : DC->decls())
2654 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002655
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002656 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002657 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002658 return Offset;
2659}
2660
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002661void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002662 using namespace llvm;
2663 RecordData Record;
2664
2665 // Write the type offsets array
2666 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002667 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002668 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002670 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2671 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2672 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002673 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002674 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002675 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002676 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002677
2678 // Write the declaration offsets array
2679 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002680 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002681 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002682 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002683 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2684 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2685 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002686 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002687 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002688 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002689 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002690}
2691
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002692void ASTWriter::WriteFileDeclIDsMap() {
2693 using namespace llvm;
2694 RecordData Record;
2695
2696 // Join the vectors of DeclIDs from all files.
2697 SmallVector<DeclID, 256> FileSortedIDs;
2698 for (FileDeclIDsTy::iterator
2699 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2700 DeclIDInFileInfo &Info = *FI->second;
2701 Info.FirstDeclIndex = FileSortedIDs.size();
2702 for (LocDeclIDsTy::iterator
2703 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2704 FileSortedIDs.push_back(DI->second);
2705 }
2706
2707 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2708 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002710 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2711 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2712 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002713 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002714 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2715}
2716
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002717void ASTWriter::WriteComments() {
2718 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002719 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002720 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002721 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2722 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002723 I != E; ++I) {
2724 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002725 AddSourceRange((*I)->getSourceRange(), Record);
2726 Record.push_back((*I)->getKind());
2727 Record.push_back((*I)->isTrailingComment());
2728 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002729 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2730 }
2731 Stream.ExitBlock();
2732}
2733
Douglas Gregorc5046832009-04-27 18:38:38 +00002734//===----------------------------------------------------------------------===//
2735// Global Method Pool and Selector Serialization
2736//===----------------------------------------------------------------------===//
2737
Douglas Gregore84a9da2009-04-20 20:36:09 +00002738namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002739// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002740class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002741 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002742
2743public:
2744 typedef Selector key_type;
2745 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002746
Sebastian Redl834bb972010-08-04 17:20:04 +00002747 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002748 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002749 ObjCMethodList Instance, Factory;
2750 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002751 typedef const data_type& data_type_ref;
2752
Justin Bogner25463f12014-04-18 20:27:24 +00002753 typedef unsigned hash_value_type;
2754 typedef unsigned offset_type;
2755
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002756 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002757
Justin Bogner25463f12014-04-18 20:27:24 +00002758 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002759 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002760 }
Mike Stump11289f42009-09-09 15:08:12 +00002761
2762 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002763 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002764 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002765 using namespace llvm::support;
2766 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002767 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002768 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002769 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2770 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002771 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002772 if (Method->Method)
2773 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002774 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002775 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002776 if (Method->Method)
2777 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002778 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002779 return std::make_pair(KeyLen, DataLen);
2780 }
Mike Stump11289f42009-09-09 15:08:12 +00002781
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002782 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002783 using namespace llvm::support;
2784 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002785 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002786 assert((Start >> 32) == 0 && "Selector key offset too large");
2787 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002788 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002789 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002790 if (N == 0)
2791 N = 1;
2792 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002793 LE.write<uint32_t>(
2794 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002795 }
Mike Stump11289f42009-09-09 15:08:12 +00002796
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002797 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002798 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002799 using namespace llvm::support;
2800 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002801 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002802 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002803 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002804 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002805 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002806 if (Method->Method)
2807 ++NumInstanceMethods;
2808
2809 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002810 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002811 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002812 if (Method->Method)
2813 ++NumFactoryMethods;
2814
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002815 unsigned InstanceBits = Methods.Instance.getBits();
2816 assert(InstanceBits < 4);
2817 unsigned NumInstanceMethodsAndBits =
2818 (NumInstanceMethods << 2) | InstanceBits;
2819 unsigned FactoryBits = Methods.Factory.getBits();
2820 assert(FactoryBits < 4);
2821 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Justin Bognere1c147c2014-03-28 22:03:19 +00002822 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2823 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002824 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002825 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002826 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002827 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002828 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002829 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002830 if (Method->Method)
Justin Bognere1c147c2014-03-28 22:03:19 +00002831 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002832
2833 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002834 }
2835};
2836} // end anonymous namespace
2837
Sebastian Redla19a67f2010-08-03 21:58:15 +00002838/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002839///
2840/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002841/// in an on-disk hash table indexed by the selector. The hash table also
2842/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002843void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002844 using namespace llvm;
2845
Sebastian Redla19a67f2010-08-03 21:58:15 +00002846 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002847 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002848 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002849 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002850 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002851 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002852 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002853 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002854
Sebastian Redla19a67f2010-08-03 21:58:15 +00002855 // Create the on-disk hash table representation. We walk through every
2856 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002857 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002858 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002859 I = SelectorIDs.begin(), E = SelectorIDs.end();
2860 I != E; ++I) {
2861 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002862 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002863 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002864 I->second,
2865 ObjCMethodList(),
2866 ObjCMethodList()
2867 };
2868 if (F != SemaRef.MethodPool.end()) {
2869 Data.Instance = F->second.first;
2870 Data.Factory = F->second.second;
2871 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002872 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002873 // changed.
2874 if (Chain && I->second < FirstSelectorID) {
2875 // Selector already exists. Did it change?
2876 bool changed = false;
2877 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002878 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002879 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002880 changed = true;
2881 }
2882 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002883 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002884 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002885 changed = true;
2886 }
2887 if (!changed)
2888 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002889 } else if (Data.Instance.Method || Data.Factory.Method) {
2890 // A new method pool entry.
2891 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002892 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002893 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002894 }
2895
Douglas Gregorc78d3462009-04-24 21:10:55 +00002896 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002897 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002898 uint32_t BucketOffset;
2899 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002900 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002901 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002902 llvm::raw_svector_ostream Out(MethodPool);
2903 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002904 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002905 BucketOffset = Generator.Emit(Out, Trait);
2906 }
2907
2908 // Create a blob abbreviation
2909 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002910 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2914 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2915
Douglas Gregor95c13f52009-04-25 17:48:32 +00002916 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002917 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002918 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002919 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002920 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002921 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002922
2923 // Create a blob abbreviation for the selector table offsets.
2924 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002925 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002926 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2929 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2930
2931 // Write the selector offsets table.
2932 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002933 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002934 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002935 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002936 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002937 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002938 }
2939}
2940
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002941/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002942void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002943 using namespace llvm;
2944 if (SemaRef.ReferencedSelectors.empty())
2945 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002946
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002947 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002948
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002949 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002950 // very tricky to fix, and given that @selector shouldn't really appear in
2951 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002952 for (DenseMap<Selector, SourceLocation>::iterator S =
2953 SemaRef.ReferencedSelectors.begin(),
2954 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2955 Selector Sel = (*S).first;
2956 SourceLocation Loc = (*S).second;
2957 AddSelectorRef(Sel, Record);
2958 AddSourceLocation(Loc, Record);
2959 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002960 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002961}
2962
Douglas Gregorc5046832009-04-27 18:38:38 +00002963//===----------------------------------------------------------------------===//
2964// Identifier Table Serialization
2965//===----------------------------------------------------------------------===//
2966
Douglas Gregorc78d3462009-04-24 21:10:55 +00002967namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002968class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002969 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002970 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002971 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002972 bool IsModule;
2973
Douglas Gregor1d583f22009-04-28 21:18:29 +00002974 /// \brief Determines whether this is an "interesting" identifier
2975 /// that needs a full IdentifierInfo structure written into the hash
2976 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002977 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002978 if (II->isPoisoned() ||
2979 II->isExtensionToken() ||
2980 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002981 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002982 II->getFETokenInfo<void>())
2983 return true;
2984
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002985 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002986 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002987
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002988 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002989 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002990 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002991
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002992 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2993 if (!IsModule)
2994 return !shouldIgnoreMacro(Macro, IsModule, PP);
2995 SubmoduleID ModID;
2996 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2997 return true;
2998 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002999
3000 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003001 }
3002
Richard Smith49f906a2014-03-01 00:08:04 +00003003 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
3004
3005 MacroDirective *
3006 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003007 ModID = 0;
Richard Smith49f906a2014-03-01 00:08:04 +00003008 llvm::SmallVector<SubmoduleID, 1> Overridden;
3009 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
3010 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
3011 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003012 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003013 }
3014
Richard Smith49f906a2014-03-01 00:08:04 +00003015 MacroDirective *
3016 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
3017 OverriddenList &Overridden) {
3018 if (MacroDirective *NextMD =
3019 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
3020 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
3021 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003022 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003023 }
3024
3025 /// \brief Traverses the macro directives history and returns the latest
Richard Smith49f906a2014-03-01 00:08:04 +00003026 /// public macro definition or undefinition that is not in ModID.
3027 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003028 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00003029 /// ModID is updated to the module containing the returned directive.
3030 ///
3031 /// FIXME: This process breaks down if a module defines a macro, imports
3032 /// another submodule that changes the macro, then changes the
3033 /// macro again itself.
3034 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
3035 SubmoduleID &ModID,
3036 OverriddenList &Overridden) {
Richard Smith57721ac2014-07-21 04:10:40 +00003037 Overridden.clear();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003038 if (!MD)
Craig Toppera13603a2014-05-22 05:54:18 +00003039 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003040
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003041 SubmoduleID OrigModID = ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003042 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003043 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003044 SubmoduleID ThisModID = getSubmoduleID(MD);
3045 if (ThisModID == 0) {
Richard Smith49f906a2014-03-01 00:08:04 +00003046 IsPublic = Optional<bool>();
Richard Smith57721ac2014-07-21 04:10:40 +00003047
3048 // If we have no directive location, this macro was installed when
3049 // finalizing the ASTReader.
3050 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
3051 if (DefMD->getInfo()->getOwningModuleID())
3052 return MD;
3053 // Skip imports that only produce #undefs for now.
3054 // FIXME: We should still re-export them!
3055
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003056 continue;
3057 }
Richard Smith49f906a2014-03-01 00:08:04 +00003058 if (ThisModID != ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003059 ModID = ThisModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003060 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003061 }
Richard Smith49f906a2014-03-01 00:08:04 +00003062
3063 // If this is a definition from a submodule import, that submodule's
3064 // definition is overridden by the definition or undefinition that we
3065 // started with.
3066 // FIXME: This should only apply to macros defined in OrigModID.
3067 // We can't do that currently, because a #include of a different submodule
3068 // of the same module just leaks through macros instead of providing new
3069 // DefMacroDirectives for them.
Richard Smith9d100862014-03-06 03:16:27 +00003070 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3071 // Figure out which submodule the macro was originally defined within.
3072 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
3073 if (!SourceID) {
3074 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3075 if (DefLoc == MD->getLocation())
3076 SourceID = ThisModID;
3077 else
3078 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3079 }
Richard Smith57721ac2014-07-21 04:10:40 +00003080 if (OrigModID && SourceID != OrigModID)
Richard Smith49f906a2014-03-01 00:08:04 +00003081 Overridden.push_back(SourceID);
Richard Smith9d100862014-03-06 03:16:27 +00003082 }
Richard Smith49f906a2014-03-01 00:08:04 +00003083
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003084 // We are looking for a definition in a different submodule than the one
3085 // that we started with. If a submodule has re-definitions of the same
3086 // macro, only the last definition will be used as the "exported" one.
3087 if (ModID == OrigModID)
3088 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003089
Richard Smith49f906a2014-03-01 00:08:04 +00003090 // The latest visibility directive for a name in a submodule affects all
3091 // the directives that come before it.
3092 if (VisibilityMacroDirective *VisMD =
3093 dyn_cast<VisibilityMacroDirective>(MD)) {
3094 if (!IsPublic.hasValue())
3095 IsPublic = VisMD->isPublic();
3096 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3097 // FIXME: If we find an imported macro, we should include its list of
3098 // overrides in our export.
3099 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003100 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003101 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003102
Craig Toppera13603a2014-05-22 05:54:18 +00003103 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003104 }
3105
3106 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Richard Smith57721ac2014-07-21 04:10:40 +00003107 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003108 }
3109
Douglas Gregore84a9da2009-04-20 20:36:09 +00003110public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003111 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003112 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003113
Sebastian Redl539c5062010-08-18 23:57:32 +00003114 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003115 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003116
Justin Bogner25463f12014-04-18 20:27:24 +00003117 typedef unsigned hash_value_type;
3118 typedef unsigned offset_type;
3119
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003120 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3121 IdentifierResolver &IdResolver, bool IsModule)
3122 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003123
Justin Bogner25463f12014-04-18 20:27:24 +00003124 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003125 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003126 }
Mike Stump11289f42009-09-09 15:08:12 +00003127
3128 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003129 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003130 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003131 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Craig Toppera13603a2014-05-22 05:54:18 +00003132 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003133 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003134 DataLen += 2; // 2 bytes for builtin ID
3135 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003136 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003137 DataLen += 4; // MacroDirectives offset.
3138 if (IsModule) {
3139 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003140 llvm::SmallVector<SubmoduleID, 4> Overridden;
3141 for (MacroDirective *
3142 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3143 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3144 // Previous macro's overrides.
3145 if (!Overridden.empty())
3146 DataLen += 4 * (1 + Overridden.size());
3147 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003148 }
Richard Smith49f906a2014-03-01 00:08:04 +00003149 // Previous macro's overrides.
3150 if (!Overridden.empty())
3151 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003152 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003153 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003154 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003155
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003156 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3157 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003158 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003159 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003160 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003161 using namespace llvm::support;
3162 endian::Writer<little> LE(Out);
3163
3164 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003165 // We emit the key length after the data length so that every
3166 // string is preceded by a 16-bit length. This matches the PTH
3167 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003168 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003169 return std::make_pair(KeyLen, DataLen);
3170 }
Mike Stump11289f42009-09-09 15:08:12 +00003171
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003172 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003173 unsigned KeyLen) {
3174 // Record the location of the key data. This is used when generating
3175 // the mapping from persistent IDs to strings.
3176 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003177 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003178 }
Mike Stump11289f42009-09-09 15:08:12 +00003179
Richard Smith49f906a2014-03-01 00:08:04 +00003180 static void emitMacroOverrides(raw_ostream &Out,
Craig Topper00bbdcf2014-06-28 23:22:23 +00003181 ArrayRef<SubmoduleID> Overridden) {
Richard Smith49f906a2014-03-01 00:08:04 +00003182 if (!Overridden.empty()) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003183 using namespace llvm::support;
3184 endian::Writer<little> LE(Out);
3185 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Richard Smith49f906a2014-03-01 00:08:04 +00003186 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003187 LE.write<uint32_t>(Overridden[I]);
Richard Smith49f906a2014-03-01 00:08:04 +00003188 }
3189 }
3190
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003191 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003192 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003193 using namespace llvm::support;
3194 endian::Writer<little> LE(Out);
Craig Toppera13603a2014-05-22 05:54:18 +00003195 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003196 if (!isInterestingIdentifier(II, Macro)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003197 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003198 return;
3199 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003200
Justin Bognere1c147c2014-03-28 22:03:19 +00003201 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003202 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3203 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003204 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003205 Bits = 0;
3206 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003207 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003208 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003209 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3210 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003211 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003212 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003213 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003214
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003215 if (HadMacroDefinition) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003216 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003217 if (IsModule) {
3218 // Write the IDs of macros coming from different submodules.
3219 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003220 llvm::SmallVector<SubmoduleID, 4> Overridden;
3221 for (MacroDirective *
3222 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3223 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3224 MacroID InfoID = 0;
3225 emitMacroOverrides(Out, Overridden);
3226 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3227 InfoID = Writer.getMacroID(DefMD->getInfo());
3228 assert(InfoID);
Justin Bognere1c147c2014-03-28 22:03:19 +00003229 LE.write<uint32_t>(InfoID << 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003230 } else {
3231 assert(isa<UndefMacroDirective>(MD));
Justin Bognere1c147c2014-03-28 22:03:19 +00003232 LE.write<uint32_t>((ModID << 1) | 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003233 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003234 }
Richard Smith49f906a2014-03-01 00:08:04 +00003235 emitMacroOverrides(Out, Overridden);
Justin Bognere1c147c2014-03-28 22:03:19 +00003236 LE.write<uint32_t>(0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003237 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003238 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003239
Douglas Gregora868bbd2009-04-21 22:25:48 +00003240 // Emit the declaration IDs in reverse order, because the
3241 // IdentifierResolver provides the declarations as they would be
3242 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003243 // "stat"), but the ASTReader adds declarations to the end of the list
3244 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003245 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003246 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3247 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003248 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003249 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003250 D != DEnd; ++D)
Justin Bognere1c147c2014-03-28 22:03:19 +00003251 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003252 }
3253
3254 /// \brief Returns the most recent local decl or the given decl if there are
3255 /// no local ones. The given decl is assumed to be the most recent one.
3256 Decl *getMostRecentLocalDecl(Decl *Orig) {
3257 // The only way a "from AST file" decl would be more recent from a local one
3258 // is if it came from a module.
3259 if (!PP.getLangOpts().Modules)
3260 return Orig;
3261
3262 // Look for a local in the decl chain.
3263 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3264 if (!D->isFromASTFile())
3265 return D;
3266 // If we come up a decl from a (chained-)PCH stop since we won't find a
3267 // local one.
3268 if (D->getOwningModuleID() == 0)
3269 break;
3270 }
3271
3272 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003273 }
3274};
3275} // end anonymous namespace
3276
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003277/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003278///
3279/// The identifier table consists of a blob containing string data
3280/// (the actual identifiers themselves) and a separate "offsets" index
3281/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003282void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3283 IdentifierResolver &IdResolver,
3284 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003285 using namespace llvm;
3286
3287 // Create and write out the blob that contains the identifier
3288 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003289 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003290 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003291 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003292
Douglas Gregore6648fb2009-04-28 20:33:11 +00003293 // Look for any identifiers that were named while processing the
3294 // headers, but are otherwise not needed. We add these to the hash
3295 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003296 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003297 // file.
3298 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3299 IDEnd = PP.getIdentifierTable().end();
3300 ID != IDEnd; ++ID)
3301 getIdentifierRef(ID->second);
3302
Sebastian Redlff4a2952010-07-23 23:49:55 +00003303 // Create the on-disk hash table representation. We only store offsets
3304 // for identifiers that appear here for the first time.
3305 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003306 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003307 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3308 ID != IDEnd; ++ID) {
3309 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003310 if (!Chain || !ID->first->isFromAST() ||
3311 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003312 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003313 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003314 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003315
Douglas Gregore84a9da2009-04-20 20:36:09 +00003316 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003317 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003318 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003319 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003320 using namespace llvm::support;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003321 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003322 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003323 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003324 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003325 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003326 }
3327
3328 // Create a blob abbreviation
3329 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003330 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003333 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003334
3335 // Write the identifier table
3336 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003337 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003338 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003339 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003340 }
3341
3342 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003343 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003344 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3348 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3349
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003350#ifndef NDEBUG
3351 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3352 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3353#endif
3354
Douglas Gregor0e149972009-04-25 19:10:14 +00003355 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003356 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003357 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003358 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003359 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003360 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003361}
3362
Douglas Gregorc5046832009-04-27 18:38:38 +00003363//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003364// DeclContext's Name Lookup Table Serialization
3365//===----------------------------------------------------------------------===//
3366
3367namespace {
3368// Trait used for the on-disk hash table used in the method pool.
3369class ASTDeclContextNameLookupTrait {
3370 ASTWriter &Writer;
3371
3372public:
3373 typedef DeclarationName key_type;
3374 typedef key_type key_type_ref;
3375
3376 typedef DeclContext::lookup_result data_type;
3377 typedef const data_type& data_type_ref;
3378
Justin Bogner25463f12014-04-18 20:27:24 +00003379 typedef unsigned hash_value_type;
3380 typedef unsigned offset_type;
3381
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003382 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3383
Justin Bogner25463f12014-04-18 20:27:24 +00003384 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003385 llvm::FoldingSetNodeID ID;
3386 ID.AddInteger(Name.getNameKind());
3387
3388 switch (Name.getNameKind()) {
3389 case DeclarationName::Identifier:
3390 ID.AddString(Name.getAsIdentifierInfo()->getName());
3391 break;
3392 case DeclarationName::ObjCZeroArgSelector:
3393 case DeclarationName::ObjCOneArgSelector:
3394 case DeclarationName::ObjCMultiArgSelector:
3395 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3396 break;
3397 case DeclarationName::CXXConstructorName:
3398 case DeclarationName::CXXDestructorName:
3399 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003400 break;
3401 case DeclarationName::CXXOperatorName:
3402 ID.AddInteger(Name.getCXXOverloadedOperator());
3403 break;
3404 case DeclarationName::CXXLiteralOperatorName:
3405 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3406 case DeclarationName::CXXUsingDirective:
3407 break;
3408 }
3409
3410 return ID.ComputeHash();
3411 }
3412
3413 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003414 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003415 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003416 using namespace llvm::support;
3417 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003418 unsigned KeyLen = 1;
3419 switch (Name.getNameKind()) {
3420 case DeclarationName::Identifier:
3421 case DeclarationName::ObjCZeroArgSelector:
3422 case DeclarationName::ObjCOneArgSelector:
3423 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003424 case DeclarationName::CXXLiteralOperatorName:
3425 KeyLen += 4;
3426 break;
3427 case DeclarationName::CXXOperatorName:
3428 KeyLen += 1;
3429 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003430 case DeclarationName::CXXConstructorName:
3431 case DeclarationName::CXXDestructorName:
3432 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003433 case DeclarationName::CXXUsingDirective:
3434 break;
3435 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003436 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003437
3438 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003439 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003440 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003441
3442 return std::make_pair(KeyLen, DataLen);
3443 }
3444
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003445 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003446 using namespace llvm::support;
3447 endian::Writer<little> LE(Out);
3448 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003449 switch (Name.getNameKind()) {
3450 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003451 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003452 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003453 case DeclarationName::ObjCZeroArgSelector:
3454 case DeclarationName::ObjCOneArgSelector:
3455 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003456 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003457 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003458 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003459 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3460 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003461 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003462 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003463 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003464 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003465 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003466 case DeclarationName::CXXConstructorName:
3467 case DeclarationName::CXXDestructorName:
3468 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003469 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003470 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003471 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003472
3473 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003474 }
3475
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003476 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003477 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003478 using namespace llvm::support;
3479 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003480 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003481 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003482 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3483 I != E; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003484 LE.write<uint32_t>(Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003485
3486 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3487 }
3488};
3489} // end anonymous namespace
3490
Richard Smithcd45dbc2014-04-19 03:48:30 +00003491template<typename Visitor>
3492static void visitLocalLookupResults(const DeclContext *ConstDC,
3493 bool NeedToReconcileExternalVisibleStorage,
3494 Visitor AddLookupResult) {
3495 // FIXME: We need to build the lookups table, which is logically const.
3496 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Richard Smith961eae52014-03-25 01:14:22 +00003497 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3498
Richard Smith961eae52014-03-25 01:14:22 +00003499 SmallVector<DeclarationName, 16> ExternalNames;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003500 for (auto &Lookup : *DC->buildLookup()) {
Richard Smith961eae52014-03-25 01:14:22 +00003501 if (Lookup.second.hasExternalDecls() ||
Richard Smithcd45dbc2014-04-19 03:48:30 +00003502 NeedToReconcileExternalVisibleStorage) {
Richard Smith961eae52014-03-25 01:14:22 +00003503 // We don't know for sure what declarations are found by this name,
3504 // because the external source might have a different set from the set
3505 // that are in the lookup map, and we can't update it now without
3506 // risking invalidating our lookup iterator. So add it to a queue to
3507 // deal with later.
3508 ExternalNames.push_back(Lookup.first);
3509 continue;
3510 }
3511
3512 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3513 }
3514
3515 // Add the names we needed to defer. Note, this shouldn't add any new decls
3516 // to the list we need to serialize: any new declarations we find here should
3517 // be imported from an external source.
3518 // FIXME: What if the external source isn't an ASTReader?
3519 for (const auto &Name : ExternalNames)
Richard Smithcd45dbc2014-04-19 03:48:30 +00003520 AddLookupResult(Name, DC->lookup(Name));
3521}
3522
3523void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
3524 if (UpdatedDeclContexts.insert(DC) && WritingAST) {
3525 // Ensure we emit all the visible declarations.
3526 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3527 [&](DeclarationName Name,
3528 DeclContext::lookup_const_result Result) {
3529 for (auto *Decl : Result)
3530 GetDeclRef(Decl);
3531 });
3532 }
3533}
3534
3535uint32_t
3536ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3537 llvm::SmallVectorImpl<char> &LookupTable) {
3538 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3539
3540 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3541 Generator;
3542 ASTDeclContextNameLookupTrait Trait(*this);
3543
3544 // Create the on-disk hash table representation.
3545 DeclarationName ConstructorName;
3546 DeclarationName ConversionName;
3547 SmallVector<NamedDecl *, 8> ConstructorDecls;
3548 SmallVector<NamedDecl *, 4> ConversionDecls;
3549
3550 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3551 [&](DeclarationName Name,
3552 DeclContext::lookup_result Result) {
3553 if (Result.empty())
3554 return;
3555
3556 // Different DeclarationName values of certain kinds are mapped to
3557 // identical serialized keys, because we don't want to use type
3558 // identifiers in the keys (since type ids are local to the module).
3559 switch (Name.getNameKind()) {
3560 case DeclarationName::CXXConstructorName:
3561 // There may be different CXXConstructorName DeclarationName values
3562 // in a DeclContext because a UsingDecl that inherits constructors
3563 // has the DeclarationName of the inherited constructors.
3564 if (!ConstructorName)
3565 ConstructorName = Name;
3566 ConstructorDecls.append(Result.begin(), Result.end());
3567 return;
3568
3569 case DeclarationName::CXXConversionFunctionName:
3570 if (!ConversionName)
3571 ConversionName = Name;
3572 ConversionDecls.append(Result.begin(), Result.end());
3573 return;
3574
3575 default:
3576 break;
3577 }
3578
3579 Generator.insert(Name, Result, Trait);
3580 });
Richard Smith961eae52014-03-25 01:14:22 +00003581
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003582 // Add the constructors.
3583 if (!ConstructorDecls.empty()) {
3584 Generator.insert(ConstructorName,
3585 DeclContext::lookup_result(ConstructorDecls.begin(),
3586 ConstructorDecls.end()),
3587 Trait);
3588 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003589
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003590 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003591 if (!ConversionDecls.empty()) {
3592 Generator.insert(ConversionName,
3593 DeclContext::lookup_result(ConversionDecls.begin(),
3594 ConversionDecls.end()),
3595 Trait);
3596 }
3597
3598 // Create the on-disk hash table in a buffer.
3599 llvm::raw_svector_ostream Out(LookupTable);
3600 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003601 using namespace llvm::support;
3602 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003603 return Generator.Emit(Out, Trait);
3604}
3605
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003606/// \brief Write the block containing all of the declaration IDs
3607/// visible from the given DeclContext.
3608///
3609/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003610/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003611uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3612 DeclContext *DC) {
3613 if (DC->getPrimaryContext() != DC)
3614 return 0;
3615
3616 // Since there is no name lookup into functions or methods, don't bother to
3617 // build a visible-declarations table for these entities.
3618 if (DC->isFunctionOrMethod())
3619 return 0;
3620
3621 // If not in C++, we perform name lookup for the translation unit via the
3622 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003623 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003624 return 0;
3625
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003626 // Serialize the contents of the mapping used for lookup. Note that,
3627 // although we have two very different code paths, the serialized
3628 // representation is the same for both cases: a declaration name,
3629 // followed by a size, followed by references to the visible
3630 // declarations that have that name.
3631 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003632 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003633 if (!Map || Map->empty())
3634 return 0;
3635
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003636 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003637 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003638 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003639
3640 // Write the lookup table
3641 RecordData Record;
3642 Record.push_back(DECL_CONTEXT_VISIBLE);
3643 Record.push_back(BucketOffset);
3644 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3645 LookupTable.str());
3646
3647 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3648 ++NumVisibleDeclContexts;
3649 return Offset;
3650}
3651
Sebastian Redla4071b42010-08-24 00:50:09 +00003652/// \brief Write an UPDATE_VISIBLE block for the given context.
3653///
3654/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3655/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003656/// (in C++), for namespaces, and for classes with forward-declared unscoped
3657/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003658void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003659 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003660 if (!Map || Map->empty())
3661 return;
3662
Sebastian Redla4071b42010-08-24 00:50:09 +00003663 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003664 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003665 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003666
3667 // Write the lookup table
3668 RecordData Record;
3669 Record.push_back(UPDATE_VISIBLE);
3670 Record.push_back(getDeclID(cast<Decl>(DC)));
3671 Record.push_back(BucketOffset);
3672 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3673}
3674
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003675/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3676void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3677 RecordData Record;
3678 Record.push_back(Opts.fp_contract);
3679 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3680}
3681
3682/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3683void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003684 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003685 return;
3686
3687 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3688 RecordData Record;
3689#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3690#include "clang/Basic/OpenCLExtensions.def"
3691 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3692}
3693
Douglas Gregor358cd442012-01-15 16:58:34 +00003694void ASTWriter::WriteRedeclarations() {
3695 RecordData LocalRedeclChains;
3696 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3697
3698 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3699 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003700 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003701
3702 Decl *MostRecent = First->getMostRecentDecl();
3703
3704 // If we only have a single declaration, there is no point in storing
3705 // a redeclaration chain.
3706 if (First == MostRecent)
3707 continue;
3708
3709 unsigned Offset = LocalRedeclChains.size();
3710 unsigned Size = 0;
3711 LocalRedeclChains.push_back(0); // Placeholder for the size.
3712
3713 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003714 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003715 Prev = Prev->getPreviousDecl()) {
3716 if (!Prev->isFromASTFile()) {
3717 AddDeclRef(Prev, LocalRedeclChains);
3718 ++Size;
3719 }
3720 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003721
3722 if (!First->isFromASTFile() && Chain) {
3723 Decl *FirstFromAST = MostRecent;
3724 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3725 if (Prev->isFromASTFile())
3726 FirstFromAST = Prev;
3727 }
3728
3729 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3730 }
3731
Douglas Gregor358cd442012-01-15 16:58:34 +00003732 LocalRedeclChains[Offset] = Size;
3733
3734 // Reverse the set of local redeclarations, so that we store them in
3735 // order (since we found them in reverse order).
3736 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3737
Douglas Gregor6168bd22013-02-18 15:53:43 +00003738 // Add the mapping from the first ID from the AST to the set of local
3739 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003740 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3741 LocalRedeclsMap.push_back(Info);
3742
3743 assert(N == Redeclarations.size() &&
3744 "Deserialized a declaration we shouldn't have");
3745 }
3746
3747 if (LocalRedeclChains.empty())
3748 return;
3749
3750 // Sort the local redeclarations map by the first declaration ID,
3751 // since the reader will be performing binary searches on this information.
3752 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3753
3754 // Emit the local redeclarations map.
3755 using namespace llvm;
3756 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3757 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3760 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3761
3762 RecordData Record;
3763 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3764 Record.push_back(LocalRedeclsMap.size());
3765 Stream.EmitRecordWithBlob(AbbrevID, Record,
3766 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3767 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3768
3769 // Emit the redeclaration chains.
3770 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3771}
3772
Douglas Gregor404cdde2012-01-27 01:47:08 +00003773void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003774 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003775 RecordData Categories;
3776
3777 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3778 unsigned Size = 0;
3779 unsigned StartIndex = Categories.size();
3780
3781 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3782
3783 // Allocate space for the size.
3784 Categories.push_back(0);
3785
3786 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003787 for (ObjCInterfaceDecl::known_categories_iterator
3788 Cat = Class->known_categories_begin(),
3789 CatEnd = Class->known_categories_end();
3790 Cat != CatEnd; ++Cat, ++Size) {
3791 assert(getDeclID(*Cat) != 0 && "Bogus category");
3792 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003793 }
3794
3795 // Update the size.
3796 Categories[StartIndex] = Size;
3797
3798 // Record this interface -> category map.
3799 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3800 CategoriesMap.push_back(CatInfo);
3801 }
3802
3803 // Sort the categories map by the definition ID, since the reader will be
3804 // performing binary searches on this information.
3805 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3806
3807 // Emit the categories map.
3808 using namespace llvm;
3809 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3810 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3813 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3814
3815 RecordData Record;
3816 Record.push_back(OBJC_CATEGORIES_MAP);
3817 Record.push_back(CategoriesMap.size());
3818 Stream.EmitRecordWithBlob(AbbrevID, Record,
3819 reinterpret_cast<char*>(CategoriesMap.data()),
3820 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3821
3822 // Emit the category lists.
3823 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3824}
3825
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003826void ASTWriter::WriteMergedDecls() {
3827 if (!Chain || Chain->MergedDecls.empty())
3828 return;
3829
3830 RecordData Record;
3831 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3832 IEnd = Chain->MergedDecls.end();
3833 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003834 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Richard Smithcd45dbc2014-04-19 03:48:30 +00003835 : GetDeclRef(I->first);
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003836 assert(CanonID && "Merged declaration not known?");
3837
3838 Record.push_back(CanonID);
3839 Record.push_back(I->second.size());
3840 Record.append(I->second.begin(), I->second.end());
3841 }
3842 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3843}
3844
Richard Smithe40f2ba2013-08-07 21:41:30 +00003845void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3846 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3847
3848 if (LPTMap.empty())
3849 return;
3850
3851 RecordData Record;
3852 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3853 ItEnd = LPTMap.end();
3854 It != ItEnd; ++It) {
3855 LateParsedTemplate *LPT = It->second;
3856 AddDeclRef(It->first, Record);
3857 AddDeclRef(LPT->D, Record);
3858 Record.push_back(LPT->Toks.size());
3859
3860 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3861 TokEnd = LPT->Toks.end();
3862 TokIt != TokEnd; ++TokIt) {
3863 AddToken(*TokIt, Record);
3864 }
3865 }
3866 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3867}
3868
Dario Domizioli13a0a382014-05-23 12:13:25 +00003869/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3870void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3871 RecordData Record;
3872 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3873 AddSourceLocation(PragmaLoc, Record);
3874 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3875}
3876
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003877//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003878// General Serialization Routines
3879//===----------------------------------------------------------------------===//
3880
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003881/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003882void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3883 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003884 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003885 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3886 e = Attrs.end(); i != e; ++i){
3887 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003888 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003889 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003890
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003891#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003892
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003893 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003894}
3895
John McCallf413f5e2013-05-03 00:10:13 +00003896void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3897 AddSourceLocation(Tok.getLocation(), Record);
3898 Record.push_back(Tok.getLength());
3899
3900 // FIXME: When reading literal tokens, reconstruct the literal pointer
3901 // if it is needed.
3902 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3903 // FIXME: Should translate token kind to a stable encoding.
3904 Record.push_back(Tok.getKind());
3905 // FIXME: Should translate token flags to a stable encoding.
3906 Record.push_back(Tok.getFlags());
3907}
3908
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003909void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003910 Record.push_back(Str.size());
3911 Record.insert(Record.end(), Str.begin(), Str.end());
3912}
3913
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003914void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3915 RecordDataImpl &Record) {
3916 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003917 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003918 Record.push_back(*Minor + 1);
3919 else
3920 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003921 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003922 Record.push_back(*Subminor + 1);
3923 else
3924 Record.push_back(0);
3925}
3926
Douglas Gregore84a9da2009-04-20 20:36:09 +00003927/// \brief Note that the identifier II occurs at the given offset
3928/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003929void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003930 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003931 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003932 // up earlier in the chain and thus don't need an offset.
3933 if (ID >= FirstIdentID)
3934 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003935}
3936
Douglas Gregor95c13f52009-04-25 17:48:32 +00003937/// \brief Note that the selector Sel occurs at the given offset
3938/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003939void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003940 unsigned ID = SelectorIDs[Sel];
3941 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003942 // Don't record offsets for selectors that are also available in a different
3943 // file.
3944 if (ID < FirstSelectorID)
3945 return;
3946 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003947}
3948
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003949ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Craig Toppera13603a2014-05-22 05:54:18 +00003950 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
3951 WritingModule(nullptr), WritingAST(false), DoneWritingDeclsAndTypes(false),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003952 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003953 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003954 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003955 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3956 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003957 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3958 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003959 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003960 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003961 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003962 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003963 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003964 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003965 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3966 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3967 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003968 DeclTypedefAbbrev(0),
3969 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3970 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003971{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003972}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003973
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003974ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003975 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003976}
3977
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003978void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003979 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003980 Module *WritingModule, StringRef isysroot,
3981 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003982 WritingAST = true;
3983
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003984 ASTHasCompilerErrors = hasErrors;
3985
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003986 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003987 Stream.Emit((unsigned)'C', 8);
3988 Stream.Emit((unsigned)'P', 8);
3989 Stream.Emit((unsigned)'C', 8);
3990 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003991
Chris Lattner28fa4e62009-04-26 22:26:21 +00003992 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003993
Douglas Gregoreda8e122011-08-09 15:13:55 +00003994 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003995 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003996 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003997 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00003998 Context = nullptr;
3999 PP = nullptr;
4000 this->WritingModule = nullptr;
4001
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004002 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004003}
4004
Douglas Gregora94a1542011-07-27 21:45:57 +00004005template<typename Vector>
4006static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4007 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004008 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4009 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004010 Writer.AddDeclRef(*I, Record);
4011 }
4012}
4013
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004014void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004015 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004016 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004017 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004018 using namespace llvm;
4019
Craig Toppera13603a2014-05-22 05:54:18 +00004020 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004021
Douglas Gregorcf68c582011-12-01 22:20:10 +00004022 // Make sure that the AST reader knows to finalize itself.
4023 if (Chain)
4024 Chain->finalizeForWriting();
4025
Sebastian Redl143413f2010-07-12 22:02:52 +00004026 ASTContext &Context = SemaRef.Context;
4027 Preprocessor &PP = SemaRef.PP;
4028
Douglas Gregordab42432011-08-12 00:15:20 +00004029 // Set up predefined declaration IDs.
4030 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00004031 if (Context.ObjCIdDecl)
4032 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00004033 if (Context.ObjCSelDecl)
4034 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00004035 if (Context.ObjCClassDecl)
4036 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00004037 if (Context.ObjCProtocolClassDecl)
4038 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00004039 if (Context.Int128Decl)
4040 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4041 if (Context.UInt128Decl)
4042 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00004043 if (Context.ObjCInstanceTypeDecl)
4044 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004045 if (Context.BuiltinVaListDecl)
4046 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4047
Douglas Gregor851443c2011-08-12 01:39:19 +00004048 if (!Chain) {
4049 // Make sure that we emit IdentifierInfos (and any attached
4050 // declarations) for builtins. We don't need to do this when we're
4051 // emitting chained PCH files, because all of the builtins will be
4052 // in the original PCH file.
4053 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004054 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004055 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00004056 if (!Context.getLangOpts().NoBuiltin) {
4057 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4058 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004059 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4060 getIdentifierRef(&Table.get(BuiltinNames[I]));
4061 }
4062
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004063 // If there are any out-of-date identifiers, bring them up to date.
4064 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00004065 // Find out-of-date identifiers.
4066 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004067 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4068 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00004069 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004070 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00004071 OutOfDate.push_back(ID->second);
4072 }
4073
4074 // Update the out-of-date identifiers.
4075 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4076 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4077 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004078 }
4079
Richard Smithcd45dbc2014-04-19 03:48:30 +00004080 // If we saw any DeclContext updates before we started writing the AST file,
4081 // make sure all visible decls in those DeclContexts are written out.
4082 if (!UpdatedDeclContexts.empty()) {
4083 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4084 UpdatedDeclContexts.clear();
4085 for (auto *DC : OldUpdatedDeclContexts)
4086 AddUpdatedDeclContext(DC);
4087 }
4088
Chris Lattner0c797362009-09-08 18:19:27 +00004089 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004090 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004091 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004092 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004093 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004094
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004095 // Build a record containing all of the file scoped decls in this file.
4096 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004097 if (!isModule)
4098 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4099 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004100
Douglas Gregor851443c2011-08-12 01:39:19 +00004101 // Build a record containing all of the delegating constructors we still need
4102 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004103 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004104 if (!isModule)
4105 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004106
Douglas Gregor851443c2011-08-12 01:39:19 +00004107 // Write the set of weak, undeclared identifiers. We always write the
4108 // entire table, since later PCH files in a PCH chain are only interested in
4109 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004110 RecordData WeakUndeclaredIdentifiers;
4111 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004112 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004113 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4114 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4115 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4116 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4117 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4118 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4119 }
4120 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004121
Richard Smith78165b52013-01-10 23:43:47 +00004122 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004123 // declarations in this header file. Generally, this record will be
4124 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00004125 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004126 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00004127 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00004128 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00004129 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4130 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00004131 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004132 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00004133 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00004134 }
4135
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004136 // Build a record containing all of the ext_vector declarations.
4137 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004138 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004139
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004140 // Build a record containing all of the VTable uses information.
4141 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004142 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004143 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4144 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4145 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4146 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4147 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004148 }
4149
4150 // Build a record containing all of dynamic classes declarations.
4151 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004152 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004153
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004154 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004155 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004156 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004157 I = SemaRef.PendingInstantiations.begin(),
4158 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4159 AddDeclRef(I->first, PendingInstantiations);
4160 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004161 }
4162 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4163 "There are local ones at end of translation unit!");
4164
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004165 // Build a record containing some declaration references.
4166 RecordData SemaDeclRefs;
4167 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4168 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4169 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4170 }
4171
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004172 RecordData CUDASpecialDeclRefs;
4173 if (Context.getcudaConfigureCallDecl()) {
4174 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4175 }
4176
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004177 // Build a record containing all of the known namespaces.
4178 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004179 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004180 I = SemaRef.KnownNamespaces.begin(),
4181 IEnd = SemaRef.KnownNamespaces.end();
4182 I != IEnd; ++I) {
4183 if (!I->second)
4184 AddDeclRef(I->first, KnownNamespaces);
4185 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004186
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004187 // Build a record of all used, undefined objects that require definitions.
4188 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004189
4190 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004191 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004192 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4193 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004194 AddDeclRef(I->first, UndefinedButUsed);
4195 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004196 }
4197
Douglas Gregor112b9072012-10-18 05:31:06 +00004198 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004199 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004201 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004202 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004203 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004204
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004205 // This is so that older clang versions, before the introduction
4206 // of the control block, can read and reject the newer PCH format.
4207 Record.clear();
4208 Record.push_back(VERSION_MAJOR);
4209 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4210
Douglas Gregor851443c2011-08-12 01:39:19 +00004211 // Create a lexical update block containing all of the declarations in the
4212 // translation unit that do not come from other AST files.
4213 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4214 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004215 for (const auto *I : TU->noload_decls()) {
4216 if (!I->isFromASTFile())
4217 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004218 }
4219
4220 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4221 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4222 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4223 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4224 Record.clear();
4225 Record.push_back(TU_UPDATE_LEXICAL);
4226 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4227 data(NewGlobalDecls));
4228
4229 // And a visible updates block for the translation unit.
4230 Abv = new llvm::BitCodeAbbrev();
4231 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4232 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4233 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4234 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4235 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4236 WriteDeclContextVisibleUpdate(TU);
4237
4238 // If the translation unit has an anonymous namespace, and we don't already
4239 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004240 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004241 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4242 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004243 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004244 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004245 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004246
Richard Smith5652c0f2014-03-21 01:48:23 +00004247 // Add update records for all mangling numbers and static local numbers.
4248 // These aren't really update records, but this is a convenient way of
4249 // tagging this rare extra data onto the declarations.
4250 for (const auto &Number : Context.MangleNumbers)
4251 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004252 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4253 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004254 for (const auto &Number : Context.StaticLocalNumbers)
4255 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004256 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4257 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004258
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004259 // Make sure visible decls, added to DeclContexts previously loaded from
4260 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004261 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004262 I = UpdatingVisibleDecls.begin(),
4263 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4264 GetDeclRef(*I);
4265 }
4266
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004267 // Make sure all decls associated with an identifier are registered for
4268 // serialization.
4269 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4270 IDEnd = PP.getIdentifierTable().end();
4271 ID != IDEnd; ++ID) {
4272 const IdentifierInfo *II = ID->second;
4273 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4274 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4275 DEnd = SemaRef.IdResolver.end();
4276 D != DEnd; ++D) {
4277 GetDeclRef(*D);
4278 }
4279 }
4280 }
4281
Douglas Gregor5204bde2011-08-02 16:26:37 +00004282 // Form the record of special types.
4283 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004284 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004285 AddTypeRef(Context.getFILEType(), SpecialTypes);
4286 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4287 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4288 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4289 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004290 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004291 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004292
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004293 if (Chain) {
4294 // Write the mapping information describing our module dependencies and how
4295 // each of those modules were mapped into our own offset/ID space, so that
4296 // the reader can build the appropriate mapping to its own offset/ID space.
4297 // The map consists solely of a blob with the following format:
4298 // *(module-name-len:i16 module-name:len*i8
4299 // source-location-offset:i32
4300 // identifier-id:i32
4301 // preprocessed-entity-id:i32
4302 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004303 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004304 // selector-id:i32
4305 // declaration-id:i32
4306 // c++-base-specifiers-id:i32
4307 // type-id:i32)
4308 //
4309 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4310 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4312 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004313 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004314 {
4315 llvm::raw_svector_ostream Out(Buffer);
4316 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004317 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004318 M != MEnd; ++M) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004319 using namespace llvm::support;
4320 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004321 StringRef FileName = (*M)->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004322 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004323 Out.write(FileName.data(), FileName.size());
Justin Bognere1c147c2014-03-28 22:03:19 +00004324 LE.write<uint32_t>((*M)->SLocEntryBaseOffset);
4325 LE.write<uint32_t>((*M)->BaseIdentifierID);
4326 LE.write<uint32_t>((*M)->BaseMacroID);
4327 LE.write<uint32_t>((*M)->BasePreprocessedEntityID);
4328 LE.write<uint32_t>((*M)->BaseSubmoduleID);
4329 LE.write<uint32_t>((*M)->BaseSelectorID);
4330 LE.write<uint32_t>((*M)->BaseDeclID);
4331 LE.write<uint32_t>((*M)->BaseTypeIndex);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004332 }
4333 }
4334 Record.clear();
4335 Record.push_back(MODULE_OFFSET_MAP);
4336 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4337 Buffer.data(), Buffer.size());
4338 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004339
Richard Smithb9eab6d2014-03-20 19:44:17 +00004340 RecordData DeclUpdatesOffsetsRecord;
4341
Richard Smith59442b42014-03-20 20:07:19 +00004342 // Keep writing types, declarations, and declaration update records
4343 // until we've emitted all of them.
Richard Smithb9eab6d2014-03-20 19:44:17 +00004344 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
4345 WriteDeclsBlockAbbrevs();
4346 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4347 E = DeclsToRewrite.end();
4348 I != E; ++I)
4349 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004350 do {
4351 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4352 while (!DeclTypesToEmit.empty()) {
4353 DeclOrType DOT = DeclTypesToEmit.front();
4354 DeclTypesToEmit.pop();
4355 if (DOT.isType())
4356 WriteType(DOT.getType());
4357 else
4358 WriteDecl(Context, DOT.getDecl());
4359 }
4360 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004361 Stream.ExitBlock();
4362
Richard Smithb9eab6d2014-03-20 19:44:17 +00004363 DoneWritingDeclsAndTypes = true;
4364
4365 // These things can only be done once we've written out decls and types.
4366 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004367 if (!DeclUpdatesOffsetsRecord.empty())
4368 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004369 WriteCXXBaseSpecifiersOffsets();
4370 WriteFileDeclIDsMap();
4371 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4372
4373 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004374 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004375 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004376 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004377 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004378 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004379 WriteFPPragmaOptions(SemaRef.getFPOptions());
4380 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004381 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004382
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004383 // If we're emitting a module, write out the submodule information.
4384 if (WritingModule)
4385 WriteSubmodules(WritingModule);
4386
Douglas Gregor5204bde2011-08-02 16:26:37 +00004387 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4388
Douglas Gregord4df8652009-04-22 22:02:47 +00004389 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004390 if (!EagerlyDeserializedDecls.empty())
4391 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004392
4393 // Write the record containing tentative definitions.
4394 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004395 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004396
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004397 // Write the record containing unused file scoped decls.
4398 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004399 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004400
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004401 // Write the record containing weak undeclared identifiers.
4402 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004403 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004404 WeakUndeclaredIdentifiers);
4405
Richard Smith78165b52013-01-10 23:43:47 +00004406 // Write the record containing locally-scoped extern "C" definitions.
4407 if (!LocallyScopedExternCDecls.empty())
4408 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4409 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004410
4411 // Write the record containing ext_vector type names.
4412 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004413 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004414
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004415 // Write the record containing VTable uses information.
4416 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004417 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004418
4419 // Write the record containing dynamic classes declarations.
4420 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004421 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004422
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004423 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004424 if (!PendingInstantiations.empty())
4425 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004426
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004427 // Write the record containing declaration references of Sema.
4428 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004429 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004430
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004431 // Write the record containing CUDA-specific declaration references.
4432 if (!CUDASpecialDeclRefs.empty())
4433 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004434
4435 // Write the delegating constructors.
4436 if (!DelegatingCtorDecls.empty())
4437 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004438
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004439 // Write the known namespaces.
4440 if (!KnownNamespaces.empty())
4441 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004442
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004443 // Write the undefined internal functions and variables, and inline functions.
4444 if (!UndefinedButUsed.empty())
4445 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004446
Douglas Gregor851443c2011-08-12 01:39:19 +00004447 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004448 for (auto *DC : UpdatedDeclContexts)
4449 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004450
Douglas Gregor959bb062011-12-03 01:15:29 +00004451 if (!WritingModule) {
4452 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004453 struct ModuleInfo {
4454 uint64_t ID;
4455 Module *M;
4456 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4457 };
Richard Smith56be7542014-03-21 00:33:59 +00004458 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004459 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004460 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004461 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4462 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004463 }
Richard Smith56be7542014-03-21 00:33:59 +00004464
4465 if (!Imports.empty()) {
4466 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4467 return A.ID < B.ID;
4468 };
4469
4470 // Sort and deduplicate module IDs.
4471 std::sort(Imports.begin(), Imports.end(), Cmp);
4472 Imports.erase(std::unique(Imports.begin(), Imports.end(), Cmp),
4473 Imports.end());
4474
4475 RecordData ImportedModules;
4476 for (const auto &Import : Imports) {
4477 ImportedModules.push_back(Import.ID);
4478 // FIXME: If the module has macros imported then later has declarations
4479 // imported, this location won't be the right one as a location for the
4480 // declaration imports.
4481 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4482 }
4483
Douglas Gregor959bb062011-12-03 01:15:29 +00004484 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4485 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004486 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004487
Douglas Gregor851443c2011-08-12 01:39:19 +00004488 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004489 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004490 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004491 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004492 WriteLateParsedTemplates(SemaRef);
Dario Domizioli13a0a382014-05-23 12:13:25 +00004493 if(!WritingModule)
4494 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004495
Douglas Gregor08f01292009-04-17 22:13:46 +00004496 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004497 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004498 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004499 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004500 Record.push_back(NumLexicalDeclContexts);
4501 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004502 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004503 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004504}
4505
Richard Smithb9eab6d2014-03-20 19:44:17 +00004506void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004507 if (DeclUpdates.empty())
4508 return;
4509
Richard Smith59442b42014-03-20 20:07:19 +00004510 DeclUpdateMap LocalUpdates;
4511 LocalUpdates.swap(DeclUpdates);
4512
Richard Smith6ef42932014-03-20 21:02:00 +00004513 for (auto &DeclUpdate : LocalUpdates) {
4514 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004515 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004516 continue; // The decl will be written completely,no need to store updates.
4517
Richard Smithd28ac5b2014-03-22 23:33:22 +00004518 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004519 RecordData Record;
4520 for (auto &Update : DeclUpdate.second) {
4521 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4522
4523 Record.push_back(Kind);
4524 switch (Kind) {
4525 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4526 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4527 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004528 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004529 Record.push_back(GetDeclRef(Update.getDecl()));
4530 break;
4531
4532 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4533 AddSourceLocation(Update.getLoc(), Record);
4534 break;
4535
Richard Smithd28ac5b2014-03-22 23:33:22 +00004536 case UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION:
4537 // An updated body is emitted last, so that the reader doesn't need
4538 // to skip over the lazy body to reach statements for other records.
4539 Record.pop_back();
4540 HasUpdatedBody = true;
4541 break;
4542
Richard Smithcd45dbc2014-04-19 03:48:30 +00004543 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4544 auto *RD = cast<CXXRecordDecl>(D);
4545 AddUpdatedDeclContext(RD->getPrimaryContext());
4546 AddCXXDefinitionData(RD, Record);
4547 Record.push_back(WriteDeclContextLexicalBlock(
4548 *Context, const_cast<CXXRecordDecl *>(RD)));
4549
4550 // This state is sometimes updated by template instantiation, when we
4551 // switch from the specialization referring to the template declaration
4552 // to it referring to the template definition.
4553 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4554 Record.push_back(MSInfo->getTemplateSpecializationKind());
4555 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4556 } else {
4557 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4558 Record.push_back(Spec->getTemplateSpecializationKind());
4559 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004560
4561 // The instantiation might have been resolved to a partial
4562 // specialization. If so, record which one.
4563 auto From = Spec->getInstantiatedFrom();
4564 if (auto PartialSpec =
4565 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4566 Record.push_back(true);
4567 AddDeclRef(PartialSpec, Record);
4568 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4569 Record);
4570 } else {
4571 Record.push_back(false);
4572 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004573 }
4574 Record.push_back(RD->getTagKind());
4575 AddSourceLocation(RD->getLocation(), Record);
4576 AddSourceLocation(RD->getLocStart(), Record);
4577 AddSourceLocation(RD->getRBraceLoc(), Record);
4578
4579 // Instantiation may change attributes; write them all out afresh.
4580 Record.push_back(D->hasAttrs());
4581 if (Record.back())
4582 WriteAttributes(ArrayRef<const Attr*>(D->getAttrs().begin(),
4583 D->getAttrs().size()), Record);
4584
4585 // FIXME: Ensure we don't get here for explicit instantiations.
4586 break;
4587 }
4588
Richard Smith564417a2014-03-20 21:47:22 +00004589 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4590 addExceptionSpec(
4591 *this,
4592 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4593 Record);
4594 break;
4595
Richard Smith6ef42932014-03-20 21:02:00 +00004596 case UPD_CXX_DEDUCED_RETURN_TYPE:
4597 Record.push_back(GetOrCreateTypeID(Update.getType()));
4598 break;
4599
4600 case UPD_DECL_MARKED_USED:
4601 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004602
4603 case UPD_MANGLING_NUMBER:
4604 case UPD_STATIC_LOCAL_NUMBER:
4605 Record.push_back(Update.getNumber());
4606 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004607 }
4608 }
4609
Richard Smithd28ac5b2014-03-22 23:33:22 +00004610 if (HasUpdatedBody) {
4611 const FunctionDecl *Def = cast<FunctionDecl>(D);
4612 Record.push_back(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION);
4613 Record.push_back(Def->isInlined());
4614 AddSourceLocation(Def->getInnerLocStart(), Record);
4615 AddFunctionDefinition(Def, Record);
4616 }
4617
Richard Smithcd45dbc2014-04-19 03:48:30 +00004618 OffsetsRecord.push_back(GetDeclRef(D));
4619 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4620
Richard Smith6ef42932014-03-20 21:02:00 +00004621 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004622
Richard Smithb9eab6d2014-03-20 19:44:17 +00004623 // Flush any statements that were written as part of this update record.
4624 FlushStmts();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004625
4626 // Flush C++ base specifiers, if there are any.
4627 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004628 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004629}
4630
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004631void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004632 if (ReplacedDecls.empty())
4633 return;
4634
4635 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004636 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4637 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004638 Record.push_back(I->ID);
4639 Record.push_back(I->Offset);
4640 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004641 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004642 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004643}
4644
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004645void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004646 Record.push_back(Loc.getRawEncoding());
4647}
4648
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004649void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004650 AddSourceLocation(Range.getBegin(), Record);
4651 AddSourceLocation(Range.getEnd(), Record);
4652}
4653
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004654void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004655 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004656 const uint64_t *Words = Value.getRawData();
4657 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004658}
4659
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004660void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004661 Record.push_back(Value.isUnsigned());
4662 AddAPInt(Value, Record);
4663}
4664
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004665void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004666 AddAPInt(Value.bitcastToAPInt(), Record);
4667}
4668
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004669void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004670 Record.push_back(getIdentifierRef(II));
4671}
4672
Sebastian Redl539c5062010-08-18 23:57:32 +00004673IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004674 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004675 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004676
Sebastian Redl539c5062010-08-18 23:57:32 +00004677 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004678 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004679 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004680 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004681}
4682
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004683MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004684 // Don't emit builtin macros like __LINE__ to the AST file unless they
4685 // have been redefined by the header (in which case they are not
4686 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004687 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004688 return 0;
4689
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004690 MacroID &ID = MacroIDs[MI];
4691 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004692 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004693 MacroInfoToEmitData Info = { Name, MI, ID };
4694 MacroInfosToEmit.push_back(Info);
4695 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004696 return ID;
4697}
4698
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004699MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004700 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004701 return 0;
4702
4703 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4704 return MacroIDs[MI];
4705}
4706
4707uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4708 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4709 return IdentMacroDirectivesOffsetMap[Name];
4710}
4711
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004712void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004713 Record.push_back(getSelectorRef(SelRef));
4714}
4715
Sebastian Redl539c5062010-08-18 23:57:32 +00004716SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004717 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004718 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004719 }
4720
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004721 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004722 if (SID == 0 && Chain) {
4723 // This might trigger a ReadSelector callback, which will set the ID for
4724 // this selector.
4725 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004726 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004727 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004728 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004729 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004730 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004731 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004732 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004733}
4734
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004735void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004736 AddDeclRef(Temp->getDestructor(), Record);
4737}
4738
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004739void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4740 CXXBaseSpecifier const *BasesEnd,
4741 RecordDataImpl &Record) {
4742 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4743 CXXBaseSpecifiersToWrite.push_back(
4744 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4745 Bases, BasesEnd));
4746 Record.push_back(NextCXXBaseSpecifiersID++);
4747}
4748
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004749void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004750 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004751 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004752 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004753 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004754 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004755 break;
4756 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004757 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004758 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004759 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004760 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004761 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004762 break;
4763 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004764 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004765 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004766 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004767 break;
John McCall0ad16662009-10-29 08:12:44 +00004768 case TemplateArgument::Null:
4769 case TemplateArgument::Integral:
4770 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004771 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004772 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004773 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004774 break;
4775 }
4776}
4777
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004778void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004779 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004780 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004781
4782 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4783 bool InfoHasSameExpr
4784 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4785 Record.push_back(InfoHasSameExpr);
4786 if (InfoHasSameExpr)
4787 return; // Avoid storing the same expr twice.
4788 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004789 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4790 Record);
4791}
4792
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004793void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4794 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004795 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00004796 AddTypeRef(QualType(), Record);
4797 return;
4798 }
4799
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004800 AddTypeLoc(TInfo->getTypeLoc(), Record);
4801}
4802
4803void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4804 AddTypeRef(TL.getType(), Record);
4805
John McCall8f115c62009-10-16 21:56:05 +00004806 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004807 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004808 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004809}
4810
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004811void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004812 Record.push_back(GetOrCreateTypeID(T));
4813}
4814
Douglas Gregoreda8e122011-08-09 15:13:55 +00004815TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004816 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004817 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004818 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4819}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004820
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004821TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004822 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004823 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004824 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004825}
4826
4827TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4828 if (T.isNull())
4829 return TypeIdx();
4830 assert(!T.getLocalFastQualifiers());
4831
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004832 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004833 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004834 if (DoneWritingDeclsAndTypes) {
4835 assert(0 && "New type seen after serializing all the types to emit!");
4836 return TypeIdx();
4837 }
4838
Douglas Gregor1970d882009-04-26 03:49:13 +00004839 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004840 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004841 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004842 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004843 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004844 return Idx;
4845}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004846
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004847TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004848 if (T.isNull())
4849 return TypeIdx();
4850 assert(!T.getLocalFastQualifiers());
4851
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004852 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4853 assert(I != TypeIdxs.end() && "Type not emitted!");
4854 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004855}
4856
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004857void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004858 Record.push_back(GetDeclRef(D));
4859}
4860
Sebastian Redl539c5062010-08-18 23:57:32 +00004861DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004862 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00004863
4864 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004865 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004866 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004867
4868 // If D comes from an AST file, its declaration ID is already known and
4869 // fixed.
4870 if (D->isFromASTFile())
4871 return D->getGlobalID();
4872
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004873 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004874 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004875 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004876 if (DoneWritingDeclsAndTypes) {
4877 assert(0 && "New decl seen after serializing all the decls to emit!");
4878 return 0;
4879 }
4880
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004881 // We haven't seen this declaration before. Give it a new ID and
4882 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004883 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004884 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004885 }
4886
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004887 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004888}
4889
Sebastian Redl539c5062010-08-18 23:57:32 +00004890DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00004891 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00004892 return 0;
4893
Douglas Gregorb3163e52012-01-05 22:33:30 +00004894 // If D comes from an AST file, its declaration ID is already known and
4895 // fixed.
4896 if (D->isFromASTFile())
4897 return D->getGlobalID();
4898
Douglas Gregore84a9da2009-04-20 20:36:09 +00004899 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4900 return DeclIDs[D];
4901}
4902
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004903void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004904 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004905 assert(D);
4906
4907 SourceLocation Loc = D->getLocation();
4908 if (Loc.isInvalid())
4909 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004910
4911 // We only keep track of the file-level declarations of each file.
4912 if (!D->getLexicalDeclContext()->isFileContext())
4913 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004914 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4915 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004916 if (isa<ParmVarDecl>(D))
4917 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004918
4919 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004920 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004921 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004922 FileID FID;
4923 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004924 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004925 if (FID.isInvalid())
4926 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004927 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004928
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004929 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004930 if (!Info)
4931 Info = new DeclIDInFileInfo();
4932
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004933 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004934 LocDeclIDsTy &Decls = Info->DeclIDs;
4935
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004936 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004937 Decls.push_back(LocDecl);
4938 return;
4939 }
4940
Benjamin Kramer45025c02013-08-24 13:22:59 +00004941 LocDeclIDsTy::iterator I =
4942 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004943
4944 Decls.insert(I, LocDecl);
4945}
4946
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004947void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004948 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004949 Record.push_back(Name.getNameKind());
4950 switch (Name.getNameKind()) {
4951 case DeclarationName::Identifier:
4952 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4953 break;
4954
4955 case DeclarationName::ObjCZeroArgSelector:
4956 case DeclarationName::ObjCOneArgSelector:
4957 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004958 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004959 break;
4960
4961 case DeclarationName::CXXConstructorName:
4962 case DeclarationName::CXXDestructorName:
4963 case DeclarationName::CXXConversionFunctionName:
4964 AddTypeRef(Name.getCXXNameType(), Record);
4965 break;
4966
4967 case DeclarationName::CXXOperatorName:
4968 Record.push_back(Name.getCXXOverloadedOperator());
4969 break;
4970
Alexis Hunt3d221f22009-11-29 07:34:05 +00004971 case DeclarationName::CXXLiteralOperatorName:
4972 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4973 break;
4974
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004975 case DeclarationName::CXXUsingDirective:
4976 // No extra data to emit
4977 break;
4978 }
4979}
Chris Lattnerca025db2010-05-07 21:43:38 +00004980
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004981void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004982 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004983 switch (Name.getNameKind()) {
4984 case DeclarationName::CXXConstructorName:
4985 case DeclarationName::CXXDestructorName:
4986 case DeclarationName::CXXConversionFunctionName:
4987 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4988 break;
4989
4990 case DeclarationName::CXXOperatorName:
4991 AddSourceLocation(
4992 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4993 Record);
4994 AddSourceLocation(
4995 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4996 Record);
4997 break;
4998
4999 case DeclarationName::CXXLiteralOperatorName:
5000 AddSourceLocation(
5001 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5002 Record);
5003 break;
5004
5005 case DeclarationName::Identifier:
5006 case DeclarationName::ObjCZeroArgSelector:
5007 case DeclarationName::ObjCOneArgSelector:
5008 case DeclarationName::ObjCMultiArgSelector:
5009 case DeclarationName::CXXUsingDirective:
5010 break;
5011 }
5012}
5013
5014void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005015 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005016 AddDeclarationName(NameInfo.getName(), Record);
5017 AddSourceLocation(NameInfo.getLoc(), Record);
5018 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5019}
5020
5021void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005022 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005023 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005024 Record.push_back(Info.NumTemplParamLists);
5025 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5026 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5027}
5028
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005029void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005030 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005031 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005032 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005033 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005034
5035 // Push each of the NNS's onto a stack for serialization in reverse order.
5036 while (NNS) {
5037 NestedNames.push_back(NNS);
5038 NNS = NNS->getPrefix();
5039 }
5040
5041 Record.push_back(NestedNames.size());
5042 while(!NestedNames.empty()) {
5043 NNS = NestedNames.pop_back_val();
5044 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5045 Record.push_back(Kind);
5046 switch (Kind) {
5047 case NestedNameSpecifier::Identifier:
5048 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5049 break;
5050
5051 case NestedNameSpecifier::Namespace:
5052 AddDeclRef(NNS->getAsNamespace(), Record);
5053 break;
5054
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005055 case NestedNameSpecifier::NamespaceAlias:
5056 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5057 break;
5058
Chris Lattnerca025db2010-05-07 21:43:38 +00005059 case NestedNameSpecifier::TypeSpec:
5060 case NestedNameSpecifier::TypeSpecWithTemplate:
5061 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5062 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5063 break;
5064
5065 case NestedNameSpecifier::Global:
5066 // Don't need to write an associated value.
5067 break;
5068 }
5069 }
5070}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005071
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005072void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5073 RecordDataImpl &Record) {
5074 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005075 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005076 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005077
5078 // Push each of the nested-name-specifiers's onto a stack for
5079 // serialization in reverse order.
5080 while (NNS) {
5081 NestedNames.push_back(NNS);
5082 NNS = NNS.getPrefix();
5083 }
5084
5085 Record.push_back(NestedNames.size());
5086 while(!NestedNames.empty()) {
5087 NNS = NestedNames.pop_back_val();
5088 NestedNameSpecifier::SpecifierKind Kind
5089 = NNS.getNestedNameSpecifier()->getKind();
5090 Record.push_back(Kind);
5091 switch (Kind) {
5092 case NestedNameSpecifier::Identifier:
5093 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5094 AddSourceRange(NNS.getLocalSourceRange(), Record);
5095 break;
5096
5097 case NestedNameSpecifier::Namespace:
5098 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5099 AddSourceRange(NNS.getLocalSourceRange(), Record);
5100 break;
5101
5102 case NestedNameSpecifier::NamespaceAlias:
5103 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5104 AddSourceRange(NNS.getLocalSourceRange(), Record);
5105 break;
5106
5107 case NestedNameSpecifier::TypeSpec:
5108 case NestedNameSpecifier::TypeSpecWithTemplate:
5109 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5110 AddTypeLoc(NNS.getTypeLoc(), Record);
5111 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5112 break;
5113
5114 case NestedNameSpecifier::Global:
5115 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5116 break;
5117 }
5118 }
5119}
5120
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005121void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005122 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005123 Record.push_back(Kind);
5124 switch (Kind) {
5125 case TemplateName::Template:
5126 AddDeclRef(Name.getAsTemplateDecl(), Record);
5127 break;
5128
5129 case TemplateName::OverloadedTemplate: {
5130 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5131 Record.push_back(OvT->size());
5132 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5133 I != E; ++I)
5134 AddDeclRef(*I, Record);
5135 break;
5136 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005137
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005138 case TemplateName::QualifiedTemplate: {
5139 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5140 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5141 Record.push_back(QualT->hasTemplateKeyword());
5142 AddDeclRef(QualT->getTemplateDecl(), Record);
5143 break;
5144 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005145
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005146 case TemplateName::DependentTemplate: {
5147 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5148 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5149 Record.push_back(DepT->isIdentifier());
5150 if (DepT->isIdentifier())
5151 AddIdentifierRef(DepT->getIdentifier(), Record);
5152 else
5153 Record.push_back(DepT->getOperator());
5154 break;
5155 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005156
5157 case TemplateName::SubstTemplateTemplateParm: {
5158 SubstTemplateTemplateParmStorage *subst
5159 = Name.getAsSubstTemplateTemplateParm();
5160 AddDeclRef(subst->getParameter(), Record);
5161 AddTemplateName(subst->getReplacement(), Record);
5162 break;
5163 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005164
5165 case TemplateName::SubstTemplateTemplateParmPack: {
5166 SubstTemplateTemplateParmPackStorage *SubstPack
5167 = Name.getAsSubstTemplateTemplateParmPack();
5168 AddDeclRef(SubstPack->getParameterPack(), Record);
5169 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5170 break;
5171 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005172 }
5173}
5174
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005175void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005176 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005177 Record.push_back(Arg.getKind());
5178 switch (Arg.getKind()) {
5179 case TemplateArgument::Null:
5180 break;
5181 case TemplateArgument::Type:
5182 AddTypeRef(Arg.getAsType(), Record);
5183 break;
5184 case TemplateArgument::Declaration:
5185 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005186 Record.push_back(Arg.isDeclForReferenceParam());
5187 break;
5188 case TemplateArgument::NullPtr:
5189 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005190 break;
5191 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005192 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005193 AddTypeRef(Arg.getIntegralType(), Record);
5194 break;
5195 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005196 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5197 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005198 case TemplateArgument::TemplateExpansion:
5199 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005200 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005201 Record.push_back(*NumExpansions + 1);
5202 else
5203 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005204 break;
5205 case TemplateArgument::Expression:
5206 AddStmt(Arg.getAsExpr());
5207 break;
5208 case TemplateArgument::Pack:
5209 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005210 for (const auto &P : Arg.pack_elements())
5211 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005212 break;
5213 }
5214}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005215
5216void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005217ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005218 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005219 assert(TemplateParams && "No TemplateParams!");
5220 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5221 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5222 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5223 Record.push_back(TemplateParams->size());
5224 for (TemplateParameterList::const_iterator
5225 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5226 P != PEnd; ++P)
5227 AddDeclRef(*P, Record);
5228}
5229
5230/// \brief Emit a template argument list.
5231void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005232ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005233 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005234 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005235 Record.push_back(TemplateArgs->size());
5236 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005237 AddTemplateArgument(TemplateArgs->get(i), Record);
5238}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005239
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005240void
5241ASTWriter::AddASTTemplateArgumentListInfo
5242(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5243 assert(ASTTemplArgList && "No ASTTemplArgList!");
5244 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5245 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5246 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5247 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5248 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5249 AddTemplateArgumentLoc(TemplArgs[i], Record);
5250}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005251
5252void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005253ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005254 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005255 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005256 I = Set.begin(), E = Set.end(); I != E; ++I) {
5257 AddDeclRef(I.getDecl(), Record);
5258 Record.push_back(I.getAccess());
5259 }
5260}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005261
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005262void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005263 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005264 Record.push_back(Base.isVirtual());
5265 Record.push_back(Base.isBaseOfClass());
5266 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005267 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005268 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005269 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005270 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5271 : SourceLocation(),
5272 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005273}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005274
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005275void ASTWriter::FlushCXXBaseSpecifiers() {
5276 RecordData Record;
5277 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5278 Record.clear();
5279
5280 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005281 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005282 if (Index == CXXBaseSpecifiersOffsets.size())
5283 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5284 else {
5285 if (Index > CXXBaseSpecifiersOffsets.size())
5286 CXXBaseSpecifiersOffsets.resize(Index + 1);
5287 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5288 }
5289
5290 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5291 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5292 Record.push_back(BEnd - B);
5293 for (; B != BEnd; ++B)
5294 AddCXXBaseSpecifier(*B, Record);
5295 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005296
5297 // Flush any expressions that were written as part of the base specifiers.
5298 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005299 }
5300
5301 CXXBaseSpecifiersToWrite.clear();
5302}
5303
Alexis Hunt1d792652011-01-08 20:30:50 +00005304void ASTWriter::AddCXXCtorInitializers(
5305 const CXXCtorInitializer * const *CtorInitializers,
5306 unsigned NumCtorInitializers,
5307 RecordDataImpl &Record) {
5308 Record.push_back(NumCtorInitializers);
5309 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5310 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005311
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005312 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005313 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005314 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005315 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005316 } else if (Init->isDelegatingInitializer()) {
5317 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005318 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005319 } else if (Init->isMemberInitializer()){
5320 Record.push_back(CTOR_INITIALIZER_MEMBER);
5321 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005322 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005323 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5324 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005325 }
Francois Pichetd583da02010-12-04 09:14:42 +00005326
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005327 AddSourceLocation(Init->getMemberLocation(), Record);
5328 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005329 AddSourceLocation(Init->getLParenLoc(), Record);
5330 AddSourceLocation(Init->getRParenLoc(), Record);
5331 Record.push_back(Init->isWritten());
5332 if (Init->isWritten()) {
5333 Record.push_back(Init->getSourceOrder());
5334 } else {
5335 Record.push_back(Init->getNumArrayIndices());
5336 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5337 AddDeclRef(Init->getArrayIndex(i), Record);
5338 }
5339 }
5340}
5341
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005342void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005343 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005344 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005345 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005346 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005347 Record.push_back(Data.Aggregate);
5348 Record.push_back(Data.PlainOldData);
5349 Record.push_back(Data.Empty);
5350 Record.push_back(Data.Polymorphic);
5351 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005352 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005353 Record.push_back(Data.HasNoNonEmptyBases);
5354 Record.push_back(Data.HasPrivateFields);
5355 Record.push_back(Data.HasProtectedFields);
5356 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005357 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005358 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005359 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005360 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005361 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005362 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5363 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5364 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5365 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5366 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5367 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005368 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005369 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005370 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005371 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005372 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005373 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005374 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005375 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005376 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005377 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005378 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5379 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5380 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5381 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005382 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005383
5384 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005385 if (Data.NumBases > 0)
5386 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5387 Record);
5388
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005389 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5390 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005391 if (Data.NumVBases > 0)
5392 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5393 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005394
Richard Smitha4ba74c2013-08-30 04:46:40 +00005395 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5396 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005397 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005398 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005399
5400 // Add lambda-specific data.
5401 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005402 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005403 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005404 Record.push_back(Lambda.IsGenericLambda);
5405 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005406 Record.push_back(Lambda.NumCaptures);
5407 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005408 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005409 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005410 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005411 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005412 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005413 AddSourceLocation(Capture.getLocation(), Record);
5414 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005415 Record.push_back(Capture.getCaptureKind());
5416 switch (Capture.getCaptureKind()) {
5417 case LCK_This:
5418 break;
5419 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005420 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005421 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005422 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005423 AddDeclRef(Var, Record);
5424 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5425 : SourceLocation(),
5426 Record);
5427 break;
5428 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005429 }
5430 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005431}
5432
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005433void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005434 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005435 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005436 assert(FirstDeclID == NextDeclID &&
5437 FirstTypeID == NextTypeID &&
5438 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005439 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005440 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005441 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005442 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005443
Sebastian Redl07a89a82010-07-30 00:29:29 +00005444 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005445
Douglas Gregordf0c1512011-08-18 04:12:04 +00005446 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5447 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5448 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005449 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005450 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005451 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005452 NextDeclID = FirstDeclID;
5453 NextTypeID = FirstTypeID;
5454 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005455 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005456 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005457 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005458}
5459
Sebastian Redl539c5062010-08-18 23:57:32 +00005460void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005461 // Always keep the highest ID. See \p TypeRead() for more information.
5462 IdentID &StoredID = IdentifierIDs[II];
5463 if (ID > StoredID)
5464 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005465}
5466
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005467void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005468 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005469 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005470 if (ID > StoredID)
5471 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005472}
5473
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005474void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005475 // Always take the highest-numbered type index. This copes with an interesting
5476 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005477 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005478 // keep the higher-numbered entry so that we can properly write it out to
5479 // the AST file.
5480 TypeIdx &StoredIdx = TypeIdxs[T];
5481 if (Idx.getIndex() >= StoredIdx.getIndex())
5482 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005483}
5484
Sebastian Redl539c5062010-08-18 23:57:32 +00005485void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005486 // Always keep the highest ID. See \p TypeRead() for more information.
5487 SelectorID &StoredID = SelectorIDs[S];
5488 if (ID > StoredID)
5489 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005490}
Douglas Gregor91096292010-10-02 19:29:26 +00005491
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005492void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005493 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005494 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005495 MacroDefinitions[MD] = ID;
5496}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005497
Douglas Gregore37a85a2011-12-02 17:30:13 +00005498void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5499 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5500 SubmoduleIDs[Mod] = ID;
5501}
5502
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005503void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005504 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005505 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005506 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5507 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005508 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005509 // A forward reference was mutated into a definition. Rewrite it.
5510 // FIXME: This happens during template instantiation, should we
5511 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005512 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5513 "completed a tag from another module but not by instantiation?");
5514 DeclUpdates[RD].push_back(
5515 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005516 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005517 }
5518}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005519
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005520void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005521 assert(!WritingAST && "Already writing the AST!");
5522
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005523 // TU and namespaces are handled elsewhere.
5524 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5525 return;
5526
Douglas Gregorb3722e22011-09-09 23:01:35 +00005527 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005528 return; // Not a source decl added to a DeclContext from PCH.
5529
Douglas Gregor9f782892013-01-21 15:25:38 +00005530 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005531 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005532 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005533}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005534
5535void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005536 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005537 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005538 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005539 return; // Not a source member added to a class from PCH.
5540 if (!isa<CXXMethodDecl>(D))
5541 return; // We are interested in lazily declared implicit methods.
5542
5543 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005544 assert(RD->isCompleteDefinition());
Aaron Ballman4f45b712014-03-21 15:22:56 +00005545 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005546}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005547
5548void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5549 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005550 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005551 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005552 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005553 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005554 return; // Not a source specialization added to a template from PCH.
5555
Aaron Ballman4f45b712014-03-21 15:22:56 +00005556 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5557 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005558}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005559
Larisse Voufo39a1e502013-08-06 01:03:05 +00005560void ASTWriter::AddedCXXTemplateSpecialization(
5561 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5562 // The specializations set is kept in the canonical template.
5563 assert(!WritingAST && "Already writing the AST!");
5564 TD = TD->getCanonicalDecl();
5565 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5566 return; // Not a source specialization added to a template from PCH.
5567
Aaron Ballman4f45b712014-03-21 15:22:56 +00005568 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5569 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005570}
5571
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005572void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5573 const FunctionDecl *D) {
5574 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005575 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005576 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005577 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005578 return; // Not a source specialization added to a template from PCH.
5579
Aaron Ballman4f45b712014-03-21 15:22:56 +00005580 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5581 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005582}
5583
Richard Smith564417a2014-03-20 21:47:22 +00005584void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5585 assert(!WritingAST && "Already writing the AST!");
5586 FD = FD->getCanonicalDecl();
5587 if (!FD->isFromASTFile())
5588 return; // Not a function declared in PCH and defined outside.
5589
5590 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5591}
5592
Richard Smith1fa5d642013-05-11 05:45:24 +00005593void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5594 assert(!WritingAST && "Already writing the AST!");
5595 FD = FD->getCanonicalDecl();
5596 if (!FD->isFromASTFile())
5597 return; // Not a function declared in PCH and defined outside.
5598
Aaron Ballman4f45b712014-03-21 15:22:56 +00005599 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith1fa5d642013-05-11 05:45:24 +00005600}
5601
Sebastian Redlab238a72011-04-24 16:28:06 +00005602void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005603 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005604 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005605 return; // Declaration not imported from PCH.
5606
5607 // Implicit decl from a PCH was defined.
5608 // FIXME: Should implicit definition be a separate FunctionDecl?
5609 RewriteDecl(D);
5610}
5611
Richard Smithd28ac5b2014-03-22 23:33:22 +00005612void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5613 assert(!WritingAST && "Already writing the AST!");
5614 if (!D->isFromASTFile())
5615 return;
5616
5617 // Since the actual instantiation is delayed, this really means that we need
5618 // to update the instantiation location.
5619 DeclUpdates[D].push_back(
5620 DeclUpdate(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION));
5621}
5622
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005623void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005624 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005625 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005626 return;
5627
5628 // Since the actual instantiation is delayed, this really means that we need
5629 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005630 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005631 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5632 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005633}
5634
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005635void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5636 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005637 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005638 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005639 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005640
5641 assert(IFD->getDefinition() && "Category on a class without a definition?");
5642 ObjCClassesWithCategories.insert(
5643 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005644}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005645
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005646
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005647void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5648 const ObjCPropertyDecl *OrigProp,
5649 const ObjCCategoryDecl *ClassExt) {
5650 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5651 if (!D)
5652 return;
5653
5654 assert(!WritingAST && "Already writing the AST!");
5655 if (!D->isFromASTFile())
5656 return; // Declaration not imported from PCH.
5657
5658 RewriteDecl(D);
5659}
Eli Friedman276dd182013-09-05 00:02:25 +00005660
5661void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5662 assert(!WritingAST && "Already writing the AST!");
5663 if (!D->isFromASTFile())
5664 return;
5665
Aaron Ballman4f45b712014-03-21 15:22:56 +00005666 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005667}