blob: aad4305d8c58ac51cb6a5898e6af0ac46ce82640 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Richard Smith961eae52014-03-25 01:14:22 +000020#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000026#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000028#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000032#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000034#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000035#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/HeaderSearchOptions.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Lex/PreprocessingRecord.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Lex/PreprocessorOptions.h"
41#include "clang/Sema/IdentifierResolver.h"
42#include "clang/Sema/Sema.h"
43#include "clang/Serialization/ASTReader.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000044#include "llvm/ADT/APFloat.h"
45#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000047#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include "llvm/Bitcode/BitstreamWriter.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000049#include "llvm/Support/EndianStream.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000050#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000051#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000052#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000053#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000054#include "llvm/Support/Process.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000055#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000056#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000057#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000058#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000059using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000060using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061
Sebastian Redl3df5a082010-07-30 17:03:48 +000062template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000063static StringRef data(const std::vector<T, Allocator> &v) {
64 if (v.empty()) return StringRef();
65 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000066 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000067}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000068
69template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000070static StringRef data(const SmallVectorImpl<T> &v) {
71 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000072 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000073}
74
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075//===----------------------------------------------------------------------===//
76// Type serialization
77//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000078
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000080 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000081 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000082 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
84 public:
85 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000086 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +000087 /// \brief Abbreviation to use for the record, if any.
88 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000090 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000091 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092
93 void VisitArrayType(const ArrayType *T);
94 void VisitFunctionType(const FunctionType *T);
95 void VisitTagType(const TagType *T);
96
97#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
98#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099#include "clang/AST/TypeNodes.def"
100 };
101}
102
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000104 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000114 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115}
116
Reid Kleckner8a365022013-06-24 17:51:48 +0000117void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
118 Writer.AddTypeRef(T->getOriginalType(), Record);
119 Code = TYPE_DECAYED;
120}
121
Reid Kleckner0503a872013-12-05 01:23:43 +0000122void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
123 Writer.AddTypeRef(T->getOriginalType(), Record);
124 Writer.AddTypeRef(T->getAdjustedType(), Record);
125 Code = TYPE_ADJUSTED;
126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000134 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
135 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000136 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137}
138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000140 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000145 Writer.AddTypeRef(T->getPointeeType(), Record);
146 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000147 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148}
149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151 Writer.AddTypeRef(T->getElementType(), Record);
152 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000153 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154}
155
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157 VisitArrayType(T);
158 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000159 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160}
161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000164 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000169 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
170 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000171 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000172 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173}
174
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176 Writer.AddTypeRef(T->getElementType(), Record);
177 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000178 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000179 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180}
181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000184 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000188 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000189 FunctionType::ExtInfo C = T->getExtInfo();
190 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000191 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000192 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000193 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000194 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000195 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000196
197 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
198 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Richard Smith564417a2014-03-20 21:47:22 +0000206static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
207 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000208 Record.push_back(T->getExceptionSpecType());
209 if (T->getExceptionSpecType() == EST_Dynamic) {
210 Record.push_back(T->getNumExceptions());
211 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
212 Writer.AddTypeRef(T->getExceptionType(I), Record);
213 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
214 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000215 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
216 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
217 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000218 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
219 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000220 }
Richard Smith564417a2014-03-20 21:47:22 +0000221}
222
223void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
224 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000225
Richard Smith564417a2014-03-20 21:47:22 +0000226 Record.push_back(T->isVariadic());
227 Record.push_back(T->hasTrailingReturn());
228 Record.push_back(T->getTypeQuals());
229 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
230 addExceptionSpec(Writer, T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000231
232 Record.push_back(T->getNumParams());
233 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
234 Writer.AddTypeRef(T->getParamType(I), Record);
235
236 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
237 T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
238 AbbrevToUse = 0;
239
Sebastian Redl539c5062010-08-18 23:57:32 +0000240 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241}
242
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000243void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000244 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000246}
John McCallb96ec562009-12-04 22:46:56 +0000247
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000250 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
251 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000252 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000253}
254
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000255void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000256 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000257 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258}
259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000266 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000267 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000268 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000269}
270
Alexis Hunte852b102011-05-24 22:41:36 +0000271void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
272 Writer.AddTypeRef(T->getBaseType(), Record);
273 Writer.AddTypeRef(T->getUnderlyingType(), Record);
274 Record.push_back(T->getUTTKind());
275 Code = TYPE_UNARY_TRANSFORM;
276}
277
Richard Smith30482bc2011-02-20 03:19:35 +0000278void ASTTypeWriter::VisitAutoType(const AutoType *T) {
279 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000280 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000281 if (T->getDeducedType().isNull())
282 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000283 Code = TYPE_AUTO;
284}
285
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000287 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000288 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000289 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000290 "Cannot serialize in the middle of a type definition");
291}
292
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000293void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000294 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000295 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000296}
297
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000298void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000299 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000300 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000301}
302
John McCall81904512011-01-06 01:58:22 +0000303void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
304 Writer.AddTypeRef(T->getModifiedType(), Record);
305 Writer.AddTypeRef(T->getEquivalentType(), Record);
306 Record.push_back(T->getAttrKind());
307 Code = TYPE_ATTRIBUTED;
308}
309
Mike Stump11289f42009-09-09 15:08:12 +0000310void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000311ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000312 const SubstTemplateTypeParmType *T) {
313 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
314 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000315 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000316}
317
318void
Douglas Gregorada4b792011-01-14 02:55:32 +0000319ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
320 const SubstTemplateTypeParmPackType *T) {
321 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
322 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
323 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
324}
325
326void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000327ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000328 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000329 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000330 Writer.AddTemplateName(T->getTemplateName(), Record);
331 Record.push_back(T->getNumArgs());
332 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
333 ArgI != ArgE; ++ArgI)
334 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000335 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
336 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000337 : T->getCanonicalTypeInternal(),
338 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000339 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000343ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000344 VisitArrayType(T);
345 Writer.AddStmt(T->getSizeExpr());
346 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000347 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000348}
349
350void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000352 const DependentSizedExtVectorType *T) {
353 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000354 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000355}
356
357void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000358ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000359 Record.push_back(T->getDepth());
360 Record.push_back(T->getIndex());
361 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000362 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000363 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000364}
365
366void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000367ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000368 Record.push_back(T->getKeyword());
369 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
370 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000371 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
372 : T->getCanonicalTypeInternal(),
373 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000374 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000375}
376
377void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000378ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000379 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000380 Record.push_back(T->getKeyword());
381 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
382 Writer.AddIdentifierRef(T->getIdentifier(), Record);
383 Record.push_back(T->getNumArgs());
384 for (DependentTemplateSpecializationType::iterator
385 I = T->begin(), E = T->end(); I != E; ++I)
386 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000387 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388}
389
Douglas Gregord2fa7662010-12-20 02:24:11 +0000390void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
391 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000392 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000393 Record.push_back(*NumExpansions + 1);
394 else
395 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000396 Code = TYPE_PACK_EXPANSION;
397}
398
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000399void ASTTypeWriter::VisitParenType(const ParenType *T) {
400 Writer.AddTypeRef(T->getInnerType(), Record);
401 Code = TYPE_PAREN;
402}
403
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000404void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000405 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000406 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
407 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000408 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000409}
410
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000411void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000412 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000413 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000414 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000415}
416
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000417void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000418 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000419 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000420}
421
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000422void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000423 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000424 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000425 for (const auto *I : T->quals())
426 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000427 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000428}
429
Steve Narofffb4330f2009-06-17 22:40:22 +0000430void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000431ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000432 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000433 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000434}
435
Eli Friedman0dfb8892011-10-06 23:00:33 +0000436void
437ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
438 Writer.AddTypeRef(T->getValueType(), Record);
439 Code = TYPE_ATOMIC;
440}
441
John McCall8f115c62009-10-16 21:56:05 +0000442namespace {
443
444class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000445 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000446 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000447
448public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000449 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000450 : Writer(Writer), Record(Record) { }
451
John McCall17001972009-10-18 01:05:36 +0000452#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000453#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000454 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000455#include "clang/AST/TypeLocNodes.def"
456
John McCall17001972009-10-18 01:05:36 +0000457 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
458 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000459};
460
461}
462
John McCall17001972009-10-18 01:05:36 +0000463void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
464 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000465}
John McCall17001972009-10-18 01:05:36 +0000466void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000467 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
468 if (TL.needsExtraLocalData()) {
469 Record.push_back(TL.getWrittenTypeSpec());
470 Record.push_back(TL.getWrittenSignSpec());
471 Record.push_back(TL.getWrittenWidthSpec());
472 Record.push_back(TL.hasModeAttr());
473 }
John McCall8f115c62009-10-16 21:56:05 +0000474}
John McCall17001972009-10-18 01:05:36 +0000475void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000477}
John McCall17001972009-10-18 01:05:36 +0000478void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000480}
Reid Kleckner8a365022013-06-24 17:51:48 +0000481void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
482 // nothing to do
483}
Reid Kleckner0503a872013-12-05 01:23:43 +0000484void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
485 // nothing to do
486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000489}
John McCall17001972009-10-18 01:05:36 +0000490void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000492}
John McCall17001972009-10-18 01:05:36 +0000493void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000495}
John McCall17001972009-10-18 01:05:36 +0000496void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000498 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
502 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
503 Record.push_back(TL.getSizeExpr() ? 1 : 0);
504 if (TL.getSizeExpr())
505 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000506}
John McCall17001972009-10-18 01:05:36 +0000507void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
508 VisitArrayTypeLoc(TL);
509}
510void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
511 VisitArrayTypeLoc(TL);
512}
513void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
514 VisitArrayTypeLoc(TL);
515}
516void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
517 DependentSizedArrayTypeLoc TL) {
518 VisitArrayTypeLoc(TL);
519}
520void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
521 DependentSizedExtVectorTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
527void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000531 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000532 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
533 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000534 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000535 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
536 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000537}
538void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
539 VisitFunctionTypeLoc(TL);
540}
541void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
542 VisitFunctionTypeLoc(TL);
543}
John McCallb96ec562009-12-04 22:46:56 +0000544void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
John McCall17001972009-10-18 01:05:36 +0000547void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
550void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000551 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
552 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
553 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000554}
555void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000556 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
557 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
558 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
559 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000560}
561void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
Alexis Hunte852b102011-05-24 22:41:36 +0000564void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
565 Writer.AddSourceLocation(TL.getKWLoc(), Record);
566 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
567 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
568 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
569}
Richard Smith30482bc2011-02-20 03:19:35 +0000570void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCall17001972009-10-18 01:05:36 +0000573void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
576void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578}
John McCall81904512011-01-06 01:58:22 +0000579void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
581 if (TL.hasAttrOperand()) {
582 SourceRange range = TL.getAttrOperandParensRange();
583 Writer.AddSourceLocation(range.getBegin(), Record);
584 Writer.AddSourceLocation(range.getEnd(), Record);
585 }
586 if (TL.hasAttrExprOperand()) {
587 Expr *operand = TL.getAttrExprOperand();
588 Record.push_back(operand ? 1 : 0);
589 if (operand) Writer.AddStmt(operand);
590 } else if (TL.hasAttrEnumOperand()) {
591 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
592 }
593}
John McCall17001972009-10-18 01:05:36 +0000594void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getNameLoc(), Record);
596}
John McCallcebee162009-10-18 09:09:24 +0000597void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
598 SubstTemplateTypeParmTypeLoc TL) {
599 Writer.AddSourceLocation(TL.getNameLoc(), Record);
600}
Douglas Gregorada4b792011-01-14 02:55:32 +0000601void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
602 SubstTemplateTypeParmPackTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
604}
John McCall17001972009-10-18 01:05:36 +0000605void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
606 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000607 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000608 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
609 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
610 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
611 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000612 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
613 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000614}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000615void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
617 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
618}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000619void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000620 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000621 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000622}
John McCalle78aac42010-03-10 03:28:59 +0000623void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
624 Writer.AddSourceLocation(TL.getNameLoc(), Record);
625}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000626void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000627 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000628 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000629 Writer.AddSourceLocation(TL.getNameLoc(), Record);
630}
John McCallc392f372010-06-11 00:33:02 +0000631void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
632 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000633 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000634 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000635 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000636 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000637 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
638 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
639 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000640 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
641 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000642}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000643void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
644 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
645}
John McCall17001972009-10-18 01:05:36 +0000646void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
647 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000648}
649void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
650 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000651 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
652 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
653 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
654 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000655}
John McCallfc93cf92009-10-22 22:37:11 +0000656void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
657 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000658}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000659void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
660 Writer.AddSourceLocation(TL.getKWLoc(), Record);
661 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
662 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
663}
John McCall8f115c62009-10-16 21:56:05 +0000664
Richard Smith01b2cb42014-07-26 06:37:51 +0000665void ASTWriter::WriteTypeAbbrevs() {
666 using namespace llvm;
667
668 BitCodeAbbrev *Abv;
669
670 // Abbreviation for TYPE_EXT_QUAL
671 Abv = new BitCodeAbbrev();
672 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
673 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
674 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
675 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
676
677 // Abbreviation for TYPE_FUNCTION_PROTO
678 Abv = new BitCodeAbbrev();
679 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
680 // FunctionType
681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
682 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
683 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
684 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
685 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
686 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
687 // FunctionProtoType
688 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
689 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
690 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
691 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
692 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
694 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
695 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
696 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
697}
698
Chris Lattner19cea4e2009-04-22 05:57:30 +0000699//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000700// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000701//===----------------------------------------------------------------------===//
702
Chris Lattner28fa4e62009-04-26 22:26:21 +0000703static void EmitBlockID(unsigned ID, const char *Name,
704 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000705 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000706 Record.clear();
707 Record.push_back(ID);
708 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
709
710 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000711 if (!Name || Name[0] == 0)
712 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000713 Record.clear();
714 while (*Name)
715 Record.push_back(*Name++);
716 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
717}
718
719static void EmitRecordID(unsigned ID, const char *Name,
720 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000721 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000722 Record.clear();
723 Record.push_back(ID);
724 while (*Name)
725 Record.push_back(*Name++);
726 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000727}
728
729static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000730 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000731#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000732 RECORD(STMT_STOP);
733 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000734 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000735 RECORD(STMT_NULL);
736 RECORD(STMT_COMPOUND);
737 RECORD(STMT_CASE);
738 RECORD(STMT_DEFAULT);
739 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000740 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000741 RECORD(STMT_IF);
742 RECORD(STMT_SWITCH);
743 RECORD(STMT_WHILE);
744 RECORD(STMT_DO);
745 RECORD(STMT_FOR);
746 RECORD(STMT_GOTO);
747 RECORD(STMT_INDIRECT_GOTO);
748 RECORD(STMT_CONTINUE);
749 RECORD(STMT_BREAK);
750 RECORD(STMT_RETURN);
751 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000752 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000753 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000754 RECORD(EXPR_PREDEFINED);
755 RECORD(EXPR_DECL_REF);
756 RECORD(EXPR_INTEGER_LITERAL);
757 RECORD(EXPR_FLOATING_LITERAL);
758 RECORD(EXPR_IMAGINARY_LITERAL);
759 RECORD(EXPR_STRING_LITERAL);
760 RECORD(EXPR_CHARACTER_LITERAL);
761 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000762 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000763 RECORD(EXPR_UNARY_OPERATOR);
764 RECORD(EXPR_SIZEOF_ALIGN_OF);
765 RECORD(EXPR_ARRAY_SUBSCRIPT);
766 RECORD(EXPR_CALL);
767 RECORD(EXPR_MEMBER);
768 RECORD(EXPR_BINARY_OPERATOR);
769 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
770 RECORD(EXPR_CONDITIONAL_OPERATOR);
771 RECORD(EXPR_IMPLICIT_CAST);
772 RECORD(EXPR_CSTYLE_CAST);
773 RECORD(EXPR_COMPOUND_LITERAL);
774 RECORD(EXPR_EXT_VECTOR_ELEMENT);
775 RECORD(EXPR_INIT_LIST);
776 RECORD(EXPR_DESIGNATED_INIT);
777 RECORD(EXPR_IMPLICIT_VALUE_INIT);
778 RECORD(EXPR_VA_ARG);
779 RECORD(EXPR_ADDR_LABEL);
780 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000781 RECORD(EXPR_CHOOSE);
782 RECORD(EXPR_GNU_NULL);
783 RECORD(EXPR_SHUFFLE_VECTOR);
784 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000785 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000786 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000787 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000788 RECORD(EXPR_OBJC_ARRAY_LITERAL);
789 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000790 RECORD(EXPR_OBJC_ENCODE);
791 RECORD(EXPR_OBJC_SELECTOR_EXPR);
792 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
793 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
794 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
795 RECORD(EXPR_OBJC_KVC_REF_EXPR);
796 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000797 RECORD(STMT_OBJC_FOR_COLLECTION);
798 RECORD(STMT_OBJC_CATCH);
799 RECORD(STMT_OBJC_FINALLY);
800 RECORD(STMT_OBJC_AT_TRY);
801 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
802 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000803 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000804 RECORD(STMT_CXX_CATCH);
805 RECORD(STMT_CXX_TRY);
806 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000807 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000808 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000809 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000810 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000811 RECORD(EXPR_CXX_STATIC_CAST);
812 RECORD(EXPR_CXX_DYNAMIC_CAST);
813 RECORD(EXPR_CXX_REINTERPRET_CAST);
814 RECORD(EXPR_CXX_CONST_CAST);
815 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000816 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000817 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000818 RECORD(EXPR_CXX_BOOL_LITERAL);
819 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000820 RECORD(EXPR_CXX_TYPEID_EXPR);
821 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000822 RECORD(EXPR_CXX_THIS);
823 RECORD(EXPR_CXX_THROW);
824 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000825 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000826 RECORD(EXPR_CXX_BIND_TEMPORARY);
827 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
828 RECORD(EXPR_CXX_NEW);
829 RECORD(EXPR_CXX_DELETE);
830 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
831 RECORD(EXPR_EXPR_WITH_CLEANUPS);
832 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
833 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
834 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
835 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
836 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000837 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000838 RECORD(EXPR_CXX_NOEXCEPT);
839 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000840 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
841 RECORD(EXPR_TYPE_TRAIT);
842 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000843 RECORD(EXPR_PACK_EXPANSION);
844 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000845 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000846 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000847 RECORD(EXPR_FUNCTION_PARM_PACK);
848 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000849 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000850 RECORD(EXPR_CXX_UUIDOF_EXPR);
851 RECORD(EXPR_CXX_UUIDOF_TYPE);
852 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000853#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000854}
Mike Stump11289f42009-09-09 15:08:12 +0000855
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000856void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000857 RecordData Record;
858 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000859
Sebastian Redl539c5062010-08-18 23:57:32 +0000860#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
861#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000862
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000863 // Control Block.
864 BLOCK(CONTROL_BLOCK);
865 RECORD(METADATA);
Ben Langmuir487ea142014-10-23 18:05:36 +0000866 RECORD(SIGNATURE);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000867 RECORD(MODULE_NAME);
868 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000869 RECORD(IMPORTS);
870 RECORD(LANGUAGE_OPTIONS);
871 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000872 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000873 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000874 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000875 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000876 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000877 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000878 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000879 RECORD(PREPROCESSOR_OPTIONS);
880
Douglas Gregor108cb222012-10-19 00:45:00 +0000881 BLOCK(INPUT_FILES_BLOCK);
882 RECORD(INPUT_FILE);
883
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000884 // AST Top-Level Block.
885 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000886 RECORD(TYPE_OFFSET);
887 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000888 RECORD(IDENTIFIER_OFFSET);
889 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000890 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000891 RECORD(SPECIAL_TYPES);
892 RECORD(STATISTICS);
893 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000894 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000895 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000896 RECORD(SELECTOR_OFFSETS);
897 RECORD(METHOD_POOL);
898 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000899 RECORD(SOURCE_LOCATION_OFFSETS);
900 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000901 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000902 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000903 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000904 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000905 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000906 RECORD(SEMA_DECL_REFS);
907 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
908 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
909 RECORD(DECL_REPLACEMENTS);
910 RECORD(UPDATE_VISIBLE);
911 RECORD(DECL_UPDATE_OFFSETS);
912 RECORD(DECL_UPDATES);
913 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
914 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000915 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000916 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000917 RECORD(FP_PRAGMA_OPTIONS);
918 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000919 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000920 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000921 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000922 RECORD(MODULE_OFFSET_MAP);
923 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000924 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000925 RECORD(FILE_SORTED_DECLS);
926 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000927 RECORD(MERGED_DECLARATIONS);
928 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000929 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000930 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000931 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000932 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +0000933 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Douglas Gregor358cd442012-01-15 16:58:34 +0000934
Chris Lattner28fa4e62009-04-26 22:26:21 +0000935 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000936 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000937 RECORD(SM_SLOC_FILE_ENTRY);
938 RECORD(SM_SLOC_BUFFER_ENTRY);
939 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000940 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000941
Chris Lattner28fa4e62009-04-26 22:26:21 +0000942 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000943 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000944 RECORD(PP_MACRO_OBJECT_LIKE);
945 RECORD(PP_MACRO_FUNCTION_LIKE);
946 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000947
Douglas Gregor12bfa382009-10-17 00:13:19 +0000948 // Decls and Types block.
949 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000950 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000951 RECORD(TYPE_COMPLEX);
952 RECORD(TYPE_POINTER);
953 RECORD(TYPE_BLOCK_POINTER);
954 RECORD(TYPE_LVALUE_REFERENCE);
955 RECORD(TYPE_RVALUE_REFERENCE);
956 RECORD(TYPE_MEMBER_POINTER);
957 RECORD(TYPE_CONSTANT_ARRAY);
958 RECORD(TYPE_INCOMPLETE_ARRAY);
959 RECORD(TYPE_VARIABLE_ARRAY);
960 RECORD(TYPE_VECTOR);
961 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000962 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000963 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000964 RECORD(TYPE_TYPEDEF);
965 RECORD(TYPE_TYPEOF_EXPR);
966 RECORD(TYPE_TYPEOF);
967 RECORD(TYPE_RECORD);
968 RECORD(TYPE_ENUM);
969 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +0000970 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000971 RECORD(TYPE_DECLTYPE);
972 RECORD(TYPE_ELABORATED);
973 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
974 RECORD(TYPE_UNRESOLVED_USING);
975 RECORD(TYPE_INJECTED_CLASS_NAME);
976 RECORD(TYPE_OBJC_OBJECT);
977 RECORD(TYPE_TEMPLATE_TYPE_PARM);
978 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
979 RECORD(TYPE_DEPENDENT_NAME);
980 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
981 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
982 RECORD(TYPE_PAREN);
983 RECORD(TYPE_PACK_EXPANSION);
984 RECORD(TYPE_ATTRIBUTED);
985 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000986 RECORD(TYPE_AUTO);
987 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000988 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000989 RECORD(TYPE_DECAYED);
990 RECORD(TYPE_ADJUSTED);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000991 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +0000992 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000993 RECORD(DECL_ENUM);
994 RECORD(DECL_RECORD);
995 RECORD(DECL_ENUM_CONSTANT);
996 RECORD(DECL_FUNCTION);
997 RECORD(DECL_OBJC_METHOD);
998 RECORD(DECL_OBJC_INTERFACE);
999 RECORD(DECL_OBJC_PROTOCOL);
1000 RECORD(DECL_OBJC_IVAR);
1001 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001002 RECORD(DECL_OBJC_CATEGORY);
1003 RECORD(DECL_OBJC_CATEGORY_IMPL);
1004 RECORD(DECL_OBJC_IMPLEMENTATION);
1005 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1006 RECORD(DECL_OBJC_PROPERTY);
1007 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001008 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001009 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001010 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001011 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001012 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001013 RECORD(DECL_FILE_SCOPE_ASM);
1014 RECORD(DECL_BLOCK);
1015 RECORD(DECL_CONTEXT_LEXICAL);
1016 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001017 RECORD(DECL_NAMESPACE);
1018 RECORD(DECL_NAMESPACE_ALIAS);
1019 RECORD(DECL_USING);
1020 RECORD(DECL_USING_SHADOW);
1021 RECORD(DECL_USING_DIRECTIVE);
1022 RECORD(DECL_UNRESOLVED_USING_VALUE);
1023 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1024 RECORD(DECL_LINKAGE_SPEC);
1025 RECORD(DECL_CXX_RECORD);
1026 RECORD(DECL_CXX_METHOD);
1027 RECORD(DECL_CXX_CONSTRUCTOR);
1028 RECORD(DECL_CXX_DESTRUCTOR);
1029 RECORD(DECL_CXX_CONVERSION);
1030 RECORD(DECL_ACCESS_SPEC);
1031 RECORD(DECL_FRIEND);
1032 RECORD(DECL_FRIEND_TEMPLATE);
1033 RECORD(DECL_CLASS_TEMPLATE);
1034 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1035 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001036 RECORD(DECL_VAR_TEMPLATE);
1037 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1038 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001039 RECORD(DECL_FUNCTION_TEMPLATE);
1040 RECORD(DECL_TEMPLATE_TYPE_PARM);
1041 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1042 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1043 RECORD(DECL_STATIC_ASSERT);
1044 RECORD(DECL_CXX_BASE_SPECIFIERS);
1045 RECORD(DECL_INDIRECTFIELD);
1046 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1047
Douglas Gregor03412ba2011-06-03 02:27:19 +00001048 // Statements and Exprs can occur in the Decls and Types block.
1049 AddStmtsExprs(Stream, Record);
1050
Douglas Gregor92a96f52011-02-08 21:58:10 +00001051 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001052 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001053 RECORD(PPD_MACRO_DEFINITION);
1054 RECORD(PPD_INCLUSION_DIRECTIVE);
1055
Chris Lattner28fa4e62009-04-26 22:26:21 +00001056#undef RECORD
1057#undef BLOCK
1058 Stream.ExitBlock();
1059}
1060
Richard Smith54cc3c22014-12-11 20:50:24 +00001061/// \brief Prepares a path for being written to an AST file by converting it
1062/// to an absolute path and removing nested './'s.
1063///
1064/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001065static bool cleanPathForOutput(FileManager &FileMgr,
1066 SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00001067 bool Changed = false;
1068
1069 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
1070 llvm::sys::fs::make_absolute(Path);
1071 Changed = true;
1072 }
1073
1074 return Changed | FileMgr.removeDotPaths(Path);
1075}
1076
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001077/// \brief Adjusts the given filename to only write out the portion of the
1078/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001079///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001080/// \param Filename the file name to adjust.
1081///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001082/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1083/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001084///
1085/// \returns either the original filename (if it needs no adjustment) or the
1086/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001087static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001088adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001089 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001090
Richard Smith7ed1bc92014-12-05 22:42:13 +00001091 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001092 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001093
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001094 // Verify that the filename and the system root have the same prefix.
1095 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001096 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1097 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001098 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001099
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001100 // We hit the end of the filename before we hit the end of the system root.
1101 if (!Filename[Pos])
1102 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001103
Richard Smith7ed1bc92014-12-05 22:42:13 +00001104 // If there's not a path separator at the end of the base directory nor
1105 // immediately after it, then this isn't within the base directory.
1106 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1107 if (!llvm::sys::path::is_separator(BaseDir.back()))
1108 return Filename;
1109 } else {
1110 // If the file name has a '/' at the current position, skip over the '/'.
1111 // We distinguish relative paths from absolute paths by the
1112 // absence of '/' at the beginning of relative paths.
1113 //
1114 // FIXME: This is wrong. We distinguish them by asking if the path is
1115 // absolute, which isn't the same thing. And there might be multiple '/'s
1116 // in a row. Use a better mechanism to indicate whether we have emitted an
1117 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001118 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001119 }
Mike Stump11289f42009-09-09 15:08:12 +00001120
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001121 return Filename + Pos;
1122}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001123
Ben Langmuir487ea142014-10-23 18:05:36 +00001124static ASTFileSignature getSignature() {
1125 while (1) {
1126 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1127 return S;
1128 // Rely on GetRandomNumber to eventually return non-zero...
1129 }
1130}
1131
Douglas Gregor112b9072012-10-18 05:31:06 +00001132/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001133void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1134 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001135 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001136 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001137 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1138 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001139
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001140 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001141 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1142 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1143 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1144 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1145 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1146 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1147 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1148 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1149 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1150 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1151 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001152 Record.push_back(VERSION_MAJOR);
1153 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001154 Record.push_back(CLANG_VERSION_MAJOR);
1155 Record.push_back(CLANG_VERSION_MINOR);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001156 assert((!WritingModule || isysroot.empty()) &&
1157 "writing module as a relocatable PCH?");
Douglas Gregorc567ba22011-07-22 16:35:34 +00001158 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001159 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001160 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1161 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001162
Ben Langmuir487ea142014-10-23 18:05:36 +00001163 // Signature
1164 Record.clear();
1165 Record.push_back(getSignature());
1166 Stream.EmitRecord(SIGNATURE, Record);
1167
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001168 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001169 // Module name
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001170 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1171 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1173 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1174 RecordData Record;
1175 Record.push_back(MODULE_NAME);
1176 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1177 }
1178
Richard Smith7ed1bc92014-12-05 22:42:13 +00001179 if (WritingModule && WritingModule->Directory) {
1180 // Module directory.
1181 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1182 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1184 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1185 RecordData Record;
1186 Record.push_back(MODULE_DIRECTORY);
1187
1188 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001189 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001190 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1191
1192 // Write out all other paths relative to the base directory if possible.
1193 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1194 } else if (!isysroot.empty()) {
1195 // Write out paths relative to the sysroot if possible.
1196 BaseDirectory = isysroot;
1197 }
1198
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001199 // Module map file
1200 if (WritingModule) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001201 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001202
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001203 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1204
1205 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001206 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001207
1208 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001209 if (auto *AdditionalModMaps =
1210 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001211 Record.push_back(AdditionalModMaps->size());
1212 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001213 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001214 } else {
1215 Record.push_back(0);
1216 }
1217
1218 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001219 }
1220
Douglas Gregor112b9072012-10-18 05:31:06 +00001221 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001222 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001223 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001224 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001225
1226 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1227 M != MEnd; ++M) {
1228 // Skip modules that weren't directly imported.
1229 if (!(*M)->isDirectlyImported())
1230 continue;
1231
1232 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001233 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001234 Record.push_back((*M)->File->getSize());
1235 Record.push_back((*M)->File->getModificationTime());
Ben Langmuir487ea142014-10-23 18:05:36 +00001236 Record.push_back((*M)->Signature);
Richard Smith7ed1bc92014-12-05 22:42:13 +00001237 AddPath((*M)->FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001238 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001239 Stream.EmitRecord(IMPORTS, Record);
1240 }
Mike Stump11289f42009-09-09 15:08:12 +00001241
Douglas Gregor112b9072012-10-18 05:31:06 +00001242 // Language options.
1243 Record.clear();
1244 const LangOptions &LangOpts = Context.getLangOpts();
1245#define LANGOPT(Name, Bits, Default, Description) \
1246 Record.push_back(LangOpts.Name);
1247#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1248 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001249#include "clang/Basic/LangOptions.def"
1250#define SANITIZER(NAME, ID) \
1251 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001252#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001253
1254 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1255 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1256
1257 Record.push_back(LangOpts.CurrentModule.size());
1258 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001259
1260 // Comment options.
1261 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1262 for (CommentOptions::BlockCommandNamesTy::const_iterator
1263 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1264 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1265 I != IEnd; ++I) {
1266 AddString(*I, Record);
1267 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001268 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001269
Douglas Gregor112b9072012-10-18 05:31:06 +00001270 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1271
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001272 // Target options.
1273 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001274 const TargetInfo &Target = Context.getTargetInfo();
1275 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001276 AddString(TargetOpts.Triple, Record);
1277 AddString(TargetOpts.CPU, Record);
1278 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001279 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1280 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1281 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1282 }
1283 Record.push_back(TargetOpts.Features.size());
1284 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1285 AddString(TargetOpts.Features[I], Record);
1286 }
1287 Stream.EmitRecord(TARGET_OPTIONS, Record);
1288
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001289 // Diagnostic options.
1290 Record.clear();
1291 const DiagnosticOptions &DiagOpts
1292 = Context.getDiagnostics().getDiagnosticOptions();
1293#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1294#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1295 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1296#include "clang/Basic/DiagnosticOptions.def"
1297 Record.push_back(DiagOpts.Warnings.size());
1298 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1299 AddString(DiagOpts.Warnings[I], Record);
Richard Smith3be1cb22014-08-07 00:24:21 +00001300 Record.push_back(DiagOpts.Remarks.size());
1301 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1302 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001303 // Note: we don't serialize the log or serialization file names, because they
1304 // are generally transient files and will almost always be overridden.
1305 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1306
Douglas Gregorc6317db2012-10-24 15:49:58 +00001307 // File system options.
1308 Record.clear();
1309 const FileSystemOptions &FSOpts
1310 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1311 AddString(FSOpts.WorkingDir, Record);
1312 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1313
Douglas Gregor2d302362012-10-24 16:50:34 +00001314 // Header search options.
1315 Record.clear();
1316 const HeaderSearchOptions &HSOpts
1317 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1318 AddString(HSOpts.Sysroot, Record);
1319
1320 // Include entries.
1321 Record.push_back(HSOpts.UserEntries.size());
1322 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1323 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1324 AddString(Entry.Path, Record);
1325 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001326 Record.push_back(Entry.IsFramework);
1327 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001328 }
1329
1330 // System header prefixes.
1331 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1332 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1333 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1334 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1335 }
1336
1337 AddString(HSOpts.ResourceDir, Record);
1338 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001339 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001340 Record.push_back(HSOpts.DisableModuleHash);
1341 Record.push_back(HSOpts.UseBuiltinIncludes);
1342 Record.push_back(HSOpts.UseStandardSystemIncludes);
1343 Record.push_back(HSOpts.UseStandardCXXIncludes);
1344 Record.push_back(HSOpts.UseLibcxx);
1345 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1346
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001347 // Preprocessor options.
1348 Record.clear();
1349 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1350
1351 // Macro definitions.
1352 Record.push_back(PPOpts.Macros.size());
1353 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1354 AddString(PPOpts.Macros[I].first, Record);
1355 Record.push_back(PPOpts.Macros[I].second);
1356 }
1357
1358 // Includes
1359 Record.push_back(PPOpts.Includes.size());
1360 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1361 AddString(PPOpts.Includes[I], Record);
1362
1363 // Macro includes
1364 Record.push_back(PPOpts.MacroIncludes.size());
1365 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1366 AddString(PPOpts.MacroIncludes[I], Record);
1367
Douglas Gregorb6368752012-10-24 23:41:50 +00001368 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001369 // Detailed record is important since it is used for the module cache hash.
1370 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001371 AddString(PPOpts.ImplicitPCHInclude, Record);
1372 AddString(PPOpts.ImplicitPTHInclude, Record);
1373 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1374 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1375
Douglas Gregora3b20262011-05-06 21:43:30 +00001376 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001377 SourceManager &SM = Context.getSourceManager();
1378 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1379 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001380 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1381 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001382 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1383 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1384
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001385 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001386 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001387 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001388 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001389 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001390
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001391 Record.clear();
1392 Record.push_back(SM.getMainFileID().getOpaqueValue());
1393 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1394
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001395 // Original PCH directory
1396 if (!OutputFile.empty() && OutputFile != "-") {
1397 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1398 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1400 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1401
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001402 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001403
1404 llvm::sys::fs::make_absolute(OutputPath);
1405 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1406
1407 RecordData Record;
1408 Record.push_back(ORIGINAL_PCH_DIR);
1409 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1410 }
1411
Douglas Gregor49491f72013-03-15 22:15:07 +00001412 WriteInputFiles(Context.SourceMgr,
1413 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001414 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001415 Stream.ExitBlock();
1416}
1417
Douglas Gregor49491f72013-03-15 22:15:07 +00001418namespace {
1419 /// \brief An input file.
1420 struct InputFileEntry {
1421 const FileEntry *File;
1422 bool IsSystemFile;
1423 bool BufferOverridden;
1424 };
1425}
1426
1427void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1428 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001429 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001430 using namespace llvm;
1431 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1432 RecordData Record;
1433
1434 // Create input-file abbreviation.
1435 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1436 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001437 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001438 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1439 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001440 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001441 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1442 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1443
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001444 // Get all ContentCache objects for files, sorted by whether the file is a
1445 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001446 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001447 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1448 // Get this source location entry.
1449 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001450 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001451
1452 // We only care about file entries that were not overridden.
1453 if (!SLoc->isFile())
1454 continue;
1455 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001456 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001457 continue;
1458
Douglas Gregor49491f72013-03-15 22:15:07 +00001459 InputFileEntry Entry;
1460 Entry.File = Cache->OrigEntry;
1461 Entry.IsSystemFile = Cache->IsSystemFile;
1462 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001463 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001464 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001465 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001466 SortedFiles.push_front(Entry);
1467 }
1468
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001469 unsigned UserFilesNum = 0;
1470 // Write out all of the input files.
1471 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001472 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001473 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001474 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001475
Douglas Gregor49491f72013-03-15 22:15:07 +00001476 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001477 if (InputFileID != 0)
1478 continue; // already recorded this file.
1479
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001480 // Record this entry's offset.
1481 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001482
1483 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001484
Douglas Gregor49491f72013-03-15 22:15:07 +00001485 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001486 ++UserFilesNum;
1487
Douglas Gregor72be3902012-10-19 00:38:02 +00001488 Record.clear();
1489 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001490 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001491
1492 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001493 Record.push_back(Entry.File->getSize());
1494 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001495
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001496 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001497 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001498
Richard Smith7ed1bc92014-12-05 22:42:13 +00001499 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1500 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001501
Douglas Gregor112b9072012-10-18 05:31:06 +00001502 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001503
1504 // Create input file offsets abbreviation.
1505 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1506 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1507 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001508 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1509 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001510 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1511 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1512
1513 // Write input file offsets.
1514 Record.clear();
1515 Record.push_back(INPUT_FILE_OFFSETS);
1516 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001517 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001518 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001519}
1520
Douglas Gregora7f71a92009-04-10 03:52:48 +00001521//===----------------------------------------------------------------------===//
1522// Source Manager Serialization
1523//===----------------------------------------------------------------------===//
1524
1525/// \brief Create an abbreviation for the SLocEntry that refers to a
1526/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001527static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001528 using namespace llvm;
1529 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001530 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001535 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001540 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001541}
1542
1543/// \brief Create an abbreviation for the SLocEntry that refers to a
1544/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001545static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001546 using namespace llvm;
1547 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001548 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001554 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001555}
1556
1557/// \brief Create an abbreviation for the SLocEntry that refers to a
1558/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001559static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001560 using namespace llvm;
1561 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001562 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001564 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001565}
1566
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001567/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1568/// expansion.
1569static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001570 using namespace llvm;
1571 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001572 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001578 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001579}
1580
Douglas Gregor09b69892011-02-10 17:09:37 +00001581namespace {
1582 // Trait used for the on-disk hash table of header search information.
1583 class HeaderFileInfoTrait {
1584 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001585 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001586
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001587 // Keep track of the framework names we've used during serialization.
1588 SmallVector<char, 128> FrameworkStringData;
1589 llvm::StringMap<unsigned> FrameworkNameOffset;
1590
Douglas Gregor09b69892011-02-10 17:09:37 +00001591 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001592 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1593 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001594
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001595 struct key_type {
1596 const FileEntry *FE;
1597 const char *Filename;
1598 };
1599 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001600
1601 typedef HeaderFileInfo data_type;
1602 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001603 typedef unsigned hash_value_type;
1604 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001605
Justin Bogner25463f12014-04-18 20:27:24 +00001606 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001607 // The hash is based only on size/time of the file, so that the reader can
1608 // match even when symlinking or excess path elements ("foo/../", "../")
1609 // change the form of the name. However, complete path is still the key.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001610 //
1611 // FIXME: Using the mtime here will cause problems for explicit module
1612 // imports.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001613 return llvm::hash_combine(key.FE->getSize(),
1614 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001615 }
1616
1617 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001618 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001619 using namespace llvm::support;
1620 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001621 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001622 Writer.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001623 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001624 if (Data.isModuleHeader)
1625 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00001626 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001627 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001628 }
1629
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001630 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001631 using namespace llvm::support;
1632 endian::Writer<little> LE(Out);
1633 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001634 KeyLen -= 8;
Justin Bognere1c147c2014-03-28 22:03:19 +00001635 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001636 KeyLen -= 8;
1637 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001638 }
1639
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001640 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001641 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001642 using namespace llvm::support;
1643 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001644 uint64_t Start = Out.tell(); (void)Start;
1645
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001646 unsigned char Flags = (Data.HeaderRole << 6)
1647 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001648 | (Data.isPragmaOnce << 4)
1649 | (Data.DirInfo << 2)
1650 | (Data.Resolved << 1)
1651 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001652 LE.write<uint8_t>(Flags);
1653 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001654
1655 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001656 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001657 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001658 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001659
1660 unsigned Offset = 0;
1661 if (!Data.Framework.empty()) {
1662 // If this header refers into a framework, save the framework name.
1663 llvm::StringMap<unsigned>::iterator Pos
1664 = FrameworkNameOffset.find(Data.Framework);
1665 if (Pos == FrameworkNameOffset.end()) {
1666 Offset = FrameworkStringData.size() + 1;
1667 FrameworkStringData.append(Data.Framework.begin(),
1668 Data.Framework.end());
1669 FrameworkStringData.push_back(0);
1670
1671 FrameworkNameOffset[Data.Framework] = Offset;
1672 } else
1673 Offset = Pos->second;
1674 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001675 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001676
1677 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001678 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Justin Bognere1c147c2014-03-28 22:03:19 +00001679 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001680 }
1681
Douglas Gregor09b69892011-02-10 17:09:37 +00001682 assert(Out.tell() - Start == DataLen && "Wrong data length");
1683 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001684
1685 const char *strings_begin() const { return FrameworkStringData.begin(); }
1686 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001687 };
1688} // end anonymous namespace
1689
1690/// \brief Write the header search block for the list of files that
1691///
1692/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001693void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001694 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001695 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1696
1697 if (FilesByUID.size() > HS.header_file_size())
1698 FilesByUID.resize(HS.header_file_size());
1699
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001700 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001701 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001702 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001703 unsigned NumHeaderSearchEntries = 0;
1704 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1705 const FileEntry *File = FilesByUID[UID];
1706 if (!File)
1707 continue;
1708
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001709 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1710 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001711 HeaderFileInfo HFI;
1712 if (!HS.tryGetFileInfo(File, HFI) ||
1713 (HFI.External && Chain) ||
1714 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001715 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001716
Richard Smith7ed1bc92014-12-05 22:42:13 +00001717 // Massage the file path into an appropriate form.
Douglas Gregor09b69892011-02-10 17:09:37 +00001718 const char *Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001719 SmallString<128> FilenameTmp(Filename);
1720 if (PreparePathForOutput(FilenameTmp)) {
1721 // If we performed any translation on the file name at all, we need to
1722 // save this string, since the generator will refer to it later.
1723 Filename = strdup(FilenameTmp.c_str());
Douglas Gregor09b69892011-02-10 17:09:37 +00001724 SavedStrings.push_back(Filename);
1725 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001726
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001727 HeaderFileInfoTrait::key_type key = { File, Filename };
1728 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001729 ++NumHeaderSearchEntries;
1730 }
1731
1732 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001733 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001734 uint32_t BucketOffset;
1735 {
Justin Bognere1c147c2014-03-28 22:03:19 +00001736 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00001737 llvm::raw_svector_ostream Out(TableData);
1738 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00001739 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00001740 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1741 }
1742
1743 // Create a blob abbreviation
1744 using namespace llvm;
1745 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1746 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1751 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1752
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001753 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001754 RecordData Record;
1755 Record.push_back(HEADER_SEARCH_TABLE);
1756 Record.push_back(BucketOffset);
1757 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001758 Record.push_back(TableData.size());
1759 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001760 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1761
1762 // Free all of the strings we had to duplicate.
1763 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001764 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001765}
1766
Douglas Gregora7f71a92009-04-10 03:52:48 +00001767/// \brief Writes the block containing the serialized form of the
1768/// source manager.
1769///
1770/// TODO: We should probably use an on-disk hash table (stored in a
1771/// blob), indexed based on the file name, so that we only create
1772/// entries for files that we actually need. In the common case (no
1773/// errors), we probably won't have to create file entries for any of
1774/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001775void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001776 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001777 RecordData Record;
1778
Chris Lattner0910e3b2009-04-10 17:16:57 +00001779 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001780 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001781
1782 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001783 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1784 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1785 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001786 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001787
Douglas Gregor258ae542009-04-27 06:38:32 +00001788 // Write out the source location entry table. We skip the first
1789 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001790 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001791 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001792 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1793 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001794 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001795 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001796 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001797 FileID FID = FileID::get(I);
1798 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001799
Douglas Gregor258ae542009-04-27 06:38:32 +00001800 // Record the offset of this source-location entry.
1801 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1802
1803 // Figure out which record code to use.
1804 unsigned Code;
1805 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001806 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1807 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001808 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001809 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001810 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001811 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001812 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001813 Record.clear();
1814 Record.push_back(Code);
1815
Douglas Gregor925296b2011-07-19 16:10:42 +00001816 // Starting offset of this entry within this module, so skip the dummy.
1817 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001818 if (SLoc->isFile()) {
1819 const SrcMgr::FileInfo &File = SLoc->getFile();
1820 Record.push_back(File.getIncludeLoc().getRawEncoding());
1821 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1822 Record.push_back(File.hasLineDirectives());
1823
1824 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001825 if (Content->OrigEntry) {
1826 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001827 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001828
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001829 // The source location entry is a file. Emit input file ID.
1830 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1831 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001832
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001833 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001834
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001835 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001836 if (FDI != FileDeclIDs.end()) {
1837 Record.push_back(FDI->second->FirstDeclIndex);
1838 Record.push_back(FDI->second->DeclIDs.size());
1839 } else {
1840 Record.push_back(0);
1841 Record.push_back(0);
1842 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001843
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001844 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001845
1846 if (Content->BufferOverridden) {
1847 Record.clear();
1848 Record.push_back(SM_SLOC_BUFFER_BLOB);
1849 const llvm::MemoryBuffer *Buffer
1850 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1851 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1852 StringRef(Buffer->getBufferStart(),
1853 Buffer->getBufferSize() + 1));
1854 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001855 } else {
1856 // The source location entry is a buffer. The blob associated
1857 // with this entry contains the contents of the buffer.
1858
1859 // We add one to the size so that we capture the trailing NULL
1860 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1861 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001862 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001863 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001864 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001865 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001866 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001867 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001868 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001869 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001870 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001871 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001872
Douglas Gregor925296b2011-07-19 16:10:42 +00001873 if (strcmp(Name, "<built-in>") == 0) {
1874 PreloadSLocs.push_back(SLocEntryOffsets.size());
1875 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001876 }
1877 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001878 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001879 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001880 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1881 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001882 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1883 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001884
1885 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001886 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001887 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001888 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001889 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001890 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001891 }
1892 }
1893
Douglas Gregor8f45df52009-04-16 22:23:12 +00001894 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001895
1896 if (SLocEntryOffsets.empty())
1897 return;
1898
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001899 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001900 // table is used for lazily loading source-location information.
1901 using namespace llvm;
1902 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001903 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1907 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001908
Douglas Gregor258ae542009-04-27 06:38:32 +00001909 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001910 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001911 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001912 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001913 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001914
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001915 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001916 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001917 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001918
1919 // Write the line table. It depends on remapping working, so it must come
1920 // after the source location offsets.
1921 if (SourceMgr.hasLineTable()) {
1922 LineTableInfo &LineTable = SourceMgr.getLineTable();
1923
1924 Record.clear();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001925 // Emit the file names.
Douglas Gregor925296b2011-07-19 16:10:42 +00001926 Record.push_back(LineTable.getNumFilenames());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001927 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I)
1928 AddPath(LineTable.getFilename(I), Record);
Douglas Gregor925296b2011-07-19 16:10:42 +00001929
1930 // Emit the line entries
1931 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1932 L != LEnd; ++L) {
1933 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001934 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001935 continue;
1936
1937 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001938 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001939
1940 // Emit the line entries
1941 Record.push_back(L->second.size());
1942 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1943 LEEnd = L->second.end();
1944 LE != LEEnd; ++LE) {
1945 Record.push_back(LE->FileOffset);
1946 Record.push_back(LE->LineNo);
1947 Record.push_back(LE->FilenameID);
1948 Record.push_back((unsigned)LE->FileKind);
1949 Record.push_back(LE->IncludeOffset);
1950 }
1951 }
1952 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1953 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001954}
1955
Douglas Gregorc5046832009-04-27 18:38:38 +00001956//===----------------------------------------------------------------------===//
1957// Preprocessor Serialization
1958//===----------------------------------------------------------------------===//
1959
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001960namespace {
1961class ASTMacroTableTrait {
1962public:
1963 typedef IdentID key_type;
1964 typedef key_type key_type_ref;
1965
1966 struct Data {
1967 uint32_t MacroDirectivesOffset;
1968 };
1969
1970 typedef Data data_type;
1971 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001972 typedef unsigned hash_value_type;
1973 typedef unsigned offset_type;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001974
Justin Bogner25463f12014-04-18 20:27:24 +00001975 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001976 return llvm::hash_value(IdID);
1977 }
1978
1979 std::pair<unsigned,unsigned>
1980 static EmitKeyDataLength(raw_ostream& Out,
1981 key_type_ref Key, data_type_ref Data) {
1982 unsigned KeyLen = 4; // IdentID.
1983 unsigned DataLen = 4; // MacroDirectivesOffset.
1984 return std::make_pair(KeyLen, DataLen);
1985 }
1986
1987 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001988 using namespace llvm::support;
1989 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001990 }
1991
1992 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1993 unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001994 using namespace llvm::support;
1995 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001996 }
1997};
1998} // end anonymous namespace
1999
Benjamin Kramer04bf1872013-09-22 14:10:29 +00002000static int compareMacroDirectives(
2001 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
2002 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
2003 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002004}
2005
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002006static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2007 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002008 if (MacroInfo *MI = MD->getMacroInfo())
2009 if (MI->isBuiltinMacro())
2010 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002011
2012 if (IsModule) {
Richard Smithe657bbd2014-07-18 22:13:40 +00002013 // Re-export any imported directives.
Richard Smithdaa69e02014-07-25 04:40:03 +00002014 if (MD->isImported())
2015 return false;
Richard Smithe657bbd2014-07-18 22:13:40 +00002016
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002017 SourceLocation Loc = MD->getLocation();
2018 if (Loc.isInvalid())
2019 return true;
2020 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2021 return true;
2022 }
2023
2024 return false;
2025}
2026
Chris Lattnereeffaef2009-04-10 17:15:23 +00002027/// \brief Writes the block containing the serialized form of the
2028/// preprocessor.
2029///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002030void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002031 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2032 if (PPRec)
2033 WritePreprocessorDetail(*PPRec);
2034
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002035 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002036
Chris Lattner0af3ba12009-04-13 01:29:17 +00002037 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2038 if (PP.getCounterValue() != 0) {
2039 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002040 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00002041 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00002042 }
2043
2044 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002045 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002046
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002047 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002048 // FIXME: use diagnostics subsystem for localization etc.
2049 if (PP.SawDateOrTime())
2050 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00002051
Douglas Gregor796d76a2010-10-20 22:00:55 +00002052
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002053 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002054 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002055
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002056 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002057 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002058 MacroDirectives;
2059 for (Preprocessor::macro_iterator
2060 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2061 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002062 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002063 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002064 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002065
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002066 // Sort the set of macro definitions that need to be serialized by the
2067 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002068 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2069 &compareMacroDirectives);
2070
Justin Bognerbb094f02014-04-18 19:57:06 +00002071 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002072
2073 // Emit the macro directives as a list and associate the offset with the
2074 // identifier they belong to.
2075 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2076 const IdentifierInfo *Name = MacroDirectives[I].first;
2077 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2078 MacroDirective *MD = MacroDirectives[I].second;
2079
2080 // If the macro or identifier need no updates, don't write the macro history
2081 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002082 // FIXME: Chain the macro history instead of re-writing it.
2083 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002084 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2085 continue;
2086
2087 // Emit the macro directives in reverse source order.
2088 for (; MD; MD = MD->getPrevious()) {
2089 if (shouldIgnoreMacro(MD, IsModule, PP))
2090 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002091
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002092 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002093 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002094 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002095 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2096 Record.push_back(InfoID);
Richard Smithdaa69e02014-07-25 04:40:03 +00002097 Record.push_back(DefMD->getOwningModuleID());
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002098 Record.push_back(DefMD->isAmbiguous());
Richard Smithdaa69e02014-07-25 04:40:03 +00002099 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2100 Record.push_back(UndefMD->getOwningModuleID());
2101 } else {
2102 auto *VisMD = cast<VisibilityMacroDirective>(MD);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002103 Record.push_back(VisMD->isPublic());
2104 }
Richard Smithdaa69e02014-07-25 04:40:03 +00002105
2106 if (MD->isImported()) {
2107 auto Overrides = MD->getOverriddenModules();
2108 Record.push_back(Overrides.size());
2109 for (auto Override : Overrides)
2110 Record.push_back(Override);
2111 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002112 }
2113 if (Record.empty())
2114 continue;
2115
2116 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2117 Record.clear();
2118
2119 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2120
2121 IdentID NameID = getIdentifierRef(Name);
2122 ASTMacroTableTrait::Data data;
2123 data.MacroDirectivesOffset = MacroDirectiveOffset;
2124 Generator.insert(NameID, data);
2125 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002126
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002127 /// \brief Offsets of each of the macros into the bitstream, indexed by
2128 /// the local macro ID
2129 ///
2130 /// For each identifier that is associated with a macro, this map
2131 /// provides the offset into the bitstream where that macro is
2132 /// defined.
2133 std::vector<uint32_t> MacroOffsets;
2134
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002135 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2136 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2137 MacroInfo *MI = MacroInfosToEmit[I].MI;
2138 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002139
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002140 if (ID < FirstMacroID) {
2141 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2142 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002143 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002144
2145 // Record the local offset of this macro.
2146 unsigned Index = ID - FirstMacroID;
2147 if (Index == MacroOffsets.size())
2148 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2149 else {
2150 if (Index > MacroOffsets.size())
2151 MacroOffsets.resize(Index + 1);
2152
2153 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2154 }
2155
2156 AddIdentifierRef(Name, Record);
2157 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2158 AddSourceLocation(MI->getDefinitionLoc(), Record);
2159 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2160 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002161 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002162 unsigned Code;
2163 if (MI->isObjectLike()) {
2164 Code = PP_MACRO_OBJECT_LIKE;
2165 } else {
2166 Code = PP_MACRO_FUNCTION_LIKE;
2167
2168 Record.push_back(MI->isC99Varargs());
2169 Record.push_back(MI->isGNUVarargs());
2170 Record.push_back(MI->hasCommaPasting());
2171 Record.push_back(MI->getNumArgs());
2172 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2173 I != E; ++I)
2174 AddIdentifierRef(*I, Record);
2175 }
2176
2177 // If we have a detailed preprocessing record, record the macro definition
2178 // ID that corresponds to this macro.
2179 if (PPRec)
2180 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2181
2182 Stream.EmitRecord(Code, Record);
2183 Record.clear();
2184
2185 // Emit the tokens array.
2186 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2187 // Note that we know that the preprocessor does not have any annotation
2188 // tokens in it because they are created by the parser, and thus can't
2189 // be in a macro definition.
2190 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002191 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002192 Stream.EmitRecord(PP_TOKEN, Record);
2193 Record.clear();
2194 }
2195 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002196 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002197
Douglas Gregor92a96f52011-02-08 21:58:10 +00002198 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002199
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002200 // Create the on-disk hash table in a buffer.
2201 SmallString<4096> MacroTable;
2202 uint32_t BucketOffset;
2203 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002204 using namespace llvm::support;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002205 llvm::raw_svector_ostream Out(MacroTable);
2206 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002207 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002208 BucketOffset = Generator.Emit(Out);
2209 }
2210
2211 // Write the macro table
2212 using namespace llvm;
2213 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2214 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2217 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2218
2219 Record.push_back(MACRO_TABLE);
2220 Record.push_back(BucketOffset);
2221 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2222 Record.clear();
2223
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002224 // Write the offsets table for macro IDs.
2225 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002226 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002227 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2231
2232 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2233 Record.clear();
2234 Record.push_back(MACRO_OFFSET);
2235 Record.push_back(MacroOffsets.size());
2236 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2237 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2238 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002239}
2240
2241void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002242 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002243 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002244
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002245 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002246
Douglas Gregor92a96f52011-02-08 21:58:10 +00002247 // Enter the preprocessor block.
2248 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002249
Douglas Gregoraae92242010-03-19 21:51:54 +00002250 // If the preprocessor has a preprocessing record, emit it.
2251 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002252 using namespace llvm;
2253
2254 // Set up the abbreviation for
2255 unsigned InclusionAbbrev = 0;
2256 {
2257 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2258 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2264 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2265 }
2266
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002267 unsigned FirstPreprocessorEntityID
2268 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2269 + NUM_PREDEF_PP_ENTITY_IDS;
2270 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002271 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002272 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2273 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002274 E != EEnd;
2275 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002276 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002277
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002278 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2279 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002280
Douglas Gregor92a96f52011-02-08 21:58:10 +00002281 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002282 // Record this macro definition's ID.
2283 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002284
Douglas Gregor92a96f52011-02-08 21:58:10 +00002285 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002286 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2287 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002288 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002289
Chandler Carrutha88a22182011-07-14 08:20:46 +00002290 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002291 Record.push_back(ME->isBuiltinMacro());
2292 if (ME->isBuiltinMacro())
2293 AddIdentifierRef(ME->getName(), Record);
2294 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002295 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002296 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002297 continue;
2298 }
2299
2300 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2301 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002302 Record.push_back(ID->getFileName().size());
2303 Record.push_back(ID->wasInQuotes());
2304 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002305 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002306 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002307 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002308 // Check that the FileEntry is not null because it was not resolved and
2309 // we create a PCH even with compiler errors.
2310 if (ID->getFile())
2311 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002312 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2313 continue;
2314 }
2315
2316 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2317 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002318 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002319
Douglas Gregoraae92242010-03-19 21:51:54 +00002320 // Write the offsets table for the preprocessing record.
2321 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002322 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2323
Douglas Gregoraae92242010-03-19 21:51:54 +00002324 // Write the offsets table for identifier IDs.
2325 using namespace llvm;
2326 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002327 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002330 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002331
Douglas Gregoraae92242010-03-19 21:51:54 +00002332 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002333 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002334 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002335 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2336 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002337 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002338}
2339
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002340unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2341 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2342 if (Known != SubmoduleIDs.end())
2343 return Known->second;
2344
2345 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2346}
2347
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002348unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2349 if (!Mod)
2350 return 0;
2351
2352 llvm::DenseMap<Module *, unsigned>::const_iterator
2353 Known = SubmoduleIDs.find(Mod);
2354 if (Known != SubmoduleIDs.end())
2355 return Known->second;
2356
2357 return 0;
2358}
2359
Douglas Gregor253eefe2011-12-01 00:59:36 +00002360/// \brief Compute the number of modules within the given tree (including the
2361/// given module).
2362static unsigned getNumberOfModules(Module *Mod) {
2363 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002364 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2365 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002366 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002367 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002368
2369 return ChildModules + 1;
2370}
2371
Douglas Gregorde3ef502011-11-30 23:21:26 +00002372void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002373 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002374 // FIXME: This feels like it belongs somewhere else, but there are no
2375 // other consumers of this information.
2376 SourceManager &SrcMgr = PP->getSourceManager();
2377 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002378 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002379 if (Module *ImportedFrom
2380 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2381 SrcMgr))) {
2382 ImportedFrom->Imports.push_back(I->getImportedModule());
2383 }
2384 }
2385
Douglas Gregor69021972011-11-30 17:33:56 +00002386 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002387 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002388
2389 // Write the abbreviations needed for the submodules block.
2390 using namespace llvm;
2391 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2392 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2404 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2405
2406 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002407 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2409 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2410
2411 Abbrev = new BitCodeAbbrev();
2412 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2414 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002415
2416 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002417 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2419 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2420
2421 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002422 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2424 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2425
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002426 Abbrev = new BitCodeAbbrev();
2427 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002430 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2431
Douglas Gregor59527662012-10-15 06:28:11 +00002432 Abbrev = new BitCodeAbbrev();
2433 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2434 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2435 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2436
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002437 Abbrev = new BitCodeAbbrev();
Richard Smith306d8922014-10-22 23:50:56 +00002438 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2440 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2441
2442 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002443 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2445 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2446
2447 Abbrev = new BitCodeAbbrev();
Richard Smith202210b2014-10-24 20:23:01 +00002448 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2449 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2450 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2451
2452 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002453 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2456 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2457
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002458 Abbrev = new BitCodeAbbrev();
2459 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2461 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2462
Douglas Gregorfb912652013-03-20 21:10:35 +00002463 Abbrev = new BitCodeAbbrev();
2464 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2466 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2467 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2468
Douglas Gregor253eefe2011-12-01 00:59:36 +00002469 // Write the submodule metadata block.
2470 RecordData Record;
2471 Record.push_back(getNumberOfModules(WritingModule));
2472 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2473 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2474
Douglas Gregor69021972011-11-30 17:33:56 +00002475 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002476 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002477 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002478 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002479 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002480 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002481 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002482
2483 // Emit the definition of the block.
2484 Record.clear();
2485 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002486 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002487 if (Mod->Parent) {
2488 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2489 Record.push_back(SubmoduleIDs[Mod->Parent]);
2490 } else {
2491 Record.push_back(0);
2492 }
2493 Record.push_back(Mod->IsFramework);
2494 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002495 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002496 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002497 Record.push_back(Mod->InferSubmodules);
2498 Record.push_back(Mod->InferExplicitSubmodules);
2499 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002500 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002501 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2502
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002503 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002504 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002505 Record.clear();
2506 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002507 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002508 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002509 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002510 }
2511
Douglas Gregor69021972011-11-30 17:33:56 +00002512 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002513 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002514 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002515 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002516 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002517 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002518 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2519 Record.clear();
2520 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2521 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2522 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002523 }
Richard Smith306d8922014-10-22 23:50:56 +00002524
Douglas Gregor69021972011-11-30 17:33:56 +00002525 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002526 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002527 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002528 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002529 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002530 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002531 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2532 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2533 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002534 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002535 Module::HK_PrivateTextual},
2536 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002537 };
2538 for (auto &HL : HeaderLists) {
Douglas Gregor69021972011-11-30 17:33:56 +00002539 Record.clear();
Richard Smith3c1a41a2014-12-02 00:08:08 +00002540 Record.push_back(HL.RecordKind);
2541 for (auto &H : Mod->Headers[HL.HeaderKind])
2542 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2543 }
2544
2545 // Emit the top headers.
2546 {
2547 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2548 Record.clear();
2549 Record.push_back(SUBMODULE_TOPHEADER);
2550 for (auto *H : TopHeaders)
2551 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002552 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002553
2554 // Emit the imports.
2555 if (!Mod->Imports.empty()) {
2556 Record.clear();
2557 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002558 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00002559 assert(ImportedID && "Unknown submodule!");
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002560 Record.push_back(ImportedID);
2561 }
2562 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2563 }
2564
Douglas Gregor24bb9232011-12-02 18:58:38 +00002565 // Emit the exports.
2566 if (!Mod->Exports.empty()) {
2567 Record.clear();
2568 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002569 if (Module *Exported = Mod->Exports[I].getPointer()) {
2570 unsigned ExportedID = SubmoduleIDs[Exported];
2571 assert(ExportedID > 0 && "Unknown submodule ID?");
2572 Record.push_back(ExportedID);
2573 } else {
2574 Record.push_back(0);
2575 }
2576
Douglas Gregor24bb9232011-12-02 18:58:38 +00002577 Record.push_back(Mod->Exports[I].getInt());
2578 }
2579 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2580 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002581
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002582 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2583 // Might be unnecessary as use declarations are only used to build the
2584 // module itself.
2585
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002586 // Emit the link libraries.
2587 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2588 Record.clear();
2589 Record.push_back(SUBMODULE_LINK_LIBRARY);
2590 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2591 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2592 Mod->LinkLibraries[I].Library);
2593 }
2594
Douglas Gregorfb912652013-03-20 21:10:35 +00002595 // Emit the conflicts.
2596 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2597 Record.clear();
2598 Record.push_back(SUBMODULE_CONFLICT);
2599 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2600 assert(OtherID && "Unknown submodule!");
2601 Record.push_back(OtherID);
2602 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2603 Mod->Conflicts[I].Message);
2604 }
2605
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002606 // Emit the configuration macros.
2607 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2608 Record.clear();
2609 Record.push_back(SUBMODULE_CONFIG_MACRO);
2610 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2611 Mod->ConfigMacros[I]);
2612 }
2613
Douglas Gregor69021972011-11-30 17:33:56 +00002614 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002615 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2616 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002617 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002618 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002619 }
2620
2621 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002622
2623 assert((NextSubmoduleID - FirstSubmoduleID
2624 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002625}
2626
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002627serialization::SubmoduleID
2628ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002629 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002630 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002631
2632 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002633 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002634 Module *OwningMod
2635 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002636 if (!OwningMod)
2637 return 0;
2638
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002639 // Check whether this submodule is part of our own module.
2640 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002641 return 0;
2642
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002643 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002644}
2645
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002646void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2647 bool isModule) {
2648 // Make sure set diagnostic pragmas don't affect the translation unit that
2649 // imports the module.
2650 // FIXME: Make diagnostic pragma sections work properly with modules.
2651 if (isModule)
2652 return;
2653
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002654 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2655 DiagStateIDMap;
2656 unsigned CurrID = 0;
2657 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002658 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002659 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002660 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2661 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002662 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002663 if (point.Loc.isInvalid())
2664 continue;
2665
2666 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002667 unsigned &DiagStateID = DiagStateIDMap[point.State];
2668 Record.push_back(DiagStateID);
2669
2670 if (DiagStateID == 0) {
2671 DiagStateID = ++CurrID;
2672 for (DiagnosticsEngine::DiagState::const_iterator
2673 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2674 if (I->second.isPragma()) {
2675 Record.push_back(I->first);
Alp Toker46df1c02014-06-12 10:15:20 +00002676 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002677 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002678 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002679 Record.push_back(-1); // mark the end of the diag/map pairs for this
2680 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002681 }
2682 }
2683
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002684 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002685 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002686}
2687
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002688void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2689 if (CXXBaseSpecifiersOffsets.empty())
2690 return;
2691
2692 RecordData Record;
2693
2694 // Create a blob abbreviation for the C++ base specifiers offsets.
2695 using namespace llvm;
2696
2697 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2698 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2701 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2702
Douglas Gregorc27b2872011-08-04 00:01:48 +00002703 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002704 Record.clear();
2705 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2706 Record.push_back(CXXBaseSpecifiersOffsets.size());
2707 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002708 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002709}
2710
Douglas Gregorc5046832009-04-27 18:38:38 +00002711//===----------------------------------------------------------------------===//
2712// Type Serialization
2713//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002714
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002715/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002716void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002717 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002718 if (Idx.getIndex() == 0) // we haven't seen this type before.
2719 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002720
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002721 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002722
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002723 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002724 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002725 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002726 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002727 else if (TypeOffsets.size() < Index) {
2728 TypeOffsets.resize(Index + 1);
2729 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002730 }
2731
2732 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002733
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002734 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002735 ASTTypeWriter W(*this, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +00002736 W.AbbrevToUse = 0;
John McCall8ccfcb52009-09-24 19:53:00 +00002737
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002738 if (T.hasLocalNonFastQualifiers()) {
2739 Qualifiers Qs = T.getLocalQualifiers();
2740 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002741 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002742 W.Code = TYPE_EXT_QUAL;
Richard Smith01b2cb42014-07-26 06:37:51 +00002743 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall8ccfcb52009-09-24 19:53:00 +00002744 } else {
2745 switch (T->getTypeClass()) {
2746 // For all of the concrete, non-dependent types, call the
2747 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002748#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002749 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002750#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002751#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002752 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002753 }
2754
2755 // Emit the serialized record.
Richard Smith01b2cb42014-07-26 06:37:51 +00002756 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002757
2758 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002759 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002760}
2761
Douglas Gregorc5046832009-04-27 18:38:38 +00002762//===----------------------------------------------------------------------===//
2763// Declaration Serialization
2764//===----------------------------------------------------------------------===//
2765
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002766/// \brief Write the block containing all of the declaration IDs
2767/// lexically declared within the given DeclContext.
2768///
2769/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2770/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002771uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002772 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002773 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002774 return 0;
2775
Douglas Gregor8f45df52009-04-16 22:23:12 +00002776 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002777 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002778 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002779 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002780 for (const auto *D : DC->decls())
2781 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002782
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002783 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002784 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002785 return Offset;
2786}
2787
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002788void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002789 using namespace llvm;
2790 RecordData Record;
2791
2792 // Write the type offsets array
2793 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002794 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2798 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2799 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002800 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002801 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002802 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002803 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002804
2805 // Write the declaration offsets array
2806 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002807 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2811 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2812 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002813 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002814 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002815 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002816 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002817}
2818
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002819void ASTWriter::WriteFileDeclIDsMap() {
2820 using namespace llvm;
2821 RecordData Record;
2822
2823 // Join the vectors of DeclIDs from all files.
2824 SmallVector<DeclID, 256> FileSortedIDs;
2825 for (FileDeclIDsTy::iterator
2826 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2827 DeclIDInFileInfo &Info = *FI->second;
2828 Info.FirstDeclIndex = FileSortedIDs.size();
2829 for (LocDeclIDsTy::iterator
2830 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2831 FileSortedIDs.push_back(DI->second);
2832 }
2833
2834 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2835 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002837 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2838 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2839 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002840 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002841 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2842}
2843
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002844void ASTWriter::WriteComments() {
2845 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002846 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002847 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002848 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2849 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002850 I != E; ++I) {
2851 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002852 AddSourceRange((*I)->getSourceRange(), Record);
2853 Record.push_back((*I)->getKind());
2854 Record.push_back((*I)->isTrailingComment());
2855 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002856 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2857 }
2858 Stream.ExitBlock();
2859}
2860
Douglas Gregorc5046832009-04-27 18:38:38 +00002861//===----------------------------------------------------------------------===//
2862// Global Method Pool and Selector Serialization
2863//===----------------------------------------------------------------------===//
2864
Douglas Gregore84a9da2009-04-20 20:36:09 +00002865namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002866// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002867class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002868 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002869
2870public:
2871 typedef Selector key_type;
2872 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002873
Sebastian Redl834bb972010-08-04 17:20:04 +00002874 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002875 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002876 ObjCMethodList Instance, Factory;
2877 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002878 typedef const data_type& data_type_ref;
2879
Justin Bogner25463f12014-04-18 20:27:24 +00002880 typedef unsigned hash_value_type;
2881 typedef unsigned offset_type;
2882
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002883 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002884
Justin Bogner25463f12014-04-18 20:27:24 +00002885 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002886 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002887 }
Mike Stump11289f42009-09-09 15:08:12 +00002888
2889 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002890 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002891 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002892 using namespace llvm::support;
2893 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002894 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00002895 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002896 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2897 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002898 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002899 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002900 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002901 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002902 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002903 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002904 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00002905 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002906 return std::make_pair(KeyLen, DataLen);
2907 }
Mike Stump11289f42009-09-09 15:08:12 +00002908
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002909 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002910 using namespace llvm::support;
2911 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00002912 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002913 assert((Start >> 32) == 0 && "Selector key offset too large");
2914 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002915 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00002916 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002917 if (N == 0)
2918 N = 1;
2919 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00002920 LE.write<uint32_t>(
2921 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002922 }
Mike Stump11289f42009-09-09 15:08:12 +00002923
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002924 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002925 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00002926 using namespace llvm::support;
2927 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002928 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00002929 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002930 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002931 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002932 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002933 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002934 ++NumInstanceMethods;
2935
2936 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002937 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002938 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002939 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002940 ++NumFactoryMethods;
2941
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002942 unsigned InstanceBits = Methods.Instance.getBits();
2943 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002944 unsigned InstanceHasMoreThanOneDeclBit =
2945 Methods.Instance.hasMoreThanOneDecl();
2946 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2947 (InstanceHasMoreThanOneDeclBit << 2) |
2948 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002949 unsigned FactoryBits = Methods.Factory.getBits();
2950 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00002951 unsigned FactoryHasMoreThanOneDeclBit =
2952 Methods.Factory.hasMoreThanOneDecl();
2953 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2954 (FactoryHasMoreThanOneDeclBit << 2) |
2955 FactoryBits;
2956 LE.write<uint16_t>(FullInstanceBits);
2957 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002958 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002959 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002960 if (Method->getMethod())
2961 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00002962 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002963 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002964 if (Method->getMethod())
2965 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002966
2967 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002968 }
2969};
2970} // end anonymous namespace
2971
Sebastian Redla19a67f2010-08-03 21:58:15 +00002972/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002973///
2974/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002975/// in an on-disk hash table indexed by the selector. The hash table also
2976/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002977void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002978 using namespace llvm;
2979
Sebastian Redla19a67f2010-08-03 21:58:15 +00002980 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002981 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002982 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002983 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002984 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002985 {
Justin Bognerbb094f02014-04-18 19:57:06 +00002986 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002987 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002988
Sebastian Redla19a67f2010-08-03 21:58:15 +00002989 // Create the on-disk hash table representation. We walk through every
2990 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002991 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002992 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002993 I = SelectorIDs.begin(), E = SelectorIDs.end();
2994 I != E; ++I) {
2995 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002996 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002997 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002998 I->second,
2999 ObjCMethodList(),
3000 ObjCMethodList()
3001 };
3002 if (F != SemaRef.MethodPool.end()) {
3003 Data.Instance = F->second.first;
3004 Data.Factory = F->second.second;
3005 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003006 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003007 // changed.
3008 if (Chain && I->second < FirstSelectorID) {
3009 // Selector already exists. Did it change?
3010 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003011 for (ObjCMethodList *M = &Data.Instance;
3012 !changed && M && M->getMethod(); M = M->getNext()) {
3013 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003014 changed = true;
3015 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003016 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003017 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003018 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003019 changed = true;
3020 }
3021 if (!changed)
3022 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003023 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003024 // A new method pool entry.
3025 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003026 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003027 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003028 }
3029
Douglas Gregorc78d3462009-04-24 21:10:55 +00003030 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003031 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003032 uint32_t BucketOffset;
3033 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003034 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003035 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003036 llvm::raw_svector_ostream Out(MethodPool);
3037 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003038 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003039 BucketOffset = Generator.Emit(Out, Trait);
3040 }
3041
3042 // Create a blob abbreviation
3043 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003044 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3048 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3049
Douglas Gregor95c13f52009-04-25 17:48:32 +00003050 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00003051 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003052 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003053 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00003054 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003055 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00003056
3057 // Create a blob abbreviation for the selector table offsets.
3058 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003059 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3063 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3064
3065 // Write the selector offsets table.
3066 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003067 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003068 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003069 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003070 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003071 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003072 }
3073}
3074
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003075/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003076void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003077 using namespace llvm;
3078 if (SemaRef.ReferencedSelectors.empty())
3079 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003080
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003081 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00003082
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003083 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003084 // very tricky to fix, and given that @selector shouldn't really appear in
3085 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003086 for (DenseMap<Selector, SourceLocation>::iterator S =
3087 SemaRef.ReferencedSelectors.begin(),
3088 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3089 Selector Sel = (*S).first;
3090 SourceLocation Loc = (*S).second;
3091 AddSelectorRef(Sel, Record);
3092 AddSourceLocation(Loc, Record);
3093 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003094 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003095}
3096
Douglas Gregorc5046832009-04-27 18:38:38 +00003097//===----------------------------------------------------------------------===//
3098// Identifier Table Serialization
3099//===----------------------------------------------------------------------===//
3100
Richard Smithb3761912015-02-05 23:08:52 +00003101/// Determine the declaration that should be put into the name lookup table to
3102/// represent the given declaration in this module. This is usually D itself,
3103/// but if D was imported and merged into a local declaration, we want the most
3104/// recent local declaration instead. The chosen declaration will be the most
3105/// recent declaration in any module that imports this one.
3106static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3107 NamedDecl *D) {
3108 if (!LangOpts.Modules || !D->isFromASTFile())
3109 return D;
3110
3111 if (Decl *Redecl = D->getPreviousDecl()) {
3112 // For Redeclarable decls, a prior declaration might be local.
3113 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3114 if (!Redecl->isFromASTFile())
3115 return cast<NamedDecl>(Redecl);
3116 // If we come up a decl from a (chained-)PCH stop since we won't find a
3117 // local one.
3118 if (D->getOwningModuleID() == 0)
3119 break;
3120 }
3121 } else if (Decl *First = D->getCanonicalDecl()) {
3122 // For Mergeable decls, the first decl might be local.
3123 if (!First->isFromASTFile())
3124 return cast<NamedDecl>(First);
3125 }
3126
3127 // All declarations are imported. Our most recent declaration will also be
3128 // the most recent one in anyone who imports us.
3129 return D;
3130}
3131
Douglas Gregorc78d3462009-04-24 21:10:55 +00003132namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003133class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003134 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00003135 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003136 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003137 bool IsModule;
3138
Douglas Gregor1d583f22009-04-28 21:18:29 +00003139 /// \brief Determines whether this is an "interesting" identifier
3140 /// that needs a full IdentifierInfo structure written into the hash
3141 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003142 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003143 if (II->isPoisoned() ||
3144 II->isExtensionToken() ||
3145 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003146 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003147 II->getFETokenInfo<void>())
3148 return true;
3149
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003150 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00003151 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003152
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003153 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003154 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003155 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003156
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003157 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3158 if (!IsModule)
3159 return !shouldIgnoreMacro(Macro, IsModule, PP);
Richard Smithdaa69e02014-07-25 04:40:03 +00003160
3161 MacroState State;
3162 if (getFirstPublicSubmoduleMacro(Macro, State))
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003163 return true;
3164 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003165
3166 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003167 }
3168
Richard Smithdaa69e02014-07-25 04:40:03 +00003169 enum class SubmoduleMacroState {
3170 /// We've seen nothing about this macro.
3171 None,
3172 /// We've seen a public visibility directive.
3173 Public,
3174 /// We've either exported a macro for this module or found that the
3175 /// module's definition of this macro is private.
3176 Done
3177 };
3178 typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
Richard Smith49f906a2014-03-01 00:08:04 +00003179
3180 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003181 getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3182 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3183 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003184 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003185 }
3186
Richard Smith49f906a2014-03-01 00:08:04 +00003187 MacroDirective *
Richard Smithdaa69e02014-07-25 04:40:03 +00003188 getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
Richard Smith49f906a2014-03-01 00:08:04 +00003189 if (MacroDirective *NextMD =
Richard Smithdaa69e02014-07-25 04:40:03 +00003190 getPublicSubmoduleMacro(MD->getPrevious(), State))
3191 return NextMD;
Craig Toppera13603a2014-05-22 05:54:18 +00003192 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003193 }
3194
Richard Smithdaa69e02014-07-25 04:40:03 +00003195 /// \brief Traverses the macro directives history and returns the next
3196 /// public macro definition or undefinition that has not been found so far.
3197 ///
Richard Smith49f906a2014-03-01 00:08:04 +00003198 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003199 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00003200 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
Richard Smithdaa69e02014-07-25 04:40:03 +00003201 MacroState &State) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003202 if (!MD)
Craig Toppera13603a2014-05-22 05:54:18 +00003203 return nullptr;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003204
Richard Smith49f906a2014-03-01 00:08:04 +00003205 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003206 for (; MD; MD = MD->getPrevious()) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003207 // Once we hit an ignored macro, we're done: the rest of the chain
3208 // will all be ignored macros.
3209 if (shouldIgnoreMacro(MD, IsModule, PP))
3210 break;
Richard Smith57721ac2014-07-21 04:10:40 +00003211
Richard Smithdaa69e02014-07-25 04:40:03 +00003212 // If this macro was imported, re-export it.
3213 if (MD->isImported())
3214 return MD;
Richard Smith57721ac2014-07-21 04:10:40 +00003215
Richard Smithdaa69e02014-07-25 04:40:03 +00003216 SubmoduleID ModID = getSubmoduleID(MD);
3217 auto &S = State[ModID];
3218 assert(ModID && "found macro in no submodule");
Richard Smith49f906a2014-03-01 00:08:04 +00003219
Richard Smithdaa69e02014-07-25 04:40:03 +00003220 if (S == SubmoduleMacroState::Done)
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003221 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003222
Richard Smithdaa69e02014-07-25 04:40:03 +00003223 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3224 // The latest visibility directive for a name in a submodule affects all
3225 // the directives that come before it.
3226 if (S == SubmoduleMacroState::None)
3227 S = VisMD->isPublic() ? SubmoduleMacroState::Public
3228 : SubmoduleMacroState::Done;
3229 } else {
3230 S = SubmoduleMacroState::Done;
Richard Smith49f906a2014-03-01 00:08:04 +00003231 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003232 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003233 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003234
Craig Toppera13603a2014-05-22 05:54:18 +00003235 return nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003236 }
3237
Richard Smithdaa69e02014-07-25 04:40:03 +00003238 ArrayRef<SubmoduleID>
3239 getOverriddenSubmodules(MacroDirective *MD,
3240 SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3241 assert(!isa<VisibilityMacroDirective>(MD) &&
3242 "only #define and #undef can override");
3243 if (MD->isImported())
3244 return MD->getOverriddenModules();
3245
3246 ScratchSpace.clear();
3247 SubmoduleID ModID = getSubmoduleID(MD);
3248 for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3249 if (shouldIgnoreMacro(MD, IsModule, PP))
3250 break;
3251
3252 // If this is a definition from a submodule import, that submodule's
3253 // definition is overridden by the definition or undefinition that we
3254 // started with.
3255 if (MD->isImported()) {
3256 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3257 SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3258 assert(DefModuleID && "imported macro has no owning module");
3259 ScratchSpace.push_back(DefModuleID);
3260 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3261 // If we override a #undef, we override anything that #undef overrides.
3262 // We don't need to override it, since an active #undef doesn't affect
3263 // the meaning of a macro.
3264 auto Overrides = UndefMD->getOverriddenModules();
3265 ScratchSpace.insert(ScratchSpace.end(),
3266 Overrides.begin(), Overrides.end());
3267 }
3268 }
3269
3270 // Stop once we leave the original macro's submodule.
3271 //
3272 // Either this submodule #included another submodule of the same
3273 // module or it just happened to be built after the other module.
3274 // In the former case, we override the submodule's macro.
3275 //
3276 // FIXME: In the latter case, we shouldn't do so, but we can't tell
3277 // these cases apart.
3278 //
3279 // FIXME: We can leave this submodule and re-enter it if it #includes a
3280 // header within a different submodule of the same module. In such cases
3281 // the overrides list will be incomplete.
3282 SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3283 if (DirectiveModuleID != ModID) {
3284 if (DirectiveModuleID && !MD->isImported())
3285 ScratchSpace.push_back(DirectiveModuleID);
3286 break;
3287 }
3288 }
3289
3290 std::sort(ScratchSpace.begin(), ScratchSpace.end());
3291 ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3292 ScratchSpace.end());
3293 return ScratchSpace;
3294 }
3295
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003296 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Richard Smith57721ac2014-07-21 04:10:40 +00003297 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003298 }
3299
Douglas Gregore84a9da2009-04-20 20:36:09 +00003300public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003301 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003302 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003303
Sebastian Redl539c5062010-08-18 23:57:32 +00003304 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003305 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003306
Justin Bogner25463f12014-04-18 20:27:24 +00003307 typedef unsigned hash_value_type;
3308 typedef unsigned offset_type;
3309
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003310 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3311 IdentifierResolver &IdResolver, bool IsModule)
3312 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003313
Justin Bogner25463f12014-04-18 20:27:24 +00003314 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003315 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003316 }
Mike Stump11289f42009-09-09 15:08:12 +00003317
3318 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003319 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003320 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003321 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Craig Toppera13603a2014-05-22 05:54:18 +00003322 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003323 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003324 DataLen += 2; // 2 bytes for builtin ID
3325 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003326 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003327 DataLen += 4; // MacroDirectives offset.
3328 if (IsModule) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003329 MacroState State;
3330 SmallVector<SubmoduleID, 16> Scratch;
3331 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3332 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003333 DataLen += 4; // MacroInfo ID or ModuleID.
Richard Smithdaa69e02014-07-25 04:40:03 +00003334 if (unsigned NumOverrides =
3335 getOverriddenSubmodules(MD, Scratch).size())
3336 DataLen += 4 * (1 + NumOverrides);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003337 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003338 DataLen += 4; // 0 terminator.
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003339 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003340 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003341
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003342 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3343 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003344 D != DEnd; ++D)
Richard Smithdaa69e02014-07-25 04:40:03 +00003345 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003346 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003347 using namespace llvm::support;
3348 endian::Writer<little> LE(Out);
3349
3350 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003351 // We emit the key length after the data length so that every
3352 // string is preceded by a 16-bit length. This matches the PTH
3353 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003354 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003355 return std::make_pair(KeyLen, DataLen);
3356 }
Mike Stump11289f42009-09-09 15:08:12 +00003357
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003358 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003359 unsigned KeyLen) {
3360 // Record the location of the key data. This is used when generating
3361 // the mapping from persistent IDs to strings.
3362 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003363 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003364 }
Mike Stump11289f42009-09-09 15:08:12 +00003365
Richard Smith49f906a2014-03-01 00:08:04 +00003366 static void emitMacroOverrides(raw_ostream &Out,
Craig Topper00bbdcf2014-06-28 23:22:23 +00003367 ArrayRef<SubmoduleID> Overridden) {
Richard Smith49f906a2014-03-01 00:08:04 +00003368 if (!Overridden.empty()) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003369 using namespace llvm::support;
3370 endian::Writer<little> LE(Out);
3371 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Richard Smithdaa69e02014-07-25 04:40:03 +00003372 for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3373 assert(Overridden[I] && "zero module ID for override");
Justin Bognere1c147c2014-03-28 22:03:19 +00003374 LE.write<uint32_t>(Overridden[I]);
Richard Smithdaa69e02014-07-25 04:40:03 +00003375 }
Richard Smith49f906a2014-03-01 00:08:04 +00003376 }
3377 }
3378
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003379 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003380 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003381 using namespace llvm::support;
3382 endian::Writer<little> LE(Out);
Craig Toppera13603a2014-05-22 05:54:18 +00003383 MacroDirective *Macro = nullptr;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003384 if (!isInterestingIdentifier(II, Macro)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003385 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003386 return;
3387 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003388
Justin Bognere1c147c2014-03-28 22:03:19 +00003389 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003390 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3391 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003392 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003393 Bits = 0;
3394 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003395 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003396 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003397 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3398 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003399 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003400 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003401 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003402
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003403 if (HadMacroDefinition) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003404 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003405 if (IsModule) {
3406 // Write the IDs of macros coming from different submodules.
Richard Smithdaa69e02014-07-25 04:40:03 +00003407 MacroState State;
3408 SmallVector<SubmoduleID, 16> Scratch;
3409 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3410 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Richard Smith49f906a2014-03-01 00:08:04 +00003411 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smithdaa69e02014-07-25 04:40:03 +00003412 // FIXME: If this macro directive was created by #pragma pop_macros,
3413 // or if it was created implicitly by resolving conflicting macros,
3414 // it may be for a different submodule from the one in the MacroInfo
3415 // object. If so, we should write out its owning ModuleID.
3416 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Richard Smith49f906a2014-03-01 00:08:04 +00003417 assert(InfoID);
Justin Bognere1c147c2014-03-28 22:03:19 +00003418 LE.write<uint32_t>(InfoID << 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003419 } else {
Richard Smithdaa69e02014-07-25 04:40:03 +00003420 auto *UndefMD = cast<UndefMacroDirective>(MD);
3421 SubmoduleID Mod = UndefMD->isImported()
3422 ? UndefMD->getOwningModuleID()
3423 : getSubmoduleID(UndefMD);
3424 LE.write<uint32_t>((Mod << 1) | 1);
Richard Smith49f906a2014-03-01 00:08:04 +00003425 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003426 emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003427 }
Richard Smithdaa69e02014-07-25 04:40:03 +00003428 LE.write<uint32_t>(0xdeadbeef);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003429 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003430 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003431
Douglas Gregora868bbd2009-04-21 22:25:48 +00003432 // Emit the declaration IDs in reverse order, because the
3433 // IdentifierResolver provides the declarations as they would be
3434 // visible (e.g., the function "stat" would come before the struct
Richard Smithb3761912015-02-05 23:08:52 +00003435 // "stat"), but the ASTReader adds declarations to the end of the list
3436 // (so we need to see the struct "stat" before the function "stat").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003437 // Only emit declarations that aren't from a chained PCH, though.
Richard Smithb3761912015-02-05 23:08:52 +00003438 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II), IdResolver.end());
3439 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3440 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003441 D != DEnd; ++D)
Richard Smithb3761912015-02-05 23:08:52 +00003442 LE.write<uint32_t>(
3443 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003444 }
3445};
3446} // end anonymous namespace
3447
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003448/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003449///
3450/// The identifier table consists of a blob containing string data
3451/// (the actual identifiers themselves) and a separate "offsets" index
3452/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003453void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3454 IdentifierResolver &IdResolver,
3455 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003456 using namespace llvm;
3457
3458 // Create and write out the blob that contains the identifier
3459 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003460 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003461 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003462 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003463
Douglas Gregore6648fb2009-04-28 20:33:11 +00003464 // Look for any identifiers that were named while processing the
3465 // headers, but are otherwise not needed. We add these to the hash
3466 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003467 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003468 // file.
3469 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3470 IDEnd = PP.getIdentifierTable().end();
3471 ID != IDEnd; ++ID)
3472 getIdentifierRef(ID->second);
3473
Sebastian Redlff4a2952010-07-23 23:49:55 +00003474 // Create the on-disk hash table representation. We only store offsets
3475 // for identifiers that appear here for the first time.
3476 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003477 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003478 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3479 ID != IDEnd; ++ID) {
3480 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003481 if (!Chain || !ID->first->isFromAST() ||
3482 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003483 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003484 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003485 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003486
Douglas Gregore84a9da2009-04-20 20:36:09 +00003487 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003488 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003489 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003490 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003491 using namespace llvm::support;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003492 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003493 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003494 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003495 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003496 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003497 }
3498
3499 // Create a blob abbreviation
3500 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003501 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003504 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003505
3506 // Write the identifier table
3507 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003508 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003509 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003510 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003511 }
3512
3513 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003514 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003515 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003516 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003517 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003518 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3519 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3520
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003521#ifndef NDEBUG
3522 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3523 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3524#endif
3525
Douglas Gregor0e149972009-04-25 19:10:14 +00003526 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003527 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003528 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003529 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003530 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003531 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003532}
3533
Douglas Gregorc5046832009-04-27 18:38:38 +00003534//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003535// DeclContext's Name Lookup Table Serialization
3536//===----------------------------------------------------------------------===//
3537
3538namespace {
3539// Trait used for the on-disk hash table used in the method pool.
3540class ASTDeclContextNameLookupTrait {
3541 ASTWriter &Writer;
3542
3543public:
3544 typedef DeclarationName key_type;
3545 typedef key_type key_type_ref;
3546
3547 typedef DeclContext::lookup_result data_type;
3548 typedef const data_type& data_type_ref;
3549
Justin Bogner25463f12014-04-18 20:27:24 +00003550 typedef unsigned hash_value_type;
3551 typedef unsigned offset_type;
3552
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003553 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3554
Justin Bogner25463f12014-04-18 20:27:24 +00003555 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003556 llvm::FoldingSetNodeID ID;
3557 ID.AddInteger(Name.getNameKind());
3558
3559 switch (Name.getNameKind()) {
3560 case DeclarationName::Identifier:
3561 ID.AddString(Name.getAsIdentifierInfo()->getName());
3562 break;
3563 case DeclarationName::ObjCZeroArgSelector:
3564 case DeclarationName::ObjCOneArgSelector:
3565 case DeclarationName::ObjCMultiArgSelector:
3566 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3567 break;
3568 case DeclarationName::CXXConstructorName:
3569 case DeclarationName::CXXDestructorName:
3570 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003571 break;
3572 case DeclarationName::CXXOperatorName:
3573 ID.AddInteger(Name.getCXXOverloadedOperator());
3574 break;
3575 case DeclarationName::CXXLiteralOperatorName:
3576 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3577 case DeclarationName::CXXUsingDirective:
3578 break;
3579 }
3580
3581 return ID.ComputeHash();
3582 }
3583
3584 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003585 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003586 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003587 using namespace llvm::support;
3588 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003589 unsigned KeyLen = 1;
3590 switch (Name.getNameKind()) {
3591 case DeclarationName::Identifier:
3592 case DeclarationName::ObjCZeroArgSelector:
3593 case DeclarationName::ObjCOneArgSelector:
3594 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003595 case DeclarationName::CXXLiteralOperatorName:
3596 KeyLen += 4;
3597 break;
3598 case DeclarationName::CXXOperatorName:
3599 KeyLen += 1;
3600 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003601 case DeclarationName::CXXConstructorName:
3602 case DeclarationName::CXXDestructorName:
3603 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003604 case DeclarationName::CXXUsingDirective:
3605 break;
3606 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003607 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003608
3609 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003610 unsigned DataLen = 2 + 4 * Lookup.size();
Justin Bognere1c147c2014-03-28 22:03:19 +00003611 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003612
3613 return std::make_pair(KeyLen, DataLen);
3614 }
3615
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003616 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003617 using namespace llvm::support;
3618 endian::Writer<little> LE(Out);
3619 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003620 switch (Name.getNameKind()) {
3621 case DeclarationName::Identifier:
Justin Bognere1c147c2014-03-28 22:03:19 +00003622 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003623 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003624 case DeclarationName::ObjCZeroArgSelector:
3625 case DeclarationName::ObjCOneArgSelector:
3626 case DeclarationName::ObjCMultiArgSelector:
Justin Bognere1c147c2014-03-28 22:03:19 +00003627 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003628 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003629 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003630 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3631 "Invalid operator?");
Justin Bognere1c147c2014-03-28 22:03:19 +00003632 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003633 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003634 case DeclarationName::CXXLiteralOperatorName:
Justin Bognere1c147c2014-03-28 22:03:19 +00003635 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003636 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003637 case DeclarationName::CXXConstructorName:
3638 case DeclarationName::CXXDestructorName:
3639 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003640 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003641 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003642 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003643
3644 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003645 }
3646
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003647 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003648 data_type Lookup, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003649 using namespace llvm::support;
3650 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003651 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003652 LE.write<uint16_t>(Lookup.size());
David Blaikieff7d47a2012-12-19 00:45:41 +00003653 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3654 I != E; ++I)
Richard Smithb3761912015-02-05 23:08:52 +00003655 LE.write<uint32_t>(
3656 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), *I)));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003657
3658 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3659 }
3660};
3661} // end anonymous namespace
3662
Richard Smithcd45dbc2014-04-19 03:48:30 +00003663template<typename Visitor>
3664static void visitLocalLookupResults(const DeclContext *ConstDC,
3665 bool NeedToReconcileExternalVisibleStorage,
3666 Visitor AddLookupResult) {
3667 // FIXME: We need to build the lookups table, which is logically const.
3668 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Richard Smith961eae52014-03-25 01:14:22 +00003669 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3670
Richard Smith961eae52014-03-25 01:14:22 +00003671 SmallVector<DeclarationName, 16> ExternalNames;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003672 for (auto &Lookup : *DC->buildLookup()) {
Richard Smith961eae52014-03-25 01:14:22 +00003673 if (Lookup.second.hasExternalDecls() ||
Richard Smithcd45dbc2014-04-19 03:48:30 +00003674 NeedToReconcileExternalVisibleStorage) {
Richard Smith961eae52014-03-25 01:14:22 +00003675 // We don't know for sure what declarations are found by this name,
3676 // because the external source might have a different set from the set
3677 // that are in the lookup map, and we can't update it now without
3678 // risking invalidating our lookup iterator. So add it to a queue to
3679 // deal with later.
3680 ExternalNames.push_back(Lookup.first);
3681 continue;
3682 }
3683
3684 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3685 }
3686
3687 // Add the names we needed to defer. Note, this shouldn't add any new decls
3688 // to the list we need to serialize: any new declarations we find here should
3689 // be imported from an external source.
3690 // FIXME: What if the external source isn't an ASTReader?
3691 for (const auto &Name : ExternalNames)
Richard Smithcd45dbc2014-04-19 03:48:30 +00003692 AddLookupResult(Name, DC->lookup(Name));
3693}
3694
3695void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
David Blaikie82e95a32014-11-19 07:49:47 +00003696 if (UpdatedDeclContexts.insert(DC).second && WritingAST) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00003697 // Ensure we emit all the visible declarations.
3698 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3699 [&](DeclarationName Name,
3700 DeclContext::lookup_const_result Result) {
3701 for (auto *Decl : Result)
Richard Smithb3761912015-02-05 23:08:52 +00003702 GetDeclRef(getDeclForLocalLookup(getLangOpts(), Decl));
Richard Smithcd45dbc2014-04-19 03:48:30 +00003703 });
3704 }
3705}
3706
3707uint32_t
3708ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3709 llvm::SmallVectorImpl<char> &LookupTable) {
3710 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3711
3712 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3713 Generator;
3714 ASTDeclContextNameLookupTrait Trait(*this);
3715
3716 // Create the on-disk hash table representation.
3717 DeclarationName ConstructorName;
3718 DeclarationName ConversionName;
3719 SmallVector<NamedDecl *, 8> ConstructorDecls;
3720 SmallVector<NamedDecl *, 4> ConversionDecls;
3721
3722 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3723 [&](DeclarationName Name,
3724 DeclContext::lookup_result Result) {
3725 if (Result.empty())
3726 return;
3727
3728 // Different DeclarationName values of certain kinds are mapped to
3729 // identical serialized keys, because we don't want to use type
3730 // identifiers in the keys (since type ids are local to the module).
3731 switch (Name.getNameKind()) {
3732 case DeclarationName::CXXConstructorName:
3733 // There may be different CXXConstructorName DeclarationName values
3734 // in a DeclContext because a UsingDecl that inherits constructors
3735 // has the DeclarationName of the inherited constructors.
3736 if (!ConstructorName)
3737 ConstructorName = Name;
3738 ConstructorDecls.append(Result.begin(), Result.end());
3739 return;
3740
3741 case DeclarationName::CXXConversionFunctionName:
3742 if (!ConversionName)
3743 ConversionName = Name;
3744 ConversionDecls.append(Result.begin(), Result.end());
3745 return;
3746
3747 default:
3748 break;
3749 }
3750
3751 Generator.insert(Name, Result, Trait);
3752 });
Richard Smith961eae52014-03-25 01:14:22 +00003753
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003754 // Add the constructors.
3755 if (!ConstructorDecls.empty()) {
3756 Generator.insert(ConstructorName,
3757 DeclContext::lookup_result(ConstructorDecls.begin(),
3758 ConstructorDecls.end()),
3759 Trait);
3760 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003761
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003762 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003763 if (!ConversionDecls.empty()) {
3764 Generator.insert(ConversionName,
3765 DeclContext::lookup_result(ConversionDecls.begin(),
3766 ConversionDecls.end()),
3767 Trait);
3768 }
3769
3770 // Create the on-disk hash table in a buffer.
3771 llvm::raw_svector_ostream Out(LookupTable);
3772 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003773 using namespace llvm::support;
3774 endian::Writer<little>(Out).write<uint32_t>(0);
Richard Smith961eae52014-03-25 01:14:22 +00003775 return Generator.Emit(Out, Trait);
3776}
3777
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003778/// \brief Write the block containing all of the declaration IDs
3779/// visible from the given DeclContext.
3780///
3781/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003782/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003783uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3784 DeclContext *DC) {
3785 if (DC->getPrimaryContext() != DC)
3786 return 0;
3787
3788 // Since there is no name lookup into functions or methods, don't bother to
3789 // build a visible-declarations table for these entities.
3790 if (DC->isFunctionOrMethod())
3791 return 0;
3792
3793 // If not in C++, we perform name lookup for the translation unit via the
3794 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003795 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003796 return 0;
3797
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003798 // Serialize the contents of the mapping used for lookup. Note that,
3799 // although we have two very different code paths, the serialized
3800 // representation is the same for both cases: a declaration name,
3801 // followed by a size, followed by references to the visible
3802 // declarations that have that name.
3803 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003804 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003805 if (!Map || Map->empty())
3806 return 0;
3807
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003808 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003809 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003810 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003811
3812 // Write the lookup table
3813 RecordData Record;
3814 Record.push_back(DECL_CONTEXT_VISIBLE);
3815 Record.push_back(BucketOffset);
3816 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3817 LookupTable.str());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003818 ++NumVisibleDeclContexts;
3819 return Offset;
3820}
3821
Sebastian Redla4071b42010-08-24 00:50:09 +00003822/// \brief Write an UPDATE_VISIBLE block for the given context.
3823///
3824/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3825/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003826/// (in C++), for namespaces, and for classes with forward-declared unscoped
3827/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003828void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003829 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003830 if (!Map || Map->empty())
3831 return;
3832
Sebastian Redla4071b42010-08-24 00:50:09 +00003833 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003834 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003835 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003836
3837 // Write the lookup table
3838 RecordData Record;
3839 Record.push_back(UPDATE_VISIBLE);
3840 Record.push_back(getDeclID(cast<Decl>(DC)));
3841 Record.push_back(BucketOffset);
3842 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3843}
3844
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003845/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3846void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3847 RecordData Record;
3848 Record.push_back(Opts.fp_contract);
3849 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3850}
3851
3852/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3853void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003854 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003855 return;
3856
3857 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3858 RecordData Record;
3859#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3860#include "clang/Basic/OpenCLExtensions.def"
3861 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3862}
3863
Douglas Gregor358cd442012-01-15 16:58:34 +00003864void ASTWriter::WriteRedeclarations() {
3865 RecordData LocalRedeclChains;
3866 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3867
3868 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3869 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003870 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003871
3872 Decl *MostRecent = First->getMostRecentDecl();
3873
3874 // If we only have a single declaration, there is no point in storing
3875 // a redeclaration chain.
3876 if (First == MostRecent)
3877 continue;
3878
3879 unsigned Offset = LocalRedeclChains.size();
3880 unsigned Size = 0;
3881 LocalRedeclChains.push_back(0); // Placeholder for the size.
3882
3883 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003884 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003885 Prev = Prev->getPreviousDecl()) {
3886 if (!Prev->isFromASTFile()) {
3887 AddDeclRef(Prev, LocalRedeclChains);
3888 ++Size;
3889 }
3890 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003891
3892 if (!First->isFromASTFile() && Chain) {
3893 Decl *FirstFromAST = MostRecent;
3894 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3895 if (Prev->isFromASTFile())
3896 FirstFromAST = Prev;
3897 }
3898
Richard Smith2516ba22014-08-11 18:35:44 +00003899 // FIXME: Do we need to do this for the first declaration from each
3900 // redeclaration chain that was merged into this one?
Douglas Gregor6168bd22013-02-18 15:53:43 +00003901 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3902 }
3903
Douglas Gregor358cd442012-01-15 16:58:34 +00003904 LocalRedeclChains[Offset] = Size;
3905
3906 // Reverse the set of local redeclarations, so that we store them in
3907 // order (since we found them in reverse order).
3908 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3909
Douglas Gregor6168bd22013-02-18 15:53:43 +00003910 // Add the mapping from the first ID from the AST to the set of local
3911 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003912 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3913 LocalRedeclsMap.push_back(Info);
3914
3915 assert(N == Redeclarations.size() &&
3916 "Deserialized a declaration we shouldn't have");
3917 }
3918
3919 if (LocalRedeclChains.empty())
3920 return;
3921
3922 // Sort the local redeclarations map by the first declaration ID,
3923 // since the reader will be performing binary searches on this information.
3924 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3925
3926 // Emit the local redeclarations map.
3927 using namespace llvm;
3928 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3929 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3932 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3933
3934 RecordData Record;
3935 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3936 Record.push_back(LocalRedeclsMap.size());
3937 Stream.EmitRecordWithBlob(AbbrevID, Record,
3938 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3939 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3940
3941 // Emit the redeclaration chains.
3942 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3943}
3944
Douglas Gregor404cdde2012-01-27 01:47:08 +00003945void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003946 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003947 RecordData Categories;
3948
3949 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3950 unsigned Size = 0;
3951 unsigned StartIndex = Categories.size();
3952
3953 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3954
3955 // Allocate space for the size.
3956 Categories.push_back(0);
3957
3958 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003959 for (ObjCInterfaceDecl::known_categories_iterator
3960 Cat = Class->known_categories_begin(),
3961 CatEnd = Class->known_categories_end();
3962 Cat != CatEnd; ++Cat, ++Size) {
3963 assert(getDeclID(*Cat) != 0 && "Bogus category");
3964 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003965 }
3966
3967 // Update the size.
3968 Categories[StartIndex] = Size;
3969
3970 // Record this interface -> category map.
3971 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3972 CategoriesMap.push_back(CatInfo);
3973 }
3974
3975 // Sort the categories map by the definition ID, since the reader will be
3976 // performing binary searches on this information.
3977 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3978
3979 // Emit the categories map.
3980 using namespace llvm;
3981 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3982 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3985 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3986
3987 RecordData Record;
3988 Record.push_back(OBJC_CATEGORIES_MAP);
3989 Record.push_back(CategoriesMap.size());
3990 Stream.EmitRecordWithBlob(AbbrevID, Record,
3991 reinterpret_cast<char*>(CategoriesMap.data()),
3992 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3993
3994 // Emit the category lists.
3995 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3996}
3997
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003998void ASTWriter::WriteMergedDecls() {
3999 if (!Chain || Chain->MergedDecls.empty())
4000 return;
4001
4002 RecordData Record;
4003 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
4004 IEnd = Chain->MergedDecls.end();
4005 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00004006 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Richard Smithcd45dbc2014-04-19 03:48:30 +00004007 : GetDeclRef(I->first);
Douglas Gregor464b0ca2011-12-22 21:40:42 +00004008 assert(CanonID && "Merged declaration not known?");
4009
4010 Record.push_back(CanonID);
4011 Record.push_back(I->second.size());
4012 Record.append(I->second.begin(), I->second.end());
4013 }
4014 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
4015}
4016
Richard Smithe40f2ba2013-08-07 21:41:30 +00004017void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4018 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4019
4020 if (LPTMap.empty())
4021 return;
4022
4023 RecordData Record;
4024 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4025 ItEnd = LPTMap.end();
4026 It != ItEnd; ++It) {
4027 LateParsedTemplate *LPT = It->second;
4028 AddDeclRef(It->first, Record);
4029 AddDeclRef(LPT->D, Record);
4030 Record.push_back(LPT->Toks.size());
4031
4032 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4033 TokEnd = LPT->Toks.end();
4034 TokIt != TokEnd; ++TokIt) {
4035 AddToken(*TokIt, Record);
4036 }
4037 }
4038 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4039}
4040
Dario Domizioli13a0a382014-05-23 12:13:25 +00004041/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4042void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4043 RecordData Record;
4044 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4045 AddSourceLocation(PragmaLoc, Record);
4046 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4047}
4048
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004049//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004050// General Serialization Routines
4051//===----------------------------------------------------------------------===//
4052
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004053/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004054void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4055 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004056 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00004057 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4058 e = Attrs.end(); i != e; ++i){
4059 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004060 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00004061 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004062
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004063#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004064
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004065 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004066}
4067
John McCallf413f5e2013-05-03 00:10:13 +00004068void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4069 AddSourceLocation(Tok.getLocation(), Record);
4070 Record.push_back(Tok.getLength());
4071
4072 // FIXME: When reading literal tokens, reconstruct the literal pointer
4073 // if it is needed.
4074 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4075 // FIXME: Should translate token kind to a stable encoding.
4076 Record.push_back(Tok.getKind());
4077 // FIXME: Should translate token flags to a stable encoding.
4078 Record.push_back(Tok.getFlags());
4079}
4080
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004081void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004082 Record.push_back(Str.size());
4083 Record.insert(Record.end(), Str.begin(), Str.end());
4084}
4085
Richard Smith7ed1bc92014-12-05 22:42:13 +00004086bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004087 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004088
Richard Smith54cc3c22014-12-11 20:50:24 +00004089 bool Changed =
4090 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004091
4092 // Remove a prefix to make the path relative, if relevant.
4093 const char *PathBegin = Path.data();
4094 const char *PathPtr =
4095 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4096 if (PathPtr != PathBegin) {
4097 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4098 Changed = true;
4099 }
4100
4101 return Changed;
4102}
4103
4104void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4105 SmallString<128> FilePath(Path);
4106 PreparePathForOutput(FilePath);
4107 AddString(FilePath, Record);
4108}
4109
4110void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
4111 StringRef Path) {
4112 SmallString<128> FilePath(Path);
4113 PreparePathForOutput(FilePath);
4114 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4115}
4116
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004117void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4118 RecordDataImpl &Record) {
4119 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004120 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004121 Record.push_back(*Minor + 1);
4122 else
4123 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004124 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004125 Record.push_back(*Subminor + 1);
4126 else
4127 Record.push_back(0);
4128}
4129
Douglas Gregore84a9da2009-04-20 20:36:09 +00004130/// \brief Note that the identifier II occurs at the given offset
4131/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004132void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004133 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004134 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004135 // up earlier in the chain and thus don't need an offset.
4136 if (ID >= FirstIdentID)
4137 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004138}
4139
Douglas Gregor95c13f52009-04-25 17:48:32 +00004140/// \brief Note that the selector Sel occurs at the given offset
4141/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004142void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004143 unsigned ID = SelectorIDs[Sel];
4144 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004145 // Don't record offsets for selectors that are also available in a different
4146 // file.
4147 if (ID < FirstSelectorID)
4148 return;
4149 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004150}
4151
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004152ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Richard Smith01b2cb42014-07-26 06:37:51 +00004153 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4154 WritingModule(nullptr), WritingAST(false),
4155 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4156 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4157 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4158 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4159 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4160 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4161 NextSubmoduleID(FirstSubmoduleID),
4162 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4163 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4164 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4165 NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4166 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4167 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004168 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
Richard Smith01b2cb42014-07-26 06:37:51 +00004169 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
Richard Smitha27c26e2014-07-27 04:19:32 +00004170 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4171 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4172 ExprImplicitCastAbbrev(0) {}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004173
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004174ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004175 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004176}
4177
Richard Smithb3761912015-02-05 23:08:52 +00004178const LangOptions &ASTWriter::getLangOpts() const {
4179 assert(WritingAST && "can't determine lang opts when not writing AST");
4180 return Context->getLangOpts();
4181}
4182
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004183void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00004184 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004185 Module *WritingModule, StringRef isysroot,
4186 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004187 WritingAST = true;
4188
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004189 ASTHasCompilerErrors = hasErrors;
4190
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004191 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004192 Stream.Emit((unsigned)'C', 8);
4193 Stream.Emit((unsigned)'P', 8);
4194 Stream.Emit((unsigned)'C', 8);
4195 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004196
Chris Lattner28fa4e62009-04-26 22:26:21 +00004197 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004198
Douglas Gregoreda8e122011-08-09 15:13:55 +00004199 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004200 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004201 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004202 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004203 Context = nullptr;
4204 PP = nullptr;
4205 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004206 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004207
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004208 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00004209}
4210
Douglas Gregora94a1542011-07-27 21:45:57 +00004211template<typename Vector>
4212static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4213 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004214 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4215 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004216 Writer.AddDeclRef(*I, Record);
4217 }
4218}
4219
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00004220void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00004221 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00004222 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004223 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004224 using namespace llvm;
4225
Craig Toppera13603a2014-05-22 05:54:18 +00004226 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004227
Douglas Gregorcf68c582011-12-01 22:20:10 +00004228 // Make sure that the AST reader knows to finalize itself.
4229 if (Chain)
4230 Chain->finalizeForWriting();
4231
Sebastian Redl143413f2010-07-12 22:02:52 +00004232 ASTContext &Context = SemaRef.Context;
4233 Preprocessor &PP = SemaRef.PP;
4234
Douglas Gregordab42432011-08-12 00:15:20 +00004235 // Set up predefined declaration IDs.
4236 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00004237 if (Context.ObjCIdDecl)
4238 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00004239 if (Context.ObjCSelDecl)
4240 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00004241 if (Context.ObjCClassDecl)
4242 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00004243 if (Context.ObjCProtocolClassDecl)
4244 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00004245 if (Context.Int128Decl)
4246 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4247 if (Context.UInt128Decl)
4248 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00004249 if (Context.ObjCInstanceTypeDecl)
4250 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00004251 if (Context.BuiltinVaListDecl)
4252 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4253
Douglas Gregor851443c2011-08-12 01:39:19 +00004254 if (!Chain) {
4255 // Make sure that we emit IdentifierInfos (and any attached
4256 // declarations) for builtins. We don't need to do this when we're
4257 // emitting chained PCH files, because all of the builtins will be
4258 // in the original PCH file.
4259 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004260 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004261 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00004262 if (!Context.getLangOpts().NoBuiltin) {
4263 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4264 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004265 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4266 getIdentifierRef(&Table.get(BuiltinNames[I]));
4267 }
4268
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004269 // If there are any out-of-date identifiers, bring them up to date.
4270 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00004271 // Find out-of-date identifiers.
4272 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004273 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4274 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00004275 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004276 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00004277 OutOfDate.push_back(ID->second);
4278 }
4279
4280 // Update the out-of-date identifiers.
4281 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4282 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4283 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00004284 }
4285
Richard Smithcd45dbc2014-04-19 03:48:30 +00004286 // If we saw any DeclContext updates before we started writing the AST file,
4287 // make sure all visible decls in those DeclContexts are written out.
4288 if (!UpdatedDeclContexts.empty()) {
4289 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4290 UpdatedDeclContexts.clear();
4291 for (auto *DC : OldUpdatedDeclContexts)
4292 AddUpdatedDeclContext(DC);
4293 }
4294
Chris Lattner0c797362009-09-08 18:19:27 +00004295 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004296 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004297 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004298 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004299 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004300
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004301 // Build a record containing all of the file scoped decls in this file.
4302 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004303 if (!isModule)
4304 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4305 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004306
Douglas Gregor851443c2011-08-12 01:39:19 +00004307 // Build a record containing all of the delegating constructors we still need
4308 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004309 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004310 if (!isModule)
4311 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004312
Douglas Gregor851443c2011-08-12 01:39:19 +00004313 // Write the set of weak, undeclared identifiers. We always write the
4314 // entire table, since later PCH files in a PCH chain are only interested in
4315 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004316 RecordData WeakUndeclaredIdentifiers;
4317 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00004318 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004319 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4320 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4321 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4322 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4323 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4324 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4325 }
4326 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004327
Richard Smith78165b52013-01-10 23:43:47 +00004328 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004329 // declarations in this header file. Generally, this record will be
4330 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00004331 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004332 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00004333 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00004334 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00004335 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4336 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00004337 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004338 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00004339 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00004340 }
4341
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004342 // Build a record containing all of the ext_vector declarations.
4343 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004344 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004345
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004346 // Build a record containing all of the VTable uses information.
4347 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004348 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004349 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4350 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4351 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4352 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4353 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004354 }
4355
Nico Weber72889432014-09-06 01:25:55 +00004356 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4357 RecordData UnusedLocalTypedefNameCandidates;
4358 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4359 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4360
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004361 // Build a record containing all of dynamic classes declarations.
4362 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004363 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004364
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004365 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004366 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004367 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004368 I = SemaRef.PendingInstantiations.begin(),
4369 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4370 AddDeclRef(I->first, PendingInstantiations);
4371 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004372 }
4373 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4374 "There are local ones at end of translation unit!");
4375
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004376 // Build a record containing some declaration references.
4377 RecordData SemaDeclRefs;
4378 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4379 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4380 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4381 }
4382
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004383 RecordData CUDASpecialDeclRefs;
4384 if (Context.getcudaConfigureCallDecl()) {
4385 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4386 }
4387
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004388 // Build a record containing all of the known namespaces.
4389 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004390 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004391 I = SemaRef.KnownNamespaces.begin(),
4392 IEnd = SemaRef.KnownNamespaces.end();
4393 I != IEnd; ++I) {
4394 if (!I->second)
4395 AddDeclRef(I->first, KnownNamespaces);
4396 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004397
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004398 // Build a record of all used, undefined objects that require definitions.
4399 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004400
4401 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004402 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004403 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4404 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004405 AddDeclRef(I->first, UndefinedButUsed);
4406 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004407 }
4408
Douglas Gregor112b9072012-10-18 05:31:06 +00004409 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004410 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004411
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004412 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004413 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004414 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004415
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004416 // This is so that older clang versions, before the introduction
4417 // of the control block, can read and reject the newer PCH format.
4418 Record.clear();
4419 Record.push_back(VERSION_MAJOR);
4420 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4421
Douglas Gregor851443c2011-08-12 01:39:19 +00004422 // Create a lexical update block containing all of the declarations in the
4423 // translation unit that do not come from other AST files.
4424 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4425 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004426 for (const auto *I : TU->noload_decls()) {
4427 if (!I->isFromASTFile())
4428 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004429 }
4430
4431 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4432 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4433 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4434 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4435 Record.clear();
4436 Record.push_back(TU_UPDATE_LEXICAL);
4437 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4438 data(NewGlobalDecls));
4439
4440 // And a visible updates block for the translation unit.
4441 Abv = new llvm::BitCodeAbbrev();
4442 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4443 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4444 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4445 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4446 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4447 WriteDeclContextVisibleUpdate(TU);
4448
4449 // If the translation unit has an anonymous namespace, and we don't already
4450 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004451 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004452 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4453 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004454 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004455 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004456 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004457
Richard Smith5652c0f2014-03-21 01:48:23 +00004458 // Add update records for all mangling numbers and static local numbers.
4459 // These aren't really update records, but this is a convenient way of
4460 // tagging this rare extra data onto the declarations.
4461 for (const auto &Number : Context.MangleNumbers)
4462 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004463 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4464 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004465 for (const auto &Number : Context.StaticLocalNumbers)
4466 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004467 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4468 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004469
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004470 // Make sure visible decls, added to DeclContexts previously loaded from
4471 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004472 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004473 I = UpdatingVisibleDecls.begin(),
4474 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4475 GetDeclRef(*I);
4476 }
4477
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004478 // Make sure all decls associated with an identifier are registered for
4479 // serialization.
4480 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4481 IDEnd = PP.getIdentifierTable().end();
4482 ID != IDEnd; ++ID) {
4483 const IdentifierInfo *II = ID->second;
4484 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4485 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4486 DEnd = SemaRef.IdResolver.end();
4487 D != DEnd; ++D) {
4488 GetDeclRef(*D);
4489 }
4490 }
4491 }
4492
Douglas Gregor5204bde2011-08-02 16:26:37 +00004493 // Form the record of special types.
4494 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004495 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004496 AddTypeRef(Context.getFILEType(), SpecialTypes);
4497 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4498 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4499 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4500 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004501 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004502 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004503
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004504 if (Chain) {
4505 // Write the mapping information describing our module dependencies and how
4506 // each of those modules were mapped into our own offset/ID space, so that
4507 // the reader can build the appropriate mapping to its own offset/ID space.
4508 // The map consists solely of a blob with the following format:
4509 // *(module-name-len:i16 module-name:len*i8
4510 // source-location-offset:i32
4511 // identifier-id:i32
4512 // preprocessed-entity-id:i32
4513 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004514 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004515 // selector-id:i32
4516 // declaration-id:i32
4517 // c++-base-specifiers-id:i32
4518 // type-id:i32)
4519 //
4520 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4521 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4522 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4523 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004524 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004525 {
4526 llvm::raw_svector_ostream Out(Buffer);
Ben Langmuir785180e2014-10-20 16:27:30 +00004527 for (ModuleFile *M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004528 using namespace llvm::support;
4529 endian::Writer<little> LE(Out);
Ben Langmuir785180e2014-10-20 16:27:30 +00004530 StringRef FileName = M->FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004531 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004532 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004533
Ben Langmuir785180e2014-10-20 16:27:30 +00004534 // Note: if a base ID was uint max, it would not be possible to load
4535 // another module after it or have more than one entity inside it.
4536 uint32_t None = std::numeric_limits<uint32_t>::max();
4537
4538 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4539 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4540 if (ShouldWrite)
4541 LE.write<uint32_t>(BaseID);
4542 else
4543 LE.write<uint32_t>(None);
4544 };
4545
Ben Langmuirfe971d92014-08-16 04:54:18 +00004546 // These values should be unique within a chain, since they will be read
4547 // as keys into ContinuousRangeMaps.
Ben Langmuir785180e2014-10-20 16:27:30 +00004548 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4549 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4550 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4551 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4552 M->NumPreprocessedEntities);
4553 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4554 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4555 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4556 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004557 }
4558 }
4559 Record.clear();
4560 Record.push_back(MODULE_OFFSET_MAP);
4561 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4562 Buffer.data(), Buffer.size());
4563 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004564
Richard Smithb9eab6d2014-03-20 19:44:17 +00004565 RecordData DeclUpdatesOffsetsRecord;
4566
Richard Smith59442b42014-03-20 20:07:19 +00004567 // Keep writing types, declarations, and declaration update records
4568 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004569 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4570 WriteTypeAbbrevs();
4571 WriteDeclAbbrevs();
Richard Smithb9eab6d2014-03-20 19:44:17 +00004572 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4573 E = DeclsToRewrite.end();
4574 I != E; ++I)
4575 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004576 do {
4577 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4578 while (!DeclTypesToEmit.empty()) {
4579 DeclOrType DOT = DeclTypesToEmit.front();
4580 DeclTypesToEmit.pop();
4581 if (DOT.isType())
4582 WriteType(DOT.getType());
4583 else
4584 WriteDecl(Context, DOT.getDecl());
4585 }
4586 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004587 Stream.ExitBlock();
4588
Richard Smithb9eab6d2014-03-20 19:44:17 +00004589 DoneWritingDeclsAndTypes = true;
4590
4591 // These things can only be done once we've written out decls and types.
4592 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004593 if (!DeclUpdatesOffsetsRecord.empty())
4594 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004595 WriteCXXBaseSpecifiersOffsets();
4596 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004597 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004598
4599 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004600 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004601 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004602 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004603 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004604 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004605 WriteFPPragmaOptions(SemaRef.getFPOptions());
4606 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004607 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004608
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004609 // If we're emitting a module, write out the submodule information.
4610 if (WritingModule)
4611 WriteSubmodules(WritingModule);
4612
Douglas Gregor5204bde2011-08-02 16:26:37 +00004613 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4614
Douglas Gregord4df8652009-04-22 22:02:47 +00004615 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004616 if (!EagerlyDeserializedDecls.empty())
4617 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004618
4619 // Write the record containing tentative definitions.
4620 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004621 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004622
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004623 // Write the record containing unused file scoped decls.
4624 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004625 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004626
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004627 // Write the record containing weak undeclared identifiers.
4628 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004629 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004630 WeakUndeclaredIdentifiers);
4631
Richard Smith78165b52013-01-10 23:43:47 +00004632 // Write the record containing locally-scoped extern "C" definitions.
4633 if (!LocallyScopedExternCDecls.empty())
4634 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4635 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004636
4637 // Write the record containing ext_vector type names.
4638 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004639 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004640
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004641 // Write the record containing VTable uses information.
4642 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004643 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004644
4645 // Write the record containing dynamic classes declarations.
4646 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004647 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004648
Nico Weber72889432014-09-06 01:25:55 +00004649 // Write the record containing potentially unused local typedefs.
4650 if (!UnusedLocalTypedefNameCandidates.empty())
4651 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4652 UnusedLocalTypedefNameCandidates);
4653
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004654 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004655 if (!PendingInstantiations.empty())
4656 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004657
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004658 // Write the record containing declaration references of Sema.
4659 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004660 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004661
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004662 // Write the record containing CUDA-specific declaration references.
4663 if (!CUDASpecialDeclRefs.empty())
4664 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004665
4666 // Write the delegating constructors.
4667 if (!DelegatingCtorDecls.empty())
4668 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004669
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004670 // Write the known namespaces.
4671 if (!KnownNamespaces.empty())
4672 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004673
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004674 // Write the undefined internal functions and variables, and inline functions.
4675 if (!UndefinedButUsed.empty())
4676 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004677
Douglas Gregor851443c2011-08-12 01:39:19 +00004678 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004679 for (auto *DC : UpdatedDeclContexts)
4680 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004681
Douglas Gregor959bb062011-12-03 01:15:29 +00004682 if (!WritingModule) {
4683 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004684 struct ModuleInfo {
4685 uint64_t ID;
4686 Module *M;
4687 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4688 };
Richard Smith56be7542014-03-21 00:33:59 +00004689 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004690 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004691 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004692 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4693 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004694 }
Richard Smith56be7542014-03-21 00:33:59 +00004695
4696 if (!Imports.empty()) {
4697 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4698 return A.ID < B.ID;
4699 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004700 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4701 return A.ID == B.ID;
4702 };
Richard Smith56be7542014-03-21 00:33:59 +00004703
4704 // Sort and deduplicate module IDs.
4705 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004706 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004707 Imports.end());
4708
4709 RecordData ImportedModules;
4710 for (const auto &Import : Imports) {
4711 ImportedModules.push_back(Import.ID);
4712 // FIXME: If the module has macros imported then later has declarations
4713 // imported, this location won't be the right one as a location for the
4714 // declaration imports.
4715 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4716 }
4717
Douglas Gregor959bb062011-12-03 01:15:29 +00004718 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4719 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004720 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004721
Douglas Gregor851443c2011-08-12 01:39:19 +00004722 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004723 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004724 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004725 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004726 WriteLateParsedTemplates(SemaRef);
Dario Domizioli13a0a382014-05-23 12:13:25 +00004727 if(!WritingModule)
4728 WriteOptimizePragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004729
Douglas Gregor08f01292009-04-17 22:13:46 +00004730 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004731 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004732 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004733 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004734 Record.push_back(NumLexicalDeclContexts);
4735 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004736 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004737 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004738}
4739
Richard Smithb9eab6d2014-03-20 19:44:17 +00004740void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004741 if (DeclUpdates.empty())
4742 return;
4743
Richard Smith59442b42014-03-20 20:07:19 +00004744 DeclUpdateMap LocalUpdates;
4745 LocalUpdates.swap(DeclUpdates);
4746
Richard Smith6ef42932014-03-20 21:02:00 +00004747 for (auto &DeclUpdate : LocalUpdates) {
4748 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004749 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004750 continue; // The decl will be written completely,no need to store updates.
4751
Richard Smithd28ac5b2014-03-22 23:33:22 +00004752 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004753 RecordData Record;
4754 for (auto &Update : DeclUpdate.second) {
4755 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4756
4757 Record.push_back(Kind);
4758 switch (Kind) {
4759 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4760 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4761 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004762 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004763 Record.push_back(GetDeclRef(Update.getDecl()));
4764 break;
4765
Richard Smith4d235792014-08-07 18:53:08 +00004766 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004767 // An updated body is emitted last, so that the reader doesn't need
4768 // to skip over the lazy body to reach statements for other records.
4769 Record.pop_back();
4770 HasUpdatedBody = true;
4771 break;
4772
Richard Smith4d235792014-08-07 18:53:08 +00004773 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4774 AddSourceLocation(Update.getLoc(), Record);
4775 break;
4776
Richard Smithcd45dbc2014-04-19 03:48:30 +00004777 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4778 auto *RD = cast<CXXRecordDecl>(D);
4779 AddUpdatedDeclContext(RD->getPrimaryContext());
4780 AddCXXDefinitionData(RD, Record);
4781 Record.push_back(WriteDeclContextLexicalBlock(
4782 *Context, const_cast<CXXRecordDecl *>(RD)));
4783
4784 // This state is sometimes updated by template instantiation, when we
4785 // switch from the specialization referring to the template declaration
4786 // to it referring to the template definition.
4787 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4788 Record.push_back(MSInfo->getTemplateSpecializationKind());
4789 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4790 } else {
4791 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4792 Record.push_back(Spec->getTemplateSpecializationKind());
4793 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
Richard Smithdf352052014-05-22 20:59:29 +00004794
4795 // The instantiation might have been resolved to a partial
4796 // specialization. If so, record which one.
4797 auto From = Spec->getInstantiatedFrom();
4798 if (auto PartialSpec =
4799 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4800 Record.push_back(true);
4801 AddDeclRef(PartialSpec, Record);
4802 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4803 Record);
4804 } else {
4805 Record.push_back(false);
4806 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004807 }
4808 Record.push_back(RD->getTagKind());
4809 AddSourceLocation(RD->getLocation(), Record);
4810 AddSourceLocation(RD->getLocStart(), Record);
4811 AddSourceLocation(RD->getRBraceLoc(), Record);
4812
4813 // Instantiation may change attributes; write them all out afresh.
4814 Record.push_back(D->hasAttrs());
4815 if (Record.back())
Craig Topper8c2a2a02014-08-30 16:55:39 +00004816 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4817 D->getAttrs().size()), Record);
Richard Smithcd45dbc2014-04-19 03:48:30 +00004818
4819 // FIXME: Ensure we don't get here for explicit instantiations.
4820 break;
4821 }
4822
Richard Smith564417a2014-03-20 21:47:22 +00004823 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4824 addExceptionSpec(
4825 *this,
4826 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4827 Record);
4828 break;
4829
Richard Smith6ef42932014-03-20 21:02:00 +00004830 case UPD_CXX_DEDUCED_RETURN_TYPE:
4831 Record.push_back(GetOrCreateTypeID(Update.getType()));
4832 break;
4833
4834 case UPD_DECL_MARKED_USED:
4835 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004836
4837 case UPD_MANGLING_NUMBER:
4838 case UPD_STATIC_LOCAL_NUMBER:
4839 Record.push_back(Update.getNumber());
4840 break;
Alexey Bataev97720002014-11-11 04:05:39 +00004841 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4842 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4843 Record);
4844 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004845 }
4846 }
4847
Richard Smithd28ac5b2014-03-22 23:33:22 +00004848 if (HasUpdatedBody) {
4849 const FunctionDecl *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00004850 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00004851 Record.push_back(Def->isInlined());
4852 AddSourceLocation(Def->getInnerLocStart(), Record);
4853 AddFunctionDefinition(Def, Record);
Richard Smith4d235792014-08-07 18:53:08 +00004854 if (auto *DD = dyn_cast<CXXDestructorDecl>(Def))
4855 Record.push_back(GetDeclRef(DD->getOperatorDelete()));
Richard Smithd28ac5b2014-03-22 23:33:22 +00004856 }
4857
Richard Smithcd45dbc2014-04-19 03:48:30 +00004858 OffsetsRecord.push_back(GetDeclRef(D));
4859 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4860
Richard Smith6ef42932014-03-20 21:02:00 +00004861 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004862
Richard Smithb9eab6d2014-03-20 19:44:17 +00004863 // Flush any statements that were written as part of this update record.
4864 FlushStmts();
Richard Smithcd45dbc2014-04-19 03:48:30 +00004865
4866 // Flush C++ base specifiers, if there are any.
4867 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004868 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004869}
4870
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004871void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004872 if (ReplacedDecls.empty())
4873 return;
4874
4875 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004876 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4877 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004878 Record.push_back(I->ID);
4879 Record.push_back(I->Offset);
4880 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004881 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004882 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004883}
4884
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004885void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004886 Record.push_back(Loc.getRawEncoding());
4887}
4888
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004889void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004890 AddSourceLocation(Range.getBegin(), Record);
4891 AddSourceLocation(Range.getEnd(), Record);
4892}
4893
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004894void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004895 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004896 const uint64_t *Words = Value.getRawData();
4897 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004898}
4899
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004900void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004901 Record.push_back(Value.isUnsigned());
4902 AddAPInt(Value, Record);
4903}
4904
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004905void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004906 AddAPInt(Value.bitcastToAPInt(), Record);
4907}
4908
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004909void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004910 Record.push_back(getIdentifierRef(II));
4911}
4912
Sebastian Redl539c5062010-08-18 23:57:32 +00004913IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00004914 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004915 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004916
Sebastian Redl539c5062010-08-18 23:57:32 +00004917 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004918 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004919 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004920 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004921}
4922
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004923MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004924 // Don't emit builtin macros like __LINE__ to the AST file unless they
4925 // have been redefined by the header (in which case they are not
4926 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00004927 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004928 return 0;
4929
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004930 MacroID &ID = MacroIDs[MI];
4931 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004932 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004933 MacroInfoToEmitData Info = { Name, MI, ID };
4934 MacroInfosToEmit.push_back(Info);
4935 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004936 return ID;
4937}
4938
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004939MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00004940 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004941 return 0;
4942
4943 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4944 return MacroIDs[MI];
4945}
4946
4947uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4948 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4949 return IdentMacroDirectivesOffsetMap[Name];
4950}
4951
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004952void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004953 Record.push_back(getSelectorRef(SelRef));
4954}
4955
Sebastian Redl539c5062010-08-18 23:57:32 +00004956SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00004957 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004958 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004959 }
4960
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004961 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004962 if (SID == 0 && Chain) {
4963 // This might trigger a ReadSelector callback, which will set the ID for
4964 // this selector.
4965 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004966 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004967 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004968 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004969 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004970 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004971 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004972 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004973}
4974
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004975void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004976 AddDeclRef(Temp->getDestructor(), Record);
4977}
4978
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004979void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4980 CXXBaseSpecifier const *BasesEnd,
4981 RecordDataImpl &Record) {
4982 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4983 CXXBaseSpecifiersToWrite.push_back(
4984 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4985 Bases, BasesEnd));
4986 Record.push_back(NextCXXBaseSpecifiersID++);
4987}
4988
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004989void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004990 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004991 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004992 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004993 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004994 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004995 break;
4996 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004997 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004998 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004999 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00005000 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005001 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005002 break;
5003 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00005004 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005005 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00005006 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005007 break;
John McCall0ad16662009-10-29 08:12:44 +00005008 case TemplateArgument::Null:
5009 case TemplateArgument::Integral:
5010 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005011 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005012 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005013 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005014 break;
5015 }
5016}
5017
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005018void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005019 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005020 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005021
5022 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5023 bool InfoHasSameExpr
5024 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5025 Record.push_back(InfoHasSameExpr);
5026 if (InfoHasSameExpr)
5027 return; // Avoid storing the same expr twice.
5028 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005029 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
5030 Record);
5031}
5032
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005033void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
5034 RecordDataImpl &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00005035 if (!TInfo) {
John McCall8f115c62009-10-16 21:56:05 +00005036 AddTypeRef(QualType(), Record);
5037 return;
5038 }
5039
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005040 AddTypeLoc(TInfo->getTypeLoc(), Record);
5041}
5042
5043void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
5044 AddTypeRef(TL.getType(), Record);
5045
John McCall8f115c62009-10-16 21:56:05 +00005046 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005047 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005048 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005049}
5050
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005051void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005052 Record.push_back(GetOrCreateTypeID(T));
5053}
5054
Douglas Gregoreda8e122011-08-09 15:13:55 +00005055TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005056 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00005057 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005058 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
5059}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005060
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005061TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005062 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00005063 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005064 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005065}
5066
5067TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
5068 if (T.isNull())
5069 return TypeIdx();
5070 assert(!T.getLocalFastQualifiers());
5071
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00005072 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005073 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005074 if (DoneWritingDeclsAndTypes) {
5075 assert(0 && "New type seen after serializing all the types to emit!");
5076 return TypeIdx();
5077 }
5078
Douglas Gregor1970d882009-04-26 03:49:13 +00005079 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00005080 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005081 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00005082 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00005083 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005084 return Idx;
5085}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005086
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005087TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00005088 if (T.isNull())
5089 return TypeIdx();
5090 assert(!T.getLocalFastQualifiers());
5091
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005092 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5093 assert(I != TypeIdxs.end() && "Type not emitted!");
5094 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005095}
5096
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005097void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005098 Record.push_back(GetDeclRef(D));
5099}
5100
Sebastian Redl539c5062010-08-18 23:57:32 +00005101DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005102 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005103
5104 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005105 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005106 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005107
5108 // If D comes from an AST file, its declaration ID is already known and
5109 // fixed.
5110 if (D->isFromASTFile())
5111 return D->getGlobalID();
5112
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005113 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005114 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005115 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005116 if (DoneWritingDeclsAndTypes) {
5117 assert(0 && "New decl seen after serializing all the decls to emit!");
5118 return 0;
5119 }
5120
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005121 // We haven't seen this declaration before. Give it a new ID and
5122 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005123 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005124 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005125 }
5126
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005127 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005128}
5129
Sebastian Redl539c5062010-08-18 23:57:32 +00005130DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005131 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005132 return 0;
5133
Douglas Gregorb3163e52012-01-05 22:33:30 +00005134 // If D comes from an AST file, its declaration ID is already known and
5135 // fixed.
5136 if (D->isFromASTFile())
5137 return D->getGlobalID();
5138
Douglas Gregore84a9da2009-04-20 20:36:09 +00005139 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5140 return DeclIDs[D];
5141}
5142
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005143void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005144 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005145 assert(D);
5146
5147 SourceLocation Loc = D->getLocation();
5148 if (Loc.isInvalid())
5149 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005150
5151 // We only keep track of the file-level declarations of each file.
5152 if (!D->getLexicalDeclContext()->isFileContext())
5153 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005154 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5155 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005156 if (isa<ParmVarDecl>(D))
5157 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005158
5159 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005160 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005161 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005162 FileID FID;
5163 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005164 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005165 if (FID.isInvalid())
5166 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005167 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005168
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005169 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005170 if (!Info)
5171 Info = new DeclIDInFileInfo();
5172
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005173 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005174 LocDeclIDsTy &Decls = Info->DeclIDs;
5175
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005176 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005177 Decls.push_back(LocDecl);
5178 return;
5179 }
5180
Benjamin Kramer45025c02013-08-24 13:22:59 +00005181 LocDeclIDsTy::iterator I =
5182 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005183
5184 Decls.insert(I, LocDecl);
5185}
5186
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005187void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00005188 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005189 Record.push_back(Name.getNameKind());
5190 switch (Name.getNameKind()) {
5191 case DeclarationName::Identifier:
5192 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5193 break;
5194
5195 case DeclarationName::ObjCZeroArgSelector:
5196 case DeclarationName::ObjCOneArgSelector:
5197 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00005198 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005199 break;
5200
5201 case DeclarationName::CXXConstructorName:
5202 case DeclarationName::CXXDestructorName:
5203 case DeclarationName::CXXConversionFunctionName:
5204 AddTypeRef(Name.getCXXNameType(), Record);
5205 break;
5206
5207 case DeclarationName::CXXOperatorName:
5208 Record.push_back(Name.getCXXOverloadedOperator());
5209 break;
5210
Alexis Hunt3d221f22009-11-29 07:34:05 +00005211 case DeclarationName::CXXLiteralOperatorName:
5212 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5213 break;
5214
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005215 case DeclarationName::CXXUsingDirective:
5216 // No extra data to emit
5217 break;
5218 }
5219}
Chris Lattnerca025db2010-05-07 21:43:38 +00005220
Richard Smithd08aeb62014-08-28 01:33:39 +00005221unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5222 assert(needsAnonymousDeclarationNumber(D) &&
5223 "expected an anonymous declaration");
5224
5225 // Number the anonymous declarations within this context, if we've not
5226 // already done so.
5227 auto It = AnonymousDeclarationNumbers.find(D);
5228 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005229 auto *DC = D->getLexicalDeclContext();
5230 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5231 AnonymousDeclarationNumbers[ND] = Number;
5232 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005233
5234 It = AnonymousDeclarationNumbers.find(D);
5235 assert(It != AnonymousDeclarationNumbers.end() &&
5236 "declaration not found within its lexical context");
5237 }
5238
5239 return It->second;
5240}
5241
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005242void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005243 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005244 switch (Name.getNameKind()) {
5245 case DeclarationName::CXXConstructorName:
5246 case DeclarationName::CXXDestructorName:
5247 case DeclarationName::CXXConversionFunctionName:
5248 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5249 break;
5250
5251 case DeclarationName::CXXOperatorName:
5252 AddSourceLocation(
5253 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5254 Record);
5255 AddSourceLocation(
5256 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5257 Record);
5258 break;
5259
5260 case DeclarationName::CXXLiteralOperatorName:
5261 AddSourceLocation(
5262 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5263 Record);
5264 break;
5265
5266 case DeclarationName::Identifier:
5267 case DeclarationName::ObjCZeroArgSelector:
5268 case DeclarationName::ObjCOneArgSelector:
5269 case DeclarationName::ObjCMultiArgSelector:
5270 case DeclarationName::CXXUsingDirective:
5271 break;
5272 }
5273}
5274
5275void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005276 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005277 AddDeclarationName(NameInfo.getName(), Record);
5278 AddSourceLocation(NameInfo.getLoc(), Record);
5279 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5280}
5281
5282void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005283 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00005284 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005285 Record.push_back(Info.NumTemplParamLists);
5286 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5287 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5288}
5289
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005290void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005291 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005292 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005293 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005294 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005295
5296 // Push each of the NNS's onto a stack for serialization in reverse order.
5297 while (NNS) {
5298 NestedNames.push_back(NNS);
5299 NNS = NNS->getPrefix();
5300 }
5301
5302 Record.push_back(NestedNames.size());
5303 while(!NestedNames.empty()) {
5304 NNS = NestedNames.pop_back_val();
5305 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5306 Record.push_back(Kind);
5307 switch (Kind) {
5308 case NestedNameSpecifier::Identifier:
5309 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5310 break;
5311
5312 case NestedNameSpecifier::Namespace:
5313 AddDeclRef(NNS->getAsNamespace(), Record);
5314 break;
5315
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005316 case NestedNameSpecifier::NamespaceAlias:
5317 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5318 break;
5319
Chris Lattnerca025db2010-05-07 21:43:38 +00005320 case NestedNameSpecifier::TypeSpec:
5321 case NestedNameSpecifier::TypeSpecWithTemplate:
5322 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5323 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5324 break;
5325
5326 case NestedNameSpecifier::Global:
5327 // Don't need to write an associated value.
5328 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005329
5330 case NestedNameSpecifier::Super:
5331 AddDeclRef(NNS->getAsRecordDecl(), Record);
5332 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005333 }
5334 }
5335}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005336
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005337void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5338 RecordDataImpl &Record) {
5339 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005340 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005341 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005342
5343 // Push each of the nested-name-specifiers's onto a stack for
5344 // serialization in reverse order.
5345 while (NNS) {
5346 NestedNames.push_back(NNS);
5347 NNS = NNS.getPrefix();
5348 }
5349
5350 Record.push_back(NestedNames.size());
5351 while(!NestedNames.empty()) {
5352 NNS = NestedNames.pop_back_val();
5353 NestedNameSpecifier::SpecifierKind Kind
5354 = NNS.getNestedNameSpecifier()->getKind();
5355 Record.push_back(Kind);
5356 switch (Kind) {
5357 case NestedNameSpecifier::Identifier:
5358 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5359 AddSourceRange(NNS.getLocalSourceRange(), Record);
5360 break;
5361
5362 case NestedNameSpecifier::Namespace:
5363 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5364 AddSourceRange(NNS.getLocalSourceRange(), Record);
5365 break;
5366
5367 case NestedNameSpecifier::NamespaceAlias:
5368 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5369 AddSourceRange(NNS.getLocalSourceRange(), Record);
5370 break;
5371
5372 case NestedNameSpecifier::TypeSpec:
5373 case NestedNameSpecifier::TypeSpecWithTemplate:
5374 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5375 AddTypeLoc(NNS.getTypeLoc(), Record);
5376 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5377 break;
5378
5379 case NestedNameSpecifier::Global:
5380 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5381 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005382
5383 case NestedNameSpecifier::Super:
5384 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5385 AddSourceRange(NNS.getLocalSourceRange(), Record);
5386 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005387 }
5388 }
5389}
5390
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005391void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005392 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005393 Record.push_back(Kind);
5394 switch (Kind) {
5395 case TemplateName::Template:
5396 AddDeclRef(Name.getAsTemplateDecl(), Record);
5397 break;
5398
5399 case TemplateName::OverloadedTemplate: {
5400 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5401 Record.push_back(OvT->size());
5402 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5403 I != E; ++I)
5404 AddDeclRef(*I, Record);
5405 break;
5406 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005407
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005408 case TemplateName::QualifiedTemplate: {
5409 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5410 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5411 Record.push_back(QualT->hasTemplateKeyword());
5412 AddDeclRef(QualT->getTemplateDecl(), Record);
5413 break;
5414 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005415
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005416 case TemplateName::DependentTemplate: {
5417 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5418 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5419 Record.push_back(DepT->isIdentifier());
5420 if (DepT->isIdentifier())
5421 AddIdentifierRef(DepT->getIdentifier(), Record);
5422 else
5423 Record.push_back(DepT->getOperator());
5424 break;
5425 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005426
5427 case TemplateName::SubstTemplateTemplateParm: {
5428 SubstTemplateTemplateParmStorage *subst
5429 = Name.getAsSubstTemplateTemplateParm();
5430 AddDeclRef(subst->getParameter(), Record);
5431 AddTemplateName(subst->getReplacement(), Record);
5432 break;
5433 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005434
5435 case TemplateName::SubstTemplateTemplateParmPack: {
5436 SubstTemplateTemplateParmPackStorage *SubstPack
5437 = Name.getAsSubstTemplateTemplateParmPack();
5438 AddDeclRef(SubstPack->getParameterPack(), Record);
5439 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5440 break;
5441 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005442 }
5443}
5444
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005445void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005446 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005447 Record.push_back(Arg.getKind());
5448 switch (Arg.getKind()) {
5449 case TemplateArgument::Null:
5450 break;
5451 case TemplateArgument::Type:
5452 AddTypeRef(Arg.getAsType(), Record);
5453 break;
5454 case TemplateArgument::Declaration:
5455 AddDeclRef(Arg.getAsDecl(), Record);
David Blaikie952a9b12014-10-17 18:00:12 +00005456 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005457 break;
5458 case TemplateArgument::NullPtr:
5459 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005460 break;
5461 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005462 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005463 AddTypeRef(Arg.getIntegralType(), Record);
5464 break;
5465 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005466 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5467 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005468 case TemplateArgument::TemplateExpansion:
5469 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005470 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005471 Record.push_back(*NumExpansions + 1);
5472 else
5473 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005474 break;
5475 case TemplateArgument::Expression:
5476 AddStmt(Arg.getAsExpr());
5477 break;
5478 case TemplateArgument::Pack:
5479 Record.push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005480 for (const auto &P : Arg.pack_elements())
5481 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005482 break;
5483 }
5484}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005485
5486void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005487ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005488 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005489 assert(TemplateParams && "No TemplateParams!");
5490 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5491 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5492 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5493 Record.push_back(TemplateParams->size());
5494 for (TemplateParameterList::const_iterator
5495 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5496 P != PEnd; ++P)
5497 AddDeclRef(*P, Record);
5498}
5499
5500/// \brief Emit a template argument list.
5501void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005502ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005503 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005504 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005505 Record.push_back(TemplateArgs->size());
5506 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005507 AddTemplateArgument(TemplateArgs->get(i), Record);
5508}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005509
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005510void
5511ASTWriter::AddASTTemplateArgumentListInfo
5512(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5513 assert(ASTTemplArgList && "No ASTTemplArgList!");
5514 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5515 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5516 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5517 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5518 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5519 AddTemplateArgumentLoc(TemplArgs[i], Record);
5520}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005521
5522void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005523ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005524 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005525 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005526 I = Set.begin(), E = Set.end(); I != E; ++I) {
5527 AddDeclRef(I.getDecl(), Record);
5528 Record.push_back(I.getAccess());
5529 }
5530}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005531
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005532void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005533 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005534 Record.push_back(Base.isVirtual());
5535 Record.push_back(Base.isBaseOfClass());
5536 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005537 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005538 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005539 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005540 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5541 : SourceLocation(),
5542 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005543}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005544
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005545void ASTWriter::FlushCXXBaseSpecifiers() {
5546 RecordData Record;
5547 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5548 Record.clear();
5549
5550 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005551 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005552 if (Index == CXXBaseSpecifiersOffsets.size())
5553 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5554 else {
5555 if (Index > CXXBaseSpecifiersOffsets.size())
5556 CXXBaseSpecifiersOffsets.resize(Index + 1);
5557 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5558 }
5559
5560 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5561 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5562 Record.push_back(BEnd - B);
5563 for (; B != BEnd; ++B)
5564 AddCXXBaseSpecifier(*B, Record);
5565 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005566
5567 // Flush any expressions that were written as part of the base specifiers.
5568 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005569 }
5570
5571 CXXBaseSpecifiersToWrite.clear();
5572}
5573
Alexis Hunt1d792652011-01-08 20:30:50 +00005574void ASTWriter::AddCXXCtorInitializers(
5575 const CXXCtorInitializer * const *CtorInitializers,
5576 unsigned NumCtorInitializers,
5577 RecordDataImpl &Record) {
5578 Record.push_back(NumCtorInitializers);
5579 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5580 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005581
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005582 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005583 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005584 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005585 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005586 } else if (Init->isDelegatingInitializer()) {
5587 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005588 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005589 } else if (Init->isMemberInitializer()){
5590 Record.push_back(CTOR_INITIALIZER_MEMBER);
5591 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005592 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005593 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5594 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005595 }
Francois Pichetd583da02010-12-04 09:14:42 +00005596
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005597 AddSourceLocation(Init->getMemberLocation(), Record);
5598 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005599 AddSourceLocation(Init->getLParenLoc(), Record);
5600 AddSourceLocation(Init->getRParenLoc(), Record);
5601 Record.push_back(Init->isWritten());
5602 if (Init->isWritten()) {
5603 Record.push_back(Init->getSourceOrder());
5604 } else {
5605 Record.push_back(Init->getNumArrayIndices());
5606 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5607 AddDeclRef(Init->getArrayIndex(i), Record);
5608 }
5609 }
5610}
5611
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005612void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Richard Smith053f6c62014-05-16 23:01:30 +00005613 auto &Data = D->data();
Douglas Gregor99ae8062012-02-14 17:54:36 +00005614 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005615 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005616 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005617 Record.push_back(Data.Aggregate);
5618 Record.push_back(Data.PlainOldData);
5619 Record.push_back(Data.Empty);
5620 Record.push_back(Data.Polymorphic);
5621 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005622 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005623 Record.push_back(Data.HasNoNonEmptyBases);
5624 Record.push_back(Data.HasPrivateFields);
5625 Record.push_back(Data.HasProtectedFields);
5626 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005627 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005628 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005629 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005630 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005631 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005632 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5633 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5634 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5635 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5636 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5637 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005638 Record.push_back(Data.HasTrivialSpecialMembers);
Richard Smithb45a6f72014-04-17 20:33:01 +00005639 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith328aae52012-11-30 05:11:39 +00005640 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005641 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005642 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005643 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005644 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005645 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005646 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005647 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005648 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5649 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5650 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5651 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005652 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005653
5654 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005655 if (Data.NumBases > 0)
5656 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5657 Record);
5658
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005659 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5660 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005661 if (Data.NumVBases > 0)
5662 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5663 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005664
Richard Smitha4ba74c2013-08-30 04:46:40 +00005665 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5666 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005667 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005668 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005669
5670 // Add lambda-specific data.
5671 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005672 auto &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005673 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005674 Record.push_back(Lambda.IsGenericLambda);
5675 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005676 Record.push_back(Lambda.NumCaptures);
5677 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005678 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005679 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005680 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005681 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005682 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor99ae8062012-02-14 17:54:36 +00005683 AddSourceLocation(Capture.getLocation(), Record);
5684 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005685 Record.push_back(Capture.getCaptureKind());
5686 switch (Capture.getCaptureKind()) {
5687 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005688 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005689 break;
5690 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005691 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005692 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005693 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smithba71c082013-05-16 06:20:58 +00005694 AddDeclRef(Var, Record);
5695 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5696 : SourceLocation(),
5697 Record);
5698 break;
5699 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005700 }
5701 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005702}
5703
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005704void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005705 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005706 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005707 assert(FirstDeclID == NextDeclID &&
5708 FirstTypeID == NextTypeID &&
5709 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005710 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005711 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005712 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005713 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005714
Sebastian Redl07a89a82010-07-30 00:29:29 +00005715 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005716
Douglas Gregordf0c1512011-08-18 04:12:04 +00005717 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5718 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5719 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005720 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005721 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005722 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005723 NextDeclID = FirstDeclID;
5724 NextTypeID = FirstTypeID;
5725 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005726 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005727 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005728 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005729}
5730
Sebastian Redl539c5062010-08-18 23:57:32 +00005731void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005732 // Always keep the highest ID. See \p TypeRead() for more information.
5733 IdentID &StoredID = IdentifierIDs[II];
5734 if (ID > StoredID)
5735 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005736}
5737
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005738void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005739 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005740 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005741 if (ID > StoredID)
5742 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005743}
5744
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005745void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005746 // Always take the highest-numbered type index. This copes with an interesting
5747 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005748 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005749 // keep the higher-numbered entry so that we can properly write it out to
5750 // the AST file.
5751 TypeIdx &StoredIdx = TypeIdxs[T];
5752 if (Idx.getIndex() >= StoredIdx.getIndex())
5753 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005754}
5755
Sebastian Redl539c5062010-08-18 23:57:32 +00005756void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005757 // Always keep the highest ID. See \p TypeRead() for more information.
5758 SelectorID &StoredID = SelectorIDs[S];
5759 if (ID > StoredID)
5760 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005761}
Douglas Gregor91096292010-10-02 19:29:26 +00005762
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005763void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005764 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005765 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005766 MacroDefinitions[MD] = ID;
5767}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005768
Douglas Gregore37a85a2011-12-02 17:30:13 +00005769void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5770 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5771 SubmoduleIDs[Mod] = ID;
5772}
5773
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005774void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005775 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005776 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005777 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5778 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005779 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005780 // A forward reference was mutated into a definition. Rewrite it.
5781 // FIXME: This happens during template instantiation, should we
5782 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005783 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5784 "completed a tag from another module but not by instantiation?");
5785 DeclUpdates[RD].push_back(
5786 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005787 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005788 }
5789}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005790
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005791void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5792 // TU and namespaces are handled elsewhere.
5793 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5794 return;
5795
Douglas Gregorb3722e22011-09-09 23:01:35 +00005796 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005797 return; // Not a source decl added to a DeclContext from PCH.
5798
Douglas Gregor9f782892013-01-21 15:25:38 +00005799 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005800 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005801 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005802 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005803}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005804
5805void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5806 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005807 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005808 return; // Not a source member added to a class from PCH.
5809 if (!isa<CXXMethodDecl>(D))
5810 return; // We are interested in lazily declared implicit methods.
5811
5812 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005813 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005814 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005815 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005816}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005817
5818void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5819 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005820 // The specializations set is kept in the canonical template.
5821 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005822 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005823 return; // Not a source specialization added to a template from PCH.
5824
Richard Smithe9a8bc32014-09-30 00:45:29 +00005825 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005826 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5827 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005828}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005829
Larisse Voufo39a1e502013-08-06 01:03:05 +00005830void ASTWriter::AddedCXXTemplateSpecialization(
5831 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5832 // The specializations set is kept in the canonical template.
Larisse Voufo39a1e502013-08-06 01:03:05 +00005833 TD = TD->getCanonicalDecl();
5834 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5835 return; // Not a source specialization added to a template from PCH.
5836
Richard Smithe9a8bc32014-09-30 00:45:29 +00005837 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005838 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5839 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005840}
5841
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005842void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5843 const FunctionDecl *D) {
5844 // The specializations set is kept in the canonical template.
5845 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005846 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005847 return; // Not a source specialization added to a template from PCH.
5848
Richard Smithe9a8bc32014-09-30 00:45:29 +00005849 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005850 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5851 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005852}
5853
Richard Smith564417a2014-03-20 21:47:22 +00005854void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5855 assert(!WritingAST && "Already writing the AST!");
5856 FD = FD->getCanonicalDecl();
5857 if (!FD->isFromASTFile())
5858 return; // Not a function declared in PCH and defined outside.
5859
5860 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5861}
5862
Richard Smith1fa5d642013-05-11 05:45:24 +00005863void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5864 assert(!WritingAST && "Already writing the AST!");
5865 FD = FD->getCanonicalDecl();
5866 if (!FD->isFromASTFile())
5867 return; // Not a function declared in PCH and defined outside.
5868
Aaron Ballman4f45b712014-03-21 15:22:56 +00005869 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith1fa5d642013-05-11 05:45:24 +00005870}
5871
Sebastian Redlab238a72011-04-24 16:28:06 +00005872void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005873 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005874 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005875 return; // Declaration not imported from PCH.
5876
Richard Smith4d235792014-08-07 18:53:08 +00005877 // Implicit function decl from a PCH was defined.
5878 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005879}
5880
Richard Smithd28ac5b2014-03-22 23:33:22 +00005881void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5882 assert(!WritingAST && "Already writing the AST!");
5883 if (!D->isFromASTFile())
5884 return;
5885
Richard Smithd28ac5b2014-03-22 23:33:22 +00005886 DeclUpdates[D].push_back(
Richard Smith4d235792014-08-07 18:53:08 +00005887 DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00005888}
5889
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005890void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005891 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005892 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005893 return;
5894
5895 // Since the actual instantiation is delayed, this really means that we need
5896 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005897 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005898 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5899 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005900}
5901
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005902void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5903 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005904 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005905 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005906 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005907
5908 assert(IFD->getDefinition() && "Category on a class without a definition?");
5909 ObjCClassesWithCategories.insert(
5910 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005911}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005912
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005913
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005914void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5915 const ObjCPropertyDecl *OrigProp,
5916 const ObjCCategoryDecl *ClassExt) {
5917 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5918 if (!D)
5919 return;
5920
5921 assert(!WritingAST && "Already writing the AST!");
5922 if (!D->isFromASTFile())
5923 return; // Declaration not imported from PCH.
5924
5925 RewriteDecl(D);
5926}
Eli Friedman276dd182013-09-05 00:02:25 +00005927
5928void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5929 assert(!WritingAST && "Already writing the AST!");
5930 if (!D->isFromASTFile())
5931 return;
5932
Aaron Ballman4f45b712014-03-21 15:22:56 +00005933 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005934}
Alexey Bataev97720002014-11-11 04:05:39 +00005935
5936void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5937 assert(!WritingAST && "Already writing the AST!");
5938 if (!D->isFromASTFile())
5939 return;
5940
5941 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5942}