blob: 887876f284278e02c420cff123031c64b3c76acd [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000045#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000047#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000048#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000049#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000050#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000051#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000052#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000053#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000054using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000055using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056
Sebastian Redl3df5a082010-07-30 17:03:48 +000057template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000058static StringRef data(const std::vector<T, Allocator> &v) {
59 if (v.empty()) return StringRef();
60 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000061 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000062}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000063
64template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000065static StringRef data(const SmallVectorImpl<T> &v) {
66 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000067 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000068}
69
Douglas Gregoref84c4b2009-04-09 22:27:44 +000070//===----------------------------------------------------------------------===//
71// Type serialization
72//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000073
Douglas Gregoref84c4b2009-04-09 22:27:44 +000074namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000075 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000076 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000077 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000078
79 public:
80 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000081 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000083 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000084 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000085
86 void VisitArrayType(const ArrayType *T);
87 void VisitFunctionType(const FunctionType *T);
88 void VisitTagType(const TagType *T);
89
90#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
91#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092#include "clang/AST/TypeNodes.def"
93 };
94}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000097 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098}
99
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000102 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103}
104
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000107 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108}
109
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000112 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000116 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
117 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000118 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119}
120
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000122 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000123 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000124}
125
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000126void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000127 Writer.AddTypeRef(T->getPointeeType(), Record);
128 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000129 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130}
131
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133 Writer.AddTypeRef(T->getElementType(), Record);
134 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000135 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139 VisitArrayType(T);
140 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000146 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147}
148
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000151 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
152 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000153 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000154 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155}
156
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158 Writer.AddTypeRef(T->getElementType(), Record);
159 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000160 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000161 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162}
163
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000166 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167}
168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000171 FunctionType::ExtInfo C = T->getExtInfo();
172 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000173 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000174 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000175 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000176 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000177 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178}
179
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000182 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183}
184
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000185void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000186 VisitFunctionType(T);
187 Record.push_back(T->getNumArgs());
188 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
189 Writer.AddTypeRef(T->getArgType(I), Record);
190 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000191 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000193 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000194 Record.push_back(T->getExceptionSpecType());
195 if (T->getExceptionSpecType() == EST_Dynamic) {
196 Record.push_back(T->getNumExceptions());
197 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
198 Writer.AddTypeRef(T->getExceptionType(I), Record);
199 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
200 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000201 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
203 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000204 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
205 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000206 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000207 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000208}
209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000211 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000212 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000213}
John McCallb96ec562009-12-04 22:46:56 +0000214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000217 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
218 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000223 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000229 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230}
231
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000232void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000233 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000234 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000235 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000236}
237
Alexis Hunte852b102011-05-24 22:41:36 +0000238void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
239 Writer.AddTypeRef(T->getBaseType(), Record);
240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
241 Record.push_back(T->getUTTKind());
242 Code = TYPE_UNARY_TRANSFORM;
243}
244
Richard Smith30482bc2011-02-20 03:19:35 +0000245void ASTTypeWriter::VisitAutoType(const AutoType *T) {
246 Writer.AddTypeRef(T->getDeducedType(), Record);
247 Code = TYPE_AUTO;
248}
249
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000251 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000252 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000253 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000254 "Cannot serialize in the middle of a type definition");
255}
256
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000257void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000259 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260}
261
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000262void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000264 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000265}
266
John McCall81904512011-01-06 01:58:22 +0000267void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
268 Writer.AddTypeRef(T->getModifiedType(), Record);
269 Writer.AddTypeRef(T->getEquivalentType(), Record);
270 Record.push_back(T->getAttrKind());
271 Code = TYPE_ATTRIBUTED;
272}
273
Mike Stump11289f42009-09-09 15:08:12 +0000274void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000275ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000276 const SubstTemplateTypeParmType *T) {
277 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
278 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000279 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000280}
281
282void
Douglas Gregorada4b792011-01-14 02:55:32 +0000283ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
284 const SubstTemplateTypeParmPackType *T) {
285 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
286 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
287 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
288}
289
290void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000291ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000292 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000293 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000294 Writer.AddTemplateName(T->getTemplateName(), Record);
295 Record.push_back(T->getNumArgs());
296 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
297 ArgI != ArgE; ++ArgI)
298 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000299 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
300 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000301 : T->getCanonicalTypeInternal(),
302 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000303 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000308 VisitArrayType(T);
309 Writer.AddStmt(T->getSizeExpr());
310 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000311 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000312}
313
314void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000315ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000316 const DependentSizedExtVectorType *T) {
317 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000318 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000322ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000323 Record.push_back(T->getDepth());
324 Record.push_back(T->getIndex());
325 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000326 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000327 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000328}
329
330void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000331ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000332 Record.push_back(T->getKeyword());
333 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
334 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000335 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
336 : T->getCanonicalTypeInternal(),
337 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000338 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000339}
340
341void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000342ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000343 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000344 Record.push_back(T->getKeyword());
345 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
346 Writer.AddIdentifierRef(T->getIdentifier(), Record);
347 Record.push_back(T->getNumArgs());
348 for (DependentTemplateSpecializationType::iterator
349 I = T->begin(), E = T->end(); I != E; ++I)
350 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000351 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000352}
353
Douglas Gregord2fa7662010-12-20 02:24:11 +0000354void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
355 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000356 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
357 Record.push_back(*NumExpansions + 1);
358 else
359 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000360 Code = TYPE_PACK_EXPANSION;
361}
362
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000363void ASTTypeWriter::VisitParenType(const ParenType *T) {
364 Writer.AddTypeRef(T->getInnerType(), Record);
365 Code = TYPE_PAREN;
366}
367
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000368void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000369 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000370 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
371 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000372 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000373}
374
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000375void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000376 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000377 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000378 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000379}
380
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000381void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000382 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000384}
385
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000386void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000387 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000389 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000390 E = T->qual_end(); I != E; ++I)
391 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000392 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000393}
394
Steve Narofffb4330f2009-06-17 22:40:22 +0000395void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000396ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000397 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000398 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000399}
400
Eli Friedman0dfb8892011-10-06 23:00:33 +0000401void
402ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
403 Writer.AddTypeRef(T->getValueType(), Record);
404 Code = TYPE_ATOMIC;
405}
406
John McCall8f115c62009-10-16 21:56:05 +0000407namespace {
408
409class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000410 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000411 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000412
413public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000414 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000415 : Writer(Writer), Record(Record) { }
416
John McCall17001972009-10-18 01:05:36 +0000417#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000418#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000419 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000420#include "clang/AST/TypeLocNodes.def"
421
John McCall17001972009-10-18 01:05:36 +0000422 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
423 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000424};
425
426}
427
John McCall17001972009-10-18 01:05:36 +0000428void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
429 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000430}
John McCall17001972009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000432 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
433 if (TL.needsExtraLocalData()) {
434 Record.push_back(TL.getWrittenTypeSpec());
435 Record.push_back(TL.getWrittenSignSpec());
436 Record.push_back(TL.getWrittenWidthSpec());
437 Record.push_back(TL.hasModeAttr());
438 }
John McCall8f115c62009-10-16 21:56:05 +0000439}
John McCall17001972009-10-18 01:05:36 +0000440void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000442}
John McCall17001972009-10-18 01:05:36 +0000443void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000445}
John McCall17001972009-10-18 01:05:36 +0000446void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000448}
John McCall17001972009-10-18 01:05:36 +0000449void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000451}
John McCall17001972009-10-18 01:05:36 +0000452void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000454}
John McCall17001972009-10-18 01:05:36 +0000455void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000457 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000458}
John McCall17001972009-10-18 01:05:36 +0000459void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
461 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
462 Record.push_back(TL.getSizeExpr() ? 1 : 0);
463 if (TL.getSizeExpr())
464 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000465}
John McCall17001972009-10-18 01:05:36 +0000466void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
473 VisitArrayTypeLoc(TL);
474}
475void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
476 DependentSizedArrayTypeLoc TL) {
477 VisitArrayTypeLoc(TL);
478}
479void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
480 DependentSizedExtVectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getNameLoc(), Record);
488}
489void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000490 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000491 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
492 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000493 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall17001972009-10-18 01:05:36 +0000494 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
495 Writer.AddDeclRef(TL.getArg(i), Record);
496}
497void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
498 VisitFunctionTypeLoc(TL);
499}
500void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
501 VisitFunctionTypeLoc(TL);
502}
John McCallb96ec562009-12-04 22:46:56 +0000503void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
John McCall17001972009-10-18 01:05:36 +0000506void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getNameLoc(), Record);
508}
509void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000510 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
511 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
512 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000513}
514void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000515 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
516 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
517 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
518 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000519}
520void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
Alexis Hunte852b102011-05-24 22:41:36 +0000523void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getKWLoc(), Record);
525 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
526 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
527 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
528}
Richard Smith30482bc2011-02-20 03:19:35 +0000529void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
John McCall17001972009-10-18 01:05:36 +0000532void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
535void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getNameLoc(), Record);
537}
John McCall81904512011-01-06 01:58:22 +0000538void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
540 if (TL.hasAttrOperand()) {
541 SourceRange range = TL.getAttrOperandParensRange();
542 Writer.AddSourceLocation(range.getBegin(), Record);
543 Writer.AddSourceLocation(range.getEnd(), Record);
544 }
545 if (TL.hasAttrExprOperand()) {
546 Expr *operand = TL.getAttrExprOperand();
547 Record.push_back(operand ? 1 : 0);
548 if (operand) Writer.AddStmt(operand);
549 } else if (TL.hasAttrEnumOperand()) {
550 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
551 }
552}
John McCall17001972009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
John McCallcebee162009-10-18 09:09:24 +0000556void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
557 SubstTemplateTypeParmTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
Douglas Gregorada4b792011-01-14 02:55:32 +0000560void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
561 SubstTemplateTypeParmPackTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
John McCall17001972009-10-18 01:05:36 +0000564void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
565 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000566 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000567 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
568 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
569 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
570 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000571 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
572 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000573}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000574void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
576 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
577}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000578void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000579 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000580 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000581}
John McCalle78aac42010-03-10 03:28:59 +0000582void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000585void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000586 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000587 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
589}
John McCallc392f372010-06-11 00:33:02 +0000590void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
591 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000592 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000593 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000594 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000595 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000596 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
597 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
598 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000599 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
600 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000601}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000602void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
604}
John McCall17001972009-10-18 01:05:36 +0000605void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000607}
608void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
609 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000610 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
611 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
612 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
613 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000614}
John McCallfc93cf92009-10-22 22:37:11 +0000615void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000617}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000618void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
619 Writer.AddSourceLocation(TL.getKWLoc(), Record);
620 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
621 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
622}
John McCall8f115c62009-10-16 21:56:05 +0000623
Chris Lattner19cea4e2009-04-22 05:57:30 +0000624//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000625// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000626//===----------------------------------------------------------------------===//
627
Chris Lattner28fa4e62009-04-26 22:26:21 +0000628static void EmitBlockID(unsigned ID, const char *Name,
629 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000630 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000631 Record.clear();
632 Record.push_back(ID);
633 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
634
635 // Emit the block name if present.
636 if (Name == 0 || Name[0] == 0) return;
637 Record.clear();
638 while (*Name)
639 Record.push_back(*Name++);
640 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
641}
642
643static void EmitRecordID(unsigned ID, const char *Name,
644 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000645 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000646 Record.clear();
647 Record.push_back(ID);
648 while (*Name)
649 Record.push_back(*Name++);
650 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000651}
652
653static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000654 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000655#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000656 RECORD(STMT_STOP);
657 RECORD(STMT_NULL_PTR);
658 RECORD(STMT_NULL);
659 RECORD(STMT_COMPOUND);
660 RECORD(STMT_CASE);
661 RECORD(STMT_DEFAULT);
662 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000663 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000664 RECORD(STMT_IF);
665 RECORD(STMT_SWITCH);
666 RECORD(STMT_WHILE);
667 RECORD(STMT_DO);
668 RECORD(STMT_FOR);
669 RECORD(STMT_GOTO);
670 RECORD(STMT_INDIRECT_GOTO);
671 RECORD(STMT_CONTINUE);
672 RECORD(STMT_BREAK);
673 RECORD(STMT_RETURN);
674 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000675 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000676 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000677 RECORD(EXPR_PREDEFINED);
678 RECORD(EXPR_DECL_REF);
679 RECORD(EXPR_INTEGER_LITERAL);
680 RECORD(EXPR_FLOATING_LITERAL);
681 RECORD(EXPR_IMAGINARY_LITERAL);
682 RECORD(EXPR_STRING_LITERAL);
683 RECORD(EXPR_CHARACTER_LITERAL);
684 RECORD(EXPR_PAREN);
685 RECORD(EXPR_UNARY_OPERATOR);
686 RECORD(EXPR_SIZEOF_ALIGN_OF);
687 RECORD(EXPR_ARRAY_SUBSCRIPT);
688 RECORD(EXPR_CALL);
689 RECORD(EXPR_MEMBER);
690 RECORD(EXPR_BINARY_OPERATOR);
691 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
692 RECORD(EXPR_CONDITIONAL_OPERATOR);
693 RECORD(EXPR_IMPLICIT_CAST);
694 RECORD(EXPR_CSTYLE_CAST);
695 RECORD(EXPR_COMPOUND_LITERAL);
696 RECORD(EXPR_EXT_VECTOR_ELEMENT);
697 RECORD(EXPR_INIT_LIST);
698 RECORD(EXPR_DESIGNATED_INIT);
699 RECORD(EXPR_IMPLICIT_VALUE_INIT);
700 RECORD(EXPR_VA_ARG);
701 RECORD(EXPR_ADDR_LABEL);
702 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000703 RECORD(EXPR_CHOOSE);
704 RECORD(EXPR_GNU_NULL);
705 RECORD(EXPR_SHUFFLE_VECTOR);
706 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000707 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000708 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000709 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000710 RECORD(EXPR_OBJC_ARRAY_LITERAL);
711 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000712 RECORD(EXPR_OBJC_ENCODE);
713 RECORD(EXPR_OBJC_SELECTOR_EXPR);
714 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
715 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
716 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
717 RECORD(EXPR_OBJC_KVC_REF_EXPR);
718 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000719 RECORD(STMT_OBJC_FOR_COLLECTION);
720 RECORD(STMT_OBJC_CATCH);
721 RECORD(STMT_OBJC_FINALLY);
722 RECORD(STMT_OBJC_AT_TRY);
723 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
724 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000725 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000726 RECORD(EXPR_CXX_OPERATOR_CALL);
727 RECORD(EXPR_CXX_CONSTRUCT);
728 RECORD(EXPR_CXX_STATIC_CAST);
729 RECORD(EXPR_CXX_DYNAMIC_CAST);
730 RECORD(EXPR_CXX_REINTERPRET_CAST);
731 RECORD(EXPR_CXX_CONST_CAST);
732 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000733 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000734 RECORD(EXPR_CXX_BOOL_LITERAL);
735 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000736 RECORD(EXPR_CXX_TYPEID_EXPR);
737 RECORD(EXPR_CXX_TYPEID_TYPE);
738 RECORD(EXPR_CXX_UUIDOF_EXPR);
739 RECORD(EXPR_CXX_UUIDOF_TYPE);
740 RECORD(EXPR_CXX_THIS);
741 RECORD(EXPR_CXX_THROW);
742 RECORD(EXPR_CXX_DEFAULT_ARG);
743 RECORD(EXPR_CXX_BIND_TEMPORARY);
744 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
745 RECORD(EXPR_CXX_NEW);
746 RECORD(EXPR_CXX_DELETE);
747 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
748 RECORD(EXPR_EXPR_WITH_CLEANUPS);
749 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
750 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
751 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
752 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
753 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
754 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
755 RECORD(EXPR_CXX_NOEXCEPT);
756 RECORD(EXPR_OPAQUE_VALUE);
757 RECORD(EXPR_BINARY_TYPE_TRAIT);
758 RECORD(EXPR_PACK_EXPANSION);
759 RECORD(EXPR_SIZEOF_PACK);
760 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000761 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000762#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000763}
Mike Stump11289f42009-09-09 15:08:12 +0000764
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000765void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000766 RecordData Record;
767 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000768
Sebastian Redl539c5062010-08-18 23:57:32 +0000769#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
770#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000771
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000772 // Control Block.
773 BLOCK(CONTROL_BLOCK);
774 RECORD(METADATA);
775 RECORD(IMPORTS);
776 RECORD(LANGUAGE_OPTIONS);
777 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000778 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000779 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000780 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000781 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000782 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000783 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000784 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000785 RECORD(PREPROCESSOR_OPTIONS);
786
Douglas Gregor108cb222012-10-19 00:45:00 +0000787 BLOCK(INPUT_FILES_BLOCK);
788 RECORD(INPUT_FILE);
789
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000790 // AST Top-Level Block.
791 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000792 RECORD(TYPE_OFFSET);
793 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000794 RECORD(IDENTIFIER_OFFSET);
795 RECORD(IDENTIFIER_TABLE);
796 RECORD(EXTERNAL_DEFINITIONS);
797 RECORD(SPECIAL_TYPES);
798 RECORD(STATISTICS);
799 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000800 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000801 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
802 RECORD(SELECTOR_OFFSETS);
803 RECORD(METHOD_POOL);
804 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000805 RECORD(SOURCE_LOCATION_OFFSETS);
806 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000807 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000808 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000809 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000810 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000811 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000812 RECORD(SEMA_DECL_REFS);
813 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
814 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
815 RECORD(DECL_REPLACEMENTS);
816 RECORD(UPDATE_VISIBLE);
817 RECORD(DECL_UPDATE_OFFSETS);
818 RECORD(DECL_UPDATES);
819 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
820 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000821 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000822 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000823 RECORD(FP_PRAGMA_OPTIONS);
824 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000825 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000826 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000827 RECORD(MODULE_OFFSET_MAP);
828 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000829 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000830 RECORD(FILE_SORTED_DECLS);
831 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000832 RECORD(MERGED_DECLARATIONS);
833 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000834 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000835 RECORD(MACRO_OFFSET);
836 RECORD(MACRO_UPDATES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000837
Chris Lattner28fa4e62009-04-26 22:26:21 +0000838 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000839 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000840 RECORD(SM_SLOC_FILE_ENTRY);
841 RECORD(SM_SLOC_BUFFER_ENTRY);
842 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000843 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner28fa4e62009-04-26 22:26:21 +0000845 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000846 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000847 RECORD(PP_MACRO_OBJECT_LIKE);
848 RECORD(PP_MACRO_FUNCTION_LIKE);
849 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000850
Douglas Gregor12bfa382009-10-17 00:13:19 +0000851 // Decls and Types block.
852 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000853 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000854 RECORD(TYPE_COMPLEX);
855 RECORD(TYPE_POINTER);
856 RECORD(TYPE_BLOCK_POINTER);
857 RECORD(TYPE_LVALUE_REFERENCE);
858 RECORD(TYPE_RVALUE_REFERENCE);
859 RECORD(TYPE_MEMBER_POINTER);
860 RECORD(TYPE_CONSTANT_ARRAY);
861 RECORD(TYPE_INCOMPLETE_ARRAY);
862 RECORD(TYPE_VARIABLE_ARRAY);
863 RECORD(TYPE_VECTOR);
864 RECORD(TYPE_EXT_VECTOR);
865 RECORD(TYPE_FUNCTION_PROTO);
866 RECORD(TYPE_FUNCTION_NO_PROTO);
867 RECORD(TYPE_TYPEDEF);
868 RECORD(TYPE_TYPEOF_EXPR);
869 RECORD(TYPE_TYPEOF);
870 RECORD(TYPE_RECORD);
871 RECORD(TYPE_ENUM);
872 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000873 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000874 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000875 RECORD(TYPE_DECLTYPE);
876 RECORD(TYPE_ELABORATED);
877 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
878 RECORD(TYPE_UNRESOLVED_USING);
879 RECORD(TYPE_INJECTED_CLASS_NAME);
880 RECORD(TYPE_OBJC_OBJECT);
881 RECORD(TYPE_TEMPLATE_TYPE_PARM);
882 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
883 RECORD(TYPE_DEPENDENT_NAME);
884 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
885 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
886 RECORD(TYPE_PAREN);
887 RECORD(TYPE_PACK_EXPANSION);
888 RECORD(TYPE_ATTRIBUTED);
889 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000890 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000891 RECORD(DECL_TYPEDEF);
892 RECORD(DECL_ENUM);
893 RECORD(DECL_RECORD);
894 RECORD(DECL_ENUM_CONSTANT);
895 RECORD(DECL_FUNCTION);
896 RECORD(DECL_OBJC_METHOD);
897 RECORD(DECL_OBJC_INTERFACE);
898 RECORD(DECL_OBJC_PROTOCOL);
899 RECORD(DECL_OBJC_IVAR);
900 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000901 RECORD(DECL_OBJC_CATEGORY);
902 RECORD(DECL_OBJC_CATEGORY_IMPL);
903 RECORD(DECL_OBJC_IMPLEMENTATION);
904 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
905 RECORD(DECL_OBJC_PROPERTY);
906 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000907 RECORD(DECL_FIELD);
908 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000909 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000910 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000911 RECORD(DECL_FILE_SCOPE_ASM);
912 RECORD(DECL_BLOCK);
913 RECORD(DECL_CONTEXT_LEXICAL);
914 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000915 RECORD(DECL_NAMESPACE);
916 RECORD(DECL_NAMESPACE_ALIAS);
917 RECORD(DECL_USING);
918 RECORD(DECL_USING_SHADOW);
919 RECORD(DECL_USING_DIRECTIVE);
920 RECORD(DECL_UNRESOLVED_USING_VALUE);
921 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
922 RECORD(DECL_LINKAGE_SPEC);
923 RECORD(DECL_CXX_RECORD);
924 RECORD(DECL_CXX_METHOD);
925 RECORD(DECL_CXX_CONSTRUCTOR);
926 RECORD(DECL_CXX_DESTRUCTOR);
927 RECORD(DECL_CXX_CONVERSION);
928 RECORD(DECL_ACCESS_SPEC);
929 RECORD(DECL_FRIEND);
930 RECORD(DECL_FRIEND_TEMPLATE);
931 RECORD(DECL_CLASS_TEMPLATE);
932 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
933 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
934 RECORD(DECL_FUNCTION_TEMPLATE);
935 RECORD(DECL_TEMPLATE_TYPE_PARM);
936 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
937 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
938 RECORD(DECL_STATIC_ASSERT);
939 RECORD(DECL_CXX_BASE_SPECIFIERS);
940 RECORD(DECL_INDIRECTFIELD);
941 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
942
Douglas Gregor03412ba2011-06-03 02:27:19 +0000943 // Statements and Exprs can occur in the Decls and Types block.
944 AddStmtsExprs(Stream, Record);
945
Douglas Gregor92a96f52011-02-08 21:58:10 +0000946 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000947 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000948 RECORD(PPD_MACRO_DEFINITION);
949 RECORD(PPD_INCLUSION_DIRECTIVE);
950
Chris Lattner28fa4e62009-04-26 22:26:21 +0000951#undef RECORD
952#undef BLOCK
953 Stream.ExitBlock();
954}
955
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000956/// \brief Adjusts the given filename to only write out the portion of the
957/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000958///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000959/// \param Filename the file name to adjust.
960///
961/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
962/// the returned filename will be adjusted by this system root.
963///
964/// \returns either the original filename (if it needs no adjustment) or the
965/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000966static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000967adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000968 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000969
Douglas Gregorc567ba22011-07-22 16:35:34 +0000970 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000971 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000972
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000973 // Verify that the filename and the system root have the same prefix.
974 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000975 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000976 if (Filename[Pos] != isysroot[Pos])
977 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000978
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000979 // We hit the end of the filename before we hit the end of the system root.
980 if (!Filename[Pos])
981 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000982
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000983 // If the file name has a '/' at the current position, skip over the '/'.
984 // We distinguish sysroot-based includes from absolute includes by the
985 // absence of '/' at the beginning of sysroot-based includes.
986 if (Filename[Pos] == '/')
987 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000988
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000989 return Filename + Pos;
990}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000991
Douglas Gregor112b9072012-10-18 05:31:06 +0000992/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +0000993void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
994 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +0000995 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000996 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000997 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
998 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +0000999
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001000 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001001 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1002 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1003 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1010 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1011 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001012 Record.push_back(VERSION_MAJOR);
1013 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001014 Record.push_back(CLANG_VERSION_MAJOR);
1015 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001016 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001017 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001018 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1019 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001020
Douglas Gregor112b9072012-10-18 05:31:06 +00001021 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001022 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001023 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1024 llvm::SmallVector<char, 128> ModulePaths;
1025 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001026
1027 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1028 M != MEnd; ++M) {
1029 // Skip modules that weren't directly imported.
1030 if (!(*M)->isDirectlyImported())
1031 continue;
1032
1033 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001034 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001035 // FIXME: This writes the absolute path for AST files we depend on.
1036 const std::string &FileName = (*M)->FileName;
1037 Record.push_back(FileName.size());
1038 Record.append(FileName.begin(), FileName.end());
1039 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001040 Stream.EmitRecord(IMPORTS, Record);
1041 }
Mike Stump11289f42009-09-09 15:08:12 +00001042
Douglas Gregor112b9072012-10-18 05:31:06 +00001043 // Language options.
1044 Record.clear();
1045 const LangOptions &LangOpts = Context.getLangOpts();
1046#define LANGOPT(Name, Bits, Default, Description) \
1047 Record.push_back(LangOpts.Name);
1048#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1049 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1050#include "clang/Basic/LangOptions.def"
1051
1052 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1053 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1054
1055 Record.push_back(LangOpts.CurrentModule.size());
1056 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1057 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1058
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001059 // Target options.
1060 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001061 const TargetInfo &Target = Context.getTargetInfo();
1062 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001063 AddString(TargetOpts.Triple, Record);
1064 AddString(TargetOpts.CPU, Record);
1065 AddString(TargetOpts.ABI, Record);
1066 AddString(TargetOpts.CXXABI, Record);
1067 AddString(TargetOpts.LinkerVersion, Record);
1068 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1069 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1070 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1071 }
1072 Record.push_back(TargetOpts.Features.size());
1073 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1074 AddString(TargetOpts.Features[I], Record);
1075 }
1076 Stream.EmitRecord(TARGET_OPTIONS, Record);
1077
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001078 // Diagnostic options.
1079 Record.clear();
1080 const DiagnosticOptions &DiagOpts
1081 = Context.getDiagnostics().getDiagnosticOptions();
1082#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1083#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1084 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1085#include "clang/Basic/DiagnosticOptions.def"
1086 Record.push_back(DiagOpts.Warnings.size());
1087 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1088 AddString(DiagOpts.Warnings[I], Record);
1089 // Note: we don't serialize the log or serialization file names, because they
1090 // are generally transient files and will almost always be overridden.
1091 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1092
Douglas Gregorc6317db2012-10-24 15:49:58 +00001093 // File system options.
1094 Record.clear();
1095 const FileSystemOptions &FSOpts
1096 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1097 AddString(FSOpts.WorkingDir, Record);
1098 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1099
Douglas Gregor2d302362012-10-24 16:50:34 +00001100 // Header search options.
1101 Record.clear();
1102 const HeaderSearchOptions &HSOpts
1103 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1104 AddString(HSOpts.Sysroot, Record);
1105
1106 // Include entries.
1107 Record.push_back(HSOpts.UserEntries.size());
1108 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1109 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1110 AddString(Entry.Path, Record);
1111 Record.push_back(static_cast<unsigned>(Entry.Group));
1112 Record.push_back(Entry.IsUserSupplied);
1113 Record.push_back(Entry.IsFramework);
1114 Record.push_back(Entry.IgnoreSysRoot);
1115 Record.push_back(Entry.IsInternal);
1116 Record.push_back(Entry.ImplicitExternC);
1117 }
1118
1119 // System header prefixes.
1120 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1121 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1122 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1123 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1124 }
1125
1126 AddString(HSOpts.ResourceDir, Record);
1127 AddString(HSOpts.ModuleCachePath, Record);
1128 Record.push_back(HSOpts.DisableModuleHash);
1129 Record.push_back(HSOpts.UseBuiltinIncludes);
1130 Record.push_back(HSOpts.UseStandardSystemIncludes);
1131 Record.push_back(HSOpts.UseStandardCXXIncludes);
1132 Record.push_back(HSOpts.UseLibcxx);
1133 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1134
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001135 // Preprocessor options.
1136 Record.clear();
1137 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1138
1139 // Macro definitions.
1140 Record.push_back(PPOpts.Macros.size());
1141 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1142 AddString(PPOpts.Macros[I].first, Record);
1143 Record.push_back(PPOpts.Macros[I].second);
1144 }
1145
1146 // Includes
1147 Record.push_back(PPOpts.Includes.size());
1148 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1149 AddString(PPOpts.Includes[I], Record);
1150
1151 // Macro includes
1152 Record.push_back(PPOpts.MacroIncludes.size());
1153 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1154 AddString(PPOpts.MacroIncludes[I], Record);
1155
Douglas Gregorb6368752012-10-24 23:41:50 +00001156 Record.push_back(PPOpts.UsePredefines);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001157 AddString(PPOpts.ImplicitPCHInclude, Record);
1158 AddString(PPOpts.ImplicitPTHInclude, Record);
1159 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1160 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1161
Douglas Gregora3b20262011-05-06 21:43:30 +00001162 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001163 SourceManager &SM = Context.getSourceManager();
1164 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1165 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001166 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1167 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001168 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1169 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1170
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001171 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001172
Michael J. Spencer740857f2010-12-21 16:45:57 +00001173 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001174
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001175 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001176 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001177 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001178 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001179 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001180 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001181 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001182 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001183
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001184 Record.clear();
1185 Record.push_back(SM.getMainFileID().getOpaqueValue());
1186 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1187
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001188 // Original PCH directory
1189 if (!OutputFile.empty() && OutputFile != "-") {
1190 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1191 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1193 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1194
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001195 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001196
1197 llvm::sys::fs::make_absolute(OutputPath);
1198 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1199
1200 RecordData Record;
1201 Record.push_back(ORIGINAL_PCH_DIR);
1202 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1203 }
1204
Douglas Gregor72be3902012-10-19 00:38:02 +00001205 WriteInputFiles(Context.SourceMgr, isysroot);
1206 Stream.ExitBlock();
1207}
1208
1209void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1210 using namespace llvm;
1211 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1212 RecordData Record;
1213
1214 // Create input-file abbreviation.
1215 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1216 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001217 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001218 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1219 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001220 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001221 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1222 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1223
1224 // Write out all of the input files.
1225 std::vector<uint32_t> InputFileOffsets;
1226 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1227 // Get this source location entry.
1228 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001229 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001230
1231 // We only care about file entries that were not overridden.
1232 if (!SLoc->isFile())
1233 continue;
1234 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001235 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001236 continue;
1237
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001238 uint32_t &InputFileID = InputFileIDs[Cache->OrigEntry];
1239 if (InputFileID != 0)
1240 continue; // already recorded this file.
1241
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001242 // Record this entry's offset.
1243 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001244
1245 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001246
Douglas Gregor72be3902012-10-19 00:38:02 +00001247 Record.clear();
1248 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001249 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001250
1251 // Emit size/modification time for this file.
1252 Record.push_back(Cache->OrigEntry->getSize());
1253 Record.push_back(Cache->OrigEntry->getModificationTime());
1254
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001255 // Whether this file was overridden.
1256 Record.push_back(Cache->BufferOverridden);
1257
Douglas Gregor72be3902012-10-19 00:38:02 +00001258 // Turn the file name into an absolute path, if it isn't already.
1259 const char *Filename = Cache->OrigEntry->getName();
1260 SmallString<128> FilePath(Filename);
1261
1262 // Ask the file manager to fixup the relative path for us. This will
1263 // honor the working directory.
1264 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1265
1266 // FIXME: This call to make_absolute shouldn't be necessary, the
1267 // call to FixupRelativePath should always return an absolute path.
1268 llvm::sys::fs::make_absolute(FilePath);
1269 Filename = FilePath.c_str();
1270
1271 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1272
1273 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1274 }
1275
Douglas Gregor112b9072012-10-18 05:31:06 +00001276 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001277
1278 // Create input file offsets abbreviation.
1279 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1280 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1281 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1282 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1283 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1284
1285 // Write input file offsets.
1286 Record.clear();
1287 Record.push_back(INPUT_FILE_OFFSETS);
1288 Record.push_back(InputFileOffsets.size());
1289 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001290}
1291
Douglas Gregora7f71a92009-04-10 03:52:48 +00001292//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001293// stat cache Serialization
1294//===----------------------------------------------------------------------===//
1295
1296namespace {
1297// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001298class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001299public:
1300 typedef const char * key_type;
1301 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001302
Chris Lattner2a6fa472010-11-23 19:28:12 +00001303 typedef struct stat data_type;
1304 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001305
1306 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001307 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001308 }
Mike Stump11289f42009-09-09 15:08:12 +00001309
1310 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001311 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001312 data_type_ref Data) {
1313 unsigned StrLen = strlen(path);
1314 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001315 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001316 clang::io::Emit8(Out, DataLen);
1317 return std::make_pair(StrLen + 1, DataLen);
1318 }
Mike Stump11289f42009-09-09 15:08:12 +00001319
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001320 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001321 Out.write(path, KeyLen);
1322 }
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001324 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001325 data_type_ref Data, unsigned DataLen) {
1326 using namespace clang::io;
1327 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001328
Chris Lattner2a6fa472010-11-23 19:28:12 +00001329 Emit32(Out, (uint32_t) Data.st_ino);
1330 Emit32(Out, (uint32_t) Data.st_dev);
1331 Emit16(Out, (uint16_t) Data.st_mode);
1332 Emit64(Out, (uint64_t) Data.st_mtime);
1333 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001334
1335 assert(Out.tell() - Start == DataLen && "Wrong data length");
1336 }
1337};
1338} // end anonymous namespace
1339
Douglas Gregorc5046832009-04-27 18:38:38 +00001340//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001341// Source Manager Serialization
1342//===----------------------------------------------------------------------===//
1343
1344/// \brief Create an abbreviation for the SLocEntry that refers to a
1345/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001346static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001347 using namespace llvm;
1348 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001349 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001354 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001359 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001360}
1361
1362/// \brief Create an abbreviation for the SLocEntry that refers to a
1363/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001364static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001365 using namespace llvm;
1366 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001367 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001368 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001373 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001374}
1375
1376/// \brief Create an abbreviation for the SLocEntry that refers to a
1377/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001378static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001379 using namespace llvm;
1380 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001381 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001383 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001384}
1385
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001386/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1387/// expansion.
1388static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001389 using namespace llvm;
1390 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001391 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001397 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001398}
1399
Douglas Gregor09b69892011-02-10 17:09:37 +00001400namespace {
1401 // Trait used for the on-disk hash table of header search information.
1402 class HeaderFileInfoTrait {
1403 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001404
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001405 // Keep track of the framework names we've used during serialization.
1406 SmallVector<char, 128> FrameworkStringData;
1407 llvm::StringMap<unsigned> FrameworkNameOffset;
1408
Douglas Gregor09b69892011-02-10 17:09:37 +00001409 public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001410 HeaderFileInfoTrait(ASTWriter &Writer)
1411 : Writer(Writer) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001412
1413 typedef const char *key_type;
1414 typedef key_type key_type_ref;
1415
1416 typedef HeaderFileInfo data_type;
1417 typedef const data_type &data_type_ref;
1418
1419 static unsigned ComputeHash(const char *path) {
1420 // The hash is based only on the filename portion of the key, so that the
1421 // reader can match based on filenames when symlinking or excess path
1422 // elements ("foo/../", "../") change the form of the name. However,
1423 // complete path is still the key.
1424 return llvm::HashString(llvm::sys::path::filename(path));
1425 }
1426
1427 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001428 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001429 data_type_ref Data) {
1430 unsigned StrLen = strlen(path);
1431 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001432 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001433 clang::io::Emit8(Out, DataLen);
1434 return std::make_pair(StrLen + 1, DataLen);
1435 }
1436
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001437 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001438 Out.write(path, KeyLen);
1439 }
1440
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001441 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001442 data_type_ref Data, unsigned DataLen) {
1443 using namespace clang::io;
1444 uint64_t Start = Out.tell(); (void)Start;
1445
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001446 unsigned char Flags = (Data.isImport << 5)
1447 | (Data.isPragmaOnce << 4)
1448 | (Data.DirInfo << 2)
1449 | (Data.Resolved << 1)
1450 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001451 Emit8(Out, (uint8_t)Flags);
1452 Emit16(Out, (uint16_t) Data.NumIncludes);
1453
1454 if (!Data.ControllingMacro)
1455 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1456 else
1457 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001458
1459 unsigned Offset = 0;
1460 if (!Data.Framework.empty()) {
1461 // If this header refers into a framework, save the framework name.
1462 llvm::StringMap<unsigned>::iterator Pos
1463 = FrameworkNameOffset.find(Data.Framework);
1464 if (Pos == FrameworkNameOffset.end()) {
1465 Offset = FrameworkStringData.size() + 1;
1466 FrameworkStringData.append(Data.Framework.begin(),
1467 Data.Framework.end());
1468 FrameworkStringData.push_back(0);
1469
1470 FrameworkNameOffset[Data.Framework] = Offset;
1471 } else
1472 Offset = Pos->second;
1473 }
1474 Emit32(Out, Offset);
1475
Douglas Gregor09b69892011-02-10 17:09:37 +00001476 assert(Out.tell() - Start == DataLen && "Wrong data length");
1477 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001478
1479 const char *strings_begin() const { return FrameworkStringData.begin(); }
1480 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001481 };
1482} // end anonymous namespace
1483
1484/// \brief Write the header search block for the list of files that
1485///
1486/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001487void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001488 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001489 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1490
1491 if (FilesByUID.size() > HS.header_file_size())
1492 FilesByUID.resize(HS.header_file_size());
1493
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001494 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregor09b69892011-02-10 17:09:37 +00001495 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001496 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001497 unsigned NumHeaderSearchEntries = 0;
1498 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1499 const FileEntry *File = FilesByUID[UID];
1500 if (!File)
1501 continue;
1502
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001503 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1504 // from the external source if it was not provided already.
1505 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001506 if (HFI.External && Chain)
1507 continue;
1508
1509 // Turn the file name into an absolute path, if it isn't already.
1510 const char *Filename = File->getName();
1511 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1512
1513 // If we performed any translation on the file name at all, we need to
1514 // save this string, since the generator will refer to it later.
1515 if (Filename != File->getName()) {
1516 Filename = strdup(Filename);
1517 SavedStrings.push_back(Filename);
1518 }
1519
1520 Generator.insert(Filename, HFI, GeneratorTrait);
1521 ++NumHeaderSearchEntries;
1522 }
1523
1524 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001525 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001526 uint32_t BucketOffset;
1527 {
1528 llvm::raw_svector_ostream Out(TableData);
1529 // Make sure that no bucket is at offset 0
1530 clang::io::Emit32(Out, 0);
1531 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1532 }
1533
1534 // Create a blob abbreviation
1535 using namespace llvm;
1536 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1537 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1542 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1543
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001544 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001545 RecordData Record;
1546 Record.push_back(HEADER_SEARCH_TABLE);
1547 Record.push_back(BucketOffset);
1548 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001549 Record.push_back(TableData.size());
1550 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001551 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1552
1553 // Free all of the strings we had to duplicate.
1554 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1555 free((void*)SavedStrings[I]);
1556}
1557
Douglas Gregora7f71a92009-04-10 03:52:48 +00001558/// \brief Writes the block containing the serialized form of the
1559/// source manager.
1560///
1561/// TODO: We should probably use an on-disk hash table (stored in a
1562/// blob), indexed based on the file name, so that we only create
1563/// entries for files that we actually need. In the common case (no
1564/// errors), we probably won't have to create file entries for any of
1565/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001566void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001567 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001568 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001569 RecordData Record;
1570
Chris Lattner0910e3b2009-04-10 17:16:57 +00001571 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001572 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001573
1574 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001575 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1576 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1577 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001578 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001579
Douglas Gregor258ae542009-04-27 06:38:32 +00001580 // Write out the source location entry table. We skip the first
1581 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001582 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001583 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001584 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1585 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001586 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001587 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001588 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001589 FileID FID = FileID::get(I);
1590 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001591
Douglas Gregor258ae542009-04-27 06:38:32 +00001592 // Record the offset of this source-location entry.
1593 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1594
1595 // Figure out which record code to use.
1596 unsigned Code;
1597 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001598 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1599 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001600 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001601 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001602 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001603 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001604 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001605 Record.clear();
1606 Record.push_back(Code);
1607
Douglas Gregor925296b2011-07-19 16:10:42 +00001608 // Starting offset of this entry within this module, so skip the dummy.
1609 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001610 if (SLoc->isFile()) {
1611 const SrcMgr::FileInfo &File = SLoc->getFile();
1612 Record.push_back(File.getIncludeLoc().getRawEncoding());
1613 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1614 Record.push_back(File.hasLineDirectives());
1615
1616 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001617 if (Content->OrigEntry) {
1618 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001619 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001620
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001621 // The source location entry is a file. Emit input file ID.
1622 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1623 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001624
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001625 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001626
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001627 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001628 if (FDI != FileDeclIDs.end()) {
1629 Record.push_back(FDI->second->FirstDeclIndex);
1630 Record.push_back(FDI->second->DeclIDs.size());
1631 } else {
1632 Record.push_back(0);
1633 Record.push_back(0);
1634 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001635
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001636 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001637
1638 if (Content->BufferOverridden) {
1639 Record.clear();
1640 Record.push_back(SM_SLOC_BUFFER_BLOB);
1641 const llvm::MemoryBuffer *Buffer
1642 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1643 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1644 StringRef(Buffer->getBufferStart(),
1645 Buffer->getBufferSize() + 1));
1646 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001647 } else {
1648 // The source location entry is a buffer. The blob associated
1649 // with this entry contains the contents of the buffer.
1650
1651 // We add one to the size so that we capture the trailing NULL
1652 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1653 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001654 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001655 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001656 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001657 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001658 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001659 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001660 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001661 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001662 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001663 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001664
Douglas Gregor925296b2011-07-19 16:10:42 +00001665 if (strcmp(Name, "<built-in>") == 0) {
1666 PreloadSLocs.push_back(SLocEntryOffsets.size());
1667 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001668 }
1669 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001670 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001671 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001672 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1673 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001674 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1675 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001676
1677 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001678 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001679 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001680 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001681 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001682 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001683 }
1684 }
1685
Douglas Gregor8f45df52009-04-16 22:23:12 +00001686 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001687
1688 if (SLocEntryOffsets.empty())
1689 return;
1690
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001691 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001692 // table is used for lazily loading source-location information.
1693 using namespace llvm;
1694 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001695 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001696 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001697 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001698 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1699 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001700
Douglas Gregor258ae542009-04-27 06:38:32 +00001701 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001702 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001703 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001704 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001705 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001706
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001707 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001708 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001709 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001710
1711 // Write the line table. It depends on remapping working, so it must come
1712 // after the source location offsets.
1713 if (SourceMgr.hasLineTable()) {
1714 LineTableInfo &LineTable = SourceMgr.getLineTable();
1715
1716 Record.clear();
1717 // Emit the file names
1718 Record.push_back(LineTable.getNumFilenames());
1719 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1720 // Emit the file name
1721 const char *Filename = LineTable.getFilename(I);
1722 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1723 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1724 Record.push_back(FilenameLen);
1725 if (FilenameLen)
1726 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1727 }
1728
1729 // Emit the line entries
1730 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1731 L != LEnd; ++L) {
1732 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001733 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001734 continue;
1735
1736 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001737 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001738
1739 // Emit the line entries
1740 Record.push_back(L->second.size());
1741 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1742 LEEnd = L->second.end();
1743 LE != LEEnd; ++LE) {
1744 Record.push_back(LE->FileOffset);
1745 Record.push_back(LE->LineNo);
1746 Record.push_back(LE->FilenameID);
1747 Record.push_back((unsigned)LE->FileKind);
1748 Record.push_back(LE->IncludeOffset);
1749 }
1750 }
1751 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1752 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001753}
1754
Douglas Gregorc5046832009-04-27 18:38:38 +00001755//===----------------------------------------------------------------------===//
1756// Preprocessor Serialization
1757//===----------------------------------------------------------------------===//
1758
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001759static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1760 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1761 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1762 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1763 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1764 return X.first->getName().compare(Y.first->getName());
1765}
1766
Chris Lattnereeffaef2009-04-10 17:15:23 +00001767/// \brief Writes the block containing the serialized form of the
1768/// preprocessor.
1769///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001770void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001771 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1772 if (PPRec)
1773 WritePreprocessorDetail(*PPRec);
1774
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001775 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001776
Chris Lattner0af3ba12009-04-13 01:29:17 +00001777 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1778 if (PP.getCounterValue() != 0) {
1779 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001780 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001781 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001782 }
1783
1784 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001785 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001786
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001787 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001788 // FIXME: use diagnostics subsystem for localization etc.
1789 if (PP.SawDateOrTime())
1790 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001791
Douglas Gregor796d76a2010-10-20 22:00:55 +00001792
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001793 // Loop over all the macro definitions that are live at the end of the file,
1794 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001795
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001796 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001797 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001798 MacrosToEmit;
1799 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001800 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor68051a72011-02-11 00:26:14 +00001801 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001802 I != E; ++I) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001803 if (!IsModule || I->second->isPublic()) {
1804 MacroDefinitionsSeen.insert(I->first);
1805 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001806 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001807 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001808
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001809 // Sort the set of macro definitions that need to be serialized by the
1810 // name of the macro, to provide a stable ordering.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001811 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001812 &compareMacroDefinitions);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001813
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001814 /// \brief Offsets of each of the macros into the bitstream, indexed by
1815 /// the local macro ID
1816 ///
1817 /// For each identifier that is associated with a macro, this map
1818 /// provides the offset into the bitstream where that macro is
1819 /// defined.
1820 std::vector<uint32_t> MacroOffsets;
1821
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001822 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1823 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001824
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001825 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1826 MI = MI->getPreviousDefinition()) {
1827 MacroID ID = getMacroRef(MI);
1828 if (!ID)
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001829 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001830
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001831 // Skip macros from a AST file if we're chaining.
1832 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1833 continue;
1834
1835 if (ID < FirstMacroID) {
1836 // This will have been dealt with via an update record.
1837 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1838 continue;
1839 }
1840
1841 // Record the local offset of this macro.
1842 unsigned Index = ID - FirstMacroID;
1843 if (Index == MacroOffsets.size())
1844 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1845 else {
1846 if (Index > MacroOffsets.size())
1847 MacroOffsets.resize(Index + 1);
1848
1849 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1850 }
1851
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001852 AddIdentifierRef(Name, Record);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001853 addMacroRef(MI, Record);
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001854 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001855 AddSourceLocation(MI->getDefinitionLoc(), Record);
1856 AddSourceLocation(MI->getUndefLoc(), Record);
1857 Record.push_back(MI->isUsed());
1858 Record.push_back(MI->isPublic());
1859 AddSourceLocation(MI->getVisibilityLocation(), Record);
1860 unsigned Code;
1861 if (MI->isObjectLike()) {
1862 Code = PP_MACRO_OBJECT_LIKE;
1863 } else {
1864 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001865
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001866 Record.push_back(MI->isC99Varargs());
1867 Record.push_back(MI->isGNUVarargs());
Eli Friedman14d3c792012-11-14 02:18:46 +00001868 Record.push_back(MI->hasCommaPasting());
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001869 Record.push_back(MI->getNumArgs());
1870 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1871 I != E; ++I)
1872 AddIdentifierRef(*I, Record);
1873 }
Mike Stump11289f42009-09-09 15:08:12 +00001874
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001875 // If we have a detailed preprocessing record, record the macro definition
1876 // ID that corresponds to this macro.
1877 if (PPRec)
1878 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1879
1880 Stream.EmitRecord(Code, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001881 Record.clear();
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001882
1883 // Emit the tokens array.
1884 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1885 // Note that we know that the preprocessor does not have any annotation
1886 // tokens in it because they are created by the parser, and thus can't
1887 // be in a macro definition.
1888 const Token &Tok = MI->getReplacementToken(TokNo);
1889
1890 Record.push_back(Tok.getLocation().getRawEncoding());
1891 Record.push_back(Tok.getLength());
1892
1893 // FIXME: When reading literal tokens, reconstruct the literal pointer
1894 // if it is needed.
1895 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1896 // FIXME: Should translate token kind to a stable encoding.
1897 Record.push_back(Tok.getKind());
1898 // FIXME: Should translate token flags to a stable encoding.
1899 Record.push_back(Tok.getFlags());
1900
1901 Stream.EmitRecord(PP_TOKEN, Record);
1902 Record.clear();
1903 }
1904 ++NumMacros;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001905 }
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001906 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001907 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001908
1909 // Write the offsets table for macro IDs.
1910 using namespace llvm;
1911 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1912 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1916
1917 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1918 Record.clear();
1919 Record.push_back(MACRO_OFFSET);
1920 Record.push_back(MacroOffsets.size());
1921 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1922 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1923 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001924}
1925
1926void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001927 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001928 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001929
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001930 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001931
Douglas Gregor92a96f52011-02-08 21:58:10 +00001932 // Enter the preprocessor block.
1933 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001934
Douglas Gregoraae92242010-03-19 21:51:54 +00001935 // If the preprocessor has a preprocessing record, emit it.
1936 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001937 using namespace llvm;
1938
1939 // Set up the abbreviation for
1940 unsigned InclusionAbbrev = 0;
1941 {
1942 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1943 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00001948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1949 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1950 }
1951
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001952 unsigned FirstPreprocessorEntityID
1953 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1954 + NUM_PREDEF_PP_ENTITY_IDS;
1955 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001956 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001957 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1958 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001959 E != EEnd;
1960 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001961 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001962
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001963 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1964 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001965
Douglas Gregor92a96f52011-02-08 21:58:10 +00001966 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001967 // Record this macro definition's ID.
1968 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001969
Douglas Gregor92a96f52011-02-08 21:58:10 +00001970 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001971 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1972 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001973 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001974
Chandler Carrutha88a22182011-07-14 08:20:46 +00001975 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001976 Record.push_back(ME->isBuiltinMacro());
1977 if (ME->isBuiltinMacro())
1978 AddIdentifierRef(ME->getName(), Record);
1979 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001980 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001981 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001982 continue;
1983 }
1984
1985 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1986 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001987 Record.push_back(ID->getFileName().size());
1988 Record.push_back(ID->wasInQuotes());
1989 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001990 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001991 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001992 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00001993 // Check that the FileEntry is not null because it was not resolved and
1994 // we create a PCH even with compiler errors.
1995 if (ID->getFile())
1996 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001997 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1998 continue;
1999 }
2000
2001 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2002 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002003 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002004
Douglas Gregoraae92242010-03-19 21:51:54 +00002005 // Write the offsets table for the preprocessing record.
2006 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002007 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2008
Douglas Gregoraae92242010-03-19 21:51:54 +00002009 // Write the offsets table for identifier IDs.
2010 using namespace llvm;
2011 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002012 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002015 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002016
Douglas Gregoraae92242010-03-19 21:51:54 +00002017 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002018 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002019 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002020 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2021 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002022 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002023}
2024
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002025unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2026 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2027 if (Known != SubmoduleIDs.end())
2028 return Known->second;
2029
2030 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2031}
2032
Douglas Gregor253eefe2011-12-01 00:59:36 +00002033/// \brief Compute the number of modules within the given tree (including the
2034/// given module).
2035static unsigned getNumberOfModules(Module *Mod) {
2036 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002037 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2038 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002039 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002040 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002041
2042 return ChildModules + 1;
2043}
2044
Douglas Gregorde3ef502011-11-30 23:21:26 +00002045void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002046 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002047 // FIXME: This feels like it belongs somewhere else, but there are no
2048 // other consumers of this information.
2049 SourceManager &SrcMgr = PP->getSourceManager();
2050 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2051 for (ASTContext::import_iterator I = Context->local_import_begin(),
2052 IEnd = Context->local_import_end();
2053 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002054 if (Module *ImportedFrom
2055 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2056 SrcMgr))) {
2057 ImportedFrom->Imports.push_back(I->getImportedModule());
2058 }
2059 }
2060
Douglas Gregor69021972011-11-30 17:33:56 +00002061 // Enter the submodule description block.
2062 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2063
2064 // Write the abbreviations needed for the submodules block.
2065 using namespace llvm;
2066 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2067 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002069 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00002076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2077 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2078
2079 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002080 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2082 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2083
2084 Abbrev = new BitCodeAbbrev();
2085 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2087 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002088
2089 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002090 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2092 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2093
2094 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002095 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2097 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2098
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002099 Abbrev = new BitCodeAbbrev();
2100 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2102 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2103
Douglas Gregor59527662012-10-15 06:28:11 +00002104 Abbrev = new BitCodeAbbrev();
2105 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2107 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2108
Douglas Gregor253eefe2011-12-01 00:59:36 +00002109 // Write the submodule metadata block.
2110 RecordData Record;
2111 Record.push_back(getNumberOfModules(WritingModule));
2112 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2113 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2114
Douglas Gregor69021972011-11-30 17:33:56 +00002115 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002116 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002117 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002118 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002119 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002120 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002121 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002122
2123 // Emit the definition of the block.
2124 Record.clear();
2125 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002126 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002127 if (Mod->Parent) {
2128 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2129 Record.push_back(SubmoduleIDs[Mod->Parent]);
2130 } else {
2131 Record.push_back(0);
2132 }
2133 Record.push_back(Mod->IsFramework);
2134 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002135 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002136 Record.push_back(Mod->InferSubmodules);
2137 Record.push_back(Mod->InferExplicitSubmodules);
2138 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00002139 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2140
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002141 // Emit the requirements.
2142 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2143 Record.clear();
2144 Record.push_back(SUBMODULE_REQUIRES);
2145 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2146 Mod->Requires[I].data(),
2147 Mod->Requires[I].size());
2148 }
2149
Douglas Gregor69021972011-11-30 17:33:56 +00002150 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002151 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002152 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002153 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002154 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002155 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002156 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2157 Record.clear();
2158 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2159 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2160 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002161 }
2162
2163 // Emit the headers.
2164 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2165 Record.clear();
2166 Record.push_back(SUBMODULE_HEADER);
2167 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2168 Mod->Headers[I]->getName());
2169 }
Douglas Gregor59527662012-10-15 06:28:11 +00002170 // Emit the excluded headers.
2171 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2172 Record.clear();
2173 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2174 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2175 Mod->ExcludedHeaders[I]->getName());
2176 }
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002177 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2178 Record.clear();
2179 Record.push_back(SUBMODULE_TOPHEADER);
2180 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2181 Mod->TopHeaders[I]->getName());
2182 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002183
2184 // Emit the imports.
2185 if (!Mod->Imports.empty()) {
2186 Record.clear();
2187 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002188 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002189 assert(ImportedID && "Unknown submodule!");
2190 Record.push_back(ImportedID);
2191 }
2192 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2193 }
2194
Douglas Gregor24bb9232011-12-02 18:58:38 +00002195 // Emit the exports.
2196 if (!Mod->Exports.empty()) {
2197 Record.clear();
2198 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002199 if (Module *Exported = Mod->Exports[I].getPointer()) {
2200 unsigned ExportedID = SubmoduleIDs[Exported];
2201 assert(ExportedID > 0 && "Unknown submodule ID?");
2202 Record.push_back(ExportedID);
2203 } else {
2204 Record.push_back(0);
2205 }
2206
Douglas Gregor24bb9232011-12-02 18:58:38 +00002207 Record.push_back(Mod->Exports[I].getInt());
2208 }
2209 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2210 }
2211
Douglas Gregor69021972011-11-30 17:33:56 +00002212 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002213 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2214 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002215 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002216 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002217 }
2218
2219 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002220
2221 assert((NextSubmoduleID - FirstSubmoduleID
2222 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002223}
2224
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002225serialization::SubmoduleID
2226ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002227 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002228 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002229
2230 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002231 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002232 Module *OwningMod
2233 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002234 if (!OwningMod)
2235 return 0;
2236
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002237 // Check whether this submodule is part of our own module.
2238 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002239 return 0;
2240
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002241 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002242}
2243
David Blaikie9c902b52011-09-25 23:23:43 +00002244void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002245 // FIXME: Make it work properly with modules.
2246 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2247 DiagStateIDMap;
2248 unsigned CurrID = 0;
2249 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002250 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002251 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002252 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2253 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002254 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002255 if (point.Loc.isInvalid())
2256 continue;
2257
2258 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002259 unsigned &DiagStateID = DiagStateIDMap[point.State];
2260 Record.push_back(DiagStateID);
2261
2262 if (DiagStateID == 0) {
2263 DiagStateID = ++CurrID;
2264 for (DiagnosticsEngine::DiagState::const_iterator
2265 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2266 if (I->second.isPragma()) {
2267 Record.push_back(I->first);
2268 Record.push_back(I->second.getMapping());
2269 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002270 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002271 Record.push_back(-1); // mark the end of the diag/map pairs for this
2272 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002273 }
2274 }
2275
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002276 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002277 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002278}
2279
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002280void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2281 if (CXXBaseSpecifiersOffsets.empty())
2282 return;
2283
2284 RecordData Record;
2285
2286 // Create a blob abbreviation for the C++ base specifiers offsets.
2287 using namespace llvm;
2288
2289 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2290 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2293 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2294
Douglas Gregorc27b2872011-08-04 00:01:48 +00002295 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002296 Record.clear();
2297 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2298 Record.push_back(CXXBaseSpecifiersOffsets.size());
2299 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002300 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002301}
2302
Douglas Gregorc5046832009-04-27 18:38:38 +00002303//===----------------------------------------------------------------------===//
2304// Type Serialization
2305//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002306
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002307/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002308void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002309 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002310 if (Idx.getIndex() == 0) // we haven't seen this type before.
2311 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002312
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002313 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002314
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002315 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002316 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002317 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002318 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002319 else if (TypeOffsets.size() < Index) {
2320 TypeOffsets.resize(Index + 1);
2321 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002322 }
2323
2324 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002325
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002326 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002327 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002328
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002329 if (T.hasLocalNonFastQualifiers()) {
2330 Qualifiers Qs = T.getLocalQualifiers();
2331 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002332 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002333 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002334 } else {
2335 switch (T->getTypeClass()) {
2336 // For all of the concrete, non-dependent types, call the
2337 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002338#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002339 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002340#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002341#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002342 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002343 }
2344
2345 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002346 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002347
2348 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002349 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002350}
2351
Douglas Gregorc5046832009-04-27 18:38:38 +00002352//===----------------------------------------------------------------------===//
2353// Declaration Serialization
2354//===----------------------------------------------------------------------===//
2355
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002356/// \brief Write the block containing all of the declaration IDs
2357/// lexically declared within the given DeclContext.
2358///
2359/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2360/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002361uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002362 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002363 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002364 return 0;
2365
Douglas Gregor8f45df52009-04-16 22:23:12 +00002366 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002367 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002368 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002369 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002370 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2371 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002372 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002373
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002374 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002375 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002376 return Offset;
2377}
2378
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002379void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002380 using namespace llvm;
2381 RecordData Record;
2382
2383 // Write the type offsets array
2384 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002385 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2389 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2390 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002391 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002392 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002393 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002394 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002395
2396 // Write the declaration offsets array
2397 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002398 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2402 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2403 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002404 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002405 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002406 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002407 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002408}
2409
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002410void ASTWriter::WriteFileDeclIDsMap() {
2411 using namespace llvm;
2412 RecordData Record;
2413
2414 // Join the vectors of DeclIDs from all files.
2415 SmallVector<DeclID, 256> FileSortedIDs;
2416 for (FileDeclIDsTy::iterator
2417 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2418 DeclIDInFileInfo &Info = *FI->second;
2419 Info.FirstDeclIndex = FileSortedIDs.size();
2420 for (LocDeclIDsTy::iterator
2421 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2422 FileSortedIDs.push_back(DI->second);
2423 }
2424
2425 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2426 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2429 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2430 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002431 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002432 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2433}
2434
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002435void ASTWriter::WriteComments() {
2436 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002437 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002438 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002439 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2440 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002441 I != E; ++I) {
2442 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002443 AddSourceRange((*I)->getSourceRange(), Record);
2444 Record.push_back((*I)->getKind());
2445 Record.push_back((*I)->isTrailingComment());
2446 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002447 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2448 }
2449 Stream.ExitBlock();
2450}
2451
Douglas Gregorc5046832009-04-27 18:38:38 +00002452//===----------------------------------------------------------------------===//
2453// Global Method Pool and Selector Serialization
2454//===----------------------------------------------------------------------===//
2455
Douglas Gregore84a9da2009-04-20 20:36:09 +00002456namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002457// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002458class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002459 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002460
2461public:
2462 typedef Selector key_type;
2463 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002464
Sebastian Redl834bb972010-08-04 17:20:04 +00002465 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002466 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002467 ObjCMethodList Instance, Factory;
2468 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002469 typedef const data_type& data_type_ref;
2470
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002471 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002472
Douglas Gregorc78d3462009-04-24 21:10:55 +00002473 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002474 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002475 }
Mike Stump11289f42009-09-09 15:08:12 +00002476
2477 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002478 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002479 data_type_ref Methods) {
2480 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2481 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002482 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2483 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002484 Method = Method->Next)
2485 if (Method->Method)
2486 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002487 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002488 Method = Method->Next)
2489 if (Method->Method)
2490 DataLen += 4;
2491 clang::io::Emit16(Out, DataLen);
2492 return std::make_pair(KeyLen, DataLen);
2493 }
Mike Stump11289f42009-09-09 15:08:12 +00002494
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002495 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002496 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002497 assert((Start >> 32) == 0 && "Selector key offset too large");
2498 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002499 unsigned N = Sel.getNumArgs();
2500 clang::io::Emit16(Out, N);
2501 if (N == 0)
2502 N = 1;
2503 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002504 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002505 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2506 }
Mike Stump11289f42009-09-09 15:08:12 +00002507
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002508 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002509 data_type_ref Methods, unsigned DataLen) {
2510 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002511 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002512 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002513 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002514 Method = Method->Next)
2515 if (Method->Method)
2516 ++NumInstanceMethods;
2517
2518 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002519 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002520 Method = Method->Next)
2521 if (Method->Method)
2522 ++NumFactoryMethods;
2523
2524 clang::io::Emit16(Out, NumInstanceMethods);
2525 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002526 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002527 Method = Method->Next)
2528 if (Method->Method)
2529 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002530 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002531 Method = Method->Next)
2532 if (Method->Method)
2533 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002534
2535 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002536 }
2537};
2538} // end anonymous namespace
2539
Sebastian Redla19a67f2010-08-03 21:58:15 +00002540/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002541///
2542/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002543/// in an on-disk hash table indexed by the selector. The hash table also
2544/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002545void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002546 using namespace llvm;
2547
Sebastian Redla19a67f2010-08-03 21:58:15 +00002548 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002549 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002550 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002551 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002552 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002553 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002554 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002555 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002556
Sebastian Redla19a67f2010-08-03 21:58:15 +00002557 // Create the on-disk hash table representation. We walk through every
2558 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002559 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002560 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002561 I = SelectorIDs.begin(), E = SelectorIDs.end();
2562 I != E; ++I) {
2563 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002564 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002565 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002566 I->second,
2567 ObjCMethodList(),
2568 ObjCMethodList()
2569 };
2570 if (F != SemaRef.MethodPool.end()) {
2571 Data.Instance = F->second.first;
2572 Data.Factory = F->second.second;
2573 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002574 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002575 // changed.
2576 if (Chain && I->second < FirstSelectorID) {
2577 // Selector already exists. Did it change?
2578 bool changed = false;
2579 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2580 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002581 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002582 changed = true;
2583 }
2584 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2585 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002586 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002587 changed = true;
2588 }
2589 if (!changed)
2590 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002591 } else if (Data.Instance.Method || Data.Factory.Method) {
2592 // A new method pool entry.
2593 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002594 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002595 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002596 }
2597
Douglas Gregorc78d3462009-04-24 21:10:55 +00002598 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002599 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002600 uint32_t BucketOffset;
2601 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002602 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002603 llvm::raw_svector_ostream Out(MethodPool);
2604 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002605 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002606 BucketOffset = Generator.Emit(Out, Trait);
2607 }
2608
2609 // Create a blob abbreviation
2610 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002611 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2615 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2616
Douglas Gregor95c13f52009-04-25 17:48:32 +00002617 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002618 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002619 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002620 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002621 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002622 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002623
2624 // Create a blob abbreviation for the selector table offsets.
2625 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002626 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2630 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2631
2632 // Write the selector offsets table.
2633 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002634 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002635 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002636 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002637 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002638 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002639 }
2640}
2641
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002642/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002643void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002644 using namespace llvm;
2645 if (SemaRef.ReferencedSelectors.empty())
2646 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002647
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002648 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002649
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002650 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002651 // very tricky to fix, and given that @selector shouldn't really appear in
2652 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002653 for (DenseMap<Selector, SourceLocation>::iterator S =
2654 SemaRef.ReferencedSelectors.begin(),
2655 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2656 Selector Sel = (*S).first;
2657 SourceLocation Loc = (*S).second;
2658 AddSelectorRef(Sel, Record);
2659 AddSourceLocation(Loc, Record);
2660 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002661 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002662}
2663
Douglas Gregorc5046832009-04-27 18:38:38 +00002664//===----------------------------------------------------------------------===//
2665// Identifier Table Serialization
2666//===----------------------------------------------------------------------===//
2667
Douglas Gregorc78d3462009-04-24 21:10:55 +00002668namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002669class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002670 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002671 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002672 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002673 bool IsModule;
2674
Douglas Gregor1d583f22009-04-28 21:18:29 +00002675 /// \brief Determines whether this is an "interesting" identifier
2676 /// that needs a full IdentifierInfo structure written into the hash
2677 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002678 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002679 if (II->isPoisoned() ||
2680 II->isExtensionToken() ||
2681 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002682 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002683 II->getFETokenInfo<void>())
2684 return true;
2685
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002686 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002687 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002688
2689 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2690 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002691 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002692
2693 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002694 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002695
2696 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002697 }
2698
Douglas Gregore84a9da2009-04-20 20:36:09 +00002699public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002700 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002701 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002702
Sebastian Redl539c5062010-08-18 23:57:32 +00002703 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002704 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002705
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002706 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2707 IdentifierResolver &IdResolver, bool IsModule)
2708 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002709
2710 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002711 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002712 }
Mike Stump11289f42009-09-09 15:08:12 +00002713
2714 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002715 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002716 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002717 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002718 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002719 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002720 DataLen += 2; // 2 bytes for builtin ID
2721 DataLen += 2; // 2 bytes for flags
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002722 if (hadMacroDefinition(II, Macro)) {
2723 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2724 if (Writer.getMacroRef(M) != 0)
2725 DataLen += 4;
2726 }
2727
2728 DataLen += 4;
2729 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002730
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002731 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2732 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002733 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002734 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002735 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002736 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002737 // We emit the key length after the data length so that every
2738 // string is preceded by a 16-bit length. This matches the PTH
2739 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002740 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002741 return std::make_pair(KeyLen, DataLen);
2742 }
Mike Stump11289f42009-09-09 15:08:12 +00002743
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002744 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002745 unsigned KeyLen) {
2746 // Record the location of the key data. This is used when generating
2747 // the mapping from persistent IDs to strings.
2748 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002749 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002750 }
Mike Stump11289f42009-09-09 15:08:12 +00002751
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002752 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002753 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002754 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002755 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002756 clang::io::Emit32(Out, ID << 1);
2757 return;
2758 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002759
Douglas Gregor1d583f22009-04-28 21:18:29 +00002760 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002761 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2762 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2763 clang::io::Emit16(Out, Bits);
2764 Bits = 0;
2765 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002766 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002767 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2768 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002769 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002770 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002771 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002772
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002773 if (HadMacroDefinition) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002774 // Write all of the macro IDs associated with this identifier.
2775 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2776 if (MacroID ID = Writer.getMacroRef(M))
2777 clang::io::Emit32(Out, ID);
2778 }
2779
2780 clang::io::Emit32(Out, 0);
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002781 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002782
Douglas Gregora868bbd2009-04-21 22:25:48 +00002783 // Emit the declaration IDs in reverse order, because the
2784 // IdentifierResolver provides the declarations as they would be
2785 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002786 // "stat"), but the ASTReader adds declarations to the end of the list
2787 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002788 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002789 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2790 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002791 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002792 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002793 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002794 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002795 }
2796};
2797} // end anonymous namespace
2798
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002799/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002800///
2801/// The identifier table consists of a blob containing string data
2802/// (the actual identifiers themselves) and a separate "offsets" index
2803/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002804void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2805 IdentifierResolver &IdResolver,
2806 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002807 using namespace llvm;
2808
2809 // Create and write out the blob that contains the identifier
2810 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002811 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002812 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002813 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002814
Douglas Gregore6648fb2009-04-28 20:33:11 +00002815 // Look for any identifiers that were named while processing the
2816 // headers, but are otherwise not needed. We add these to the hash
2817 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002818 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002819 // file.
2820 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2821 IDEnd = PP.getIdentifierTable().end();
2822 ID != IDEnd; ++ID)
2823 getIdentifierRef(ID->second);
2824
Sebastian Redlff4a2952010-07-23 23:49:55 +00002825 // Create the on-disk hash table representation. We only store offsets
2826 // for identifiers that appear here for the first time.
2827 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002828 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002829 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2830 ID != IDEnd; ++ID) {
2831 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002832 if (!Chain || !ID->first->isFromAST() ||
2833 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002834 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2835 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002836 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002837
Douglas Gregore84a9da2009-04-20 20:36:09 +00002838 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002839 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002840 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002841 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002842 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002843 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002844 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002845 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002846 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002847 }
2848
2849 // Create a blob abbreviation
2850 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002851 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002852 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002853 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002854 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002855
2856 // Write the identifier table
2857 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002858 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002859 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002860 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002861 }
2862
2863 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002864 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002865 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2869 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2870
2871 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002872 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002873 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002874 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002875 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002876 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002877}
2878
Douglas Gregorc5046832009-04-27 18:38:38 +00002879//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002880// DeclContext's Name Lookup Table Serialization
2881//===----------------------------------------------------------------------===//
2882
2883namespace {
2884// Trait used for the on-disk hash table used in the method pool.
2885class ASTDeclContextNameLookupTrait {
2886 ASTWriter &Writer;
2887
2888public:
2889 typedef DeclarationName key_type;
2890 typedef key_type key_type_ref;
2891
2892 typedef DeclContext::lookup_result data_type;
2893 typedef const data_type& data_type_ref;
2894
2895 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2896
2897 unsigned ComputeHash(DeclarationName Name) {
2898 llvm::FoldingSetNodeID ID;
2899 ID.AddInteger(Name.getNameKind());
2900
2901 switch (Name.getNameKind()) {
2902 case DeclarationName::Identifier:
2903 ID.AddString(Name.getAsIdentifierInfo()->getName());
2904 break;
2905 case DeclarationName::ObjCZeroArgSelector:
2906 case DeclarationName::ObjCOneArgSelector:
2907 case DeclarationName::ObjCMultiArgSelector:
2908 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2909 break;
2910 case DeclarationName::CXXConstructorName:
2911 case DeclarationName::CXXDestructorName:
2912 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002913 break;
2914 case DeclarationName::CXXOperatorName:
2915 ID.AddInteger(Name.getCXXOverloadedOperator());
2916 break;
2917 case DeclarationName::CXXLiteralOperatorName:
2918 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2919 case DeclarationName::CXXUsingDirective:
2920 break;
2921 }
2922
2923 return ID.ComputeHash();
2924 }
2925
2926 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002927 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002928 data_type_ref Lookup) {
2929 unsigned KeyLen = 1;
2930 switch (Name.getNameKind()) {
2931 case DeclarationName::Identifier:
2932 case DeclarationName::ObjCZeroArgSelector:
2933 case DeclarationName::ObjCOneArgSelector:
2934 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002935 case DeclarationName::CXXLiteralOperatorName:
2936 KeyLen += 4;
2937 break;
2938 case DeclarationName::CXXOperatorName:
2939 KeyLen += 1;
2940 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002941 case DeclarationName::CXXConstructorName:
2942 case DeclarationName::CXXDestructorName:
2943 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002944 case DeclarationName::CXXUsingDirective:
2945 break;
2946 }
2947 clang::io::Emit16(Out, KeyLen);
2948
2949 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00002950 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002951 clang::io::Emit16(Out, DataLen);
2952
2953 return std::make_pair(KeyLen, DataLen);
2954 }
2955
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002956 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002957 using namespace clang::io;
2958
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002959 Emit8(Out, Name.getNameKind());
2960 switch (Name.getNameKind()) {
2961 case DeclarationName::Identifier:
2962 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002963 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002964 case DeclarationName::ObjCZeroArgSelector:
2965 case DeclarationName::ObjCOneArgSelector:
2966 case DeclarationName::ObjCMultiArgSelector:
2967 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002968 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002969 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002970 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2971 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002972 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00002973 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002974 case DeclarationName::CXXLiteralOperatorName:
2975 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002976 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002977 case DeclarationName::CXXConstructorName:
2978 case DeclarationName::CXXDestructorName:
2979 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002980 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002981 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002982 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00002983
2984 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002985 }
2986
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002987 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002988 data_type Lookup, unsigned DataLen) {
2989 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00002990 clang::io::Emit16(Out, Lookup.size());
2991 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
2992 I != E; ++I)
2993 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002994
2995 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2996 }
2997};
2998} // end anonymous namespace
2999
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003000/// \brief Write the block containing all of the declaration IDs
3001/// visible from the given DeclContext.
3002///
3003/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003004/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003005uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3006 DeclContext *DC) {
3007 if (DC->getPrimaryContext() != DC)
3008 return 0;
3009
3010 // Since there is no name lookup into functions or methods, don't bother to
3011 // build a visible-declarations table for these entities.
3012 if (DC->isFunctionOrMethod())
3013 return 0;
3014
3015 // If not in C++, we perform name lookup for the translation unit via the
3016 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3017 // FIXME: In C++ we need the visible declarations in order to "see" the
3018 // friend declarations, is there a way to do this without writing the table ?
David Blaikiebbafb8a2012-03-11 07:00:24 +00003019 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003020 return 0;
3021
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003022 // Serialize the contents of the mapping used for lookup. Note that,
3023 // although we have two very different code paths, the serialized
3024 // representation is the same for both cases: a declaration name,
3025 // followed by a size, followed by references to the visible
3026 // declarations that have that name.
3027 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003028 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003029 if (!Map || Map->empty())
3030 return 0;
3031
3032 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3033 ASTDeclContextNameLookupTrait Trait(*this);
3034
3035 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003036 DeclarationName ConversionName;
3037 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003038 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3039 D != DEnd; ++D) {
3040 DeclarationName Name = D->first;
3041 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikieff7d47a2012-12-19 00:45:41 +00003042 if (!Result.empty()) {
Douglas Gregor05ef9312011-08-30 20:49:19 +00003043 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3044 // Hash all conversion function names to the same name. The actual
3045 // type information in conversion function name is not used in the
3046 // key (since such type information is not stable across different
3047 // modules), so the intended effect is to coalesce all of the conversion
3048 // functions under a single key.
3049 if (!ConversionName)
3050 ConversionName = Name;
David Blaikieff7d47a2012-12-19 00:45:41 +00003051 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregor05ef9312011-08-30 20:49:19 +00003052 continue;
3053 }
3054
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003055 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003056 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003057 }
3058
Douglas Gregor05ef9312011-08-30 20:49:19 +00003059 // Add the conversion functions
3060 if (!ConversionDecls.empty()) {
3061 Generator.insert(ConversionName,
3062 DeclContext::lookup_result(ConversionDecls.begin(),
3063 ConversionDecls.end()),
3064 Trait);
3065 }
3066
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003067 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003068 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003069 uint32_t BucketOffset;
3070 {
3071 llvm::raw_svector_ostream Out(LookupTable);
3072 // Make sure that no bucket is at offset 0
3073 clang::io::Emit32(Out, 0);
3074 BucketOffset = Generator.Emit(Out, Trait);
3075 }
3076
3077 // Write the lookup table
3078 RecordData Record;
3079 Record.push_back(DECL_CONTEXT_VISIBLE);
3080 Record.push_back(BucketOffset);
3081 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3082 LookupTable.str());
3083
3084 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3085 ++NumVisibleDeclContexts;
3086 return Offset;
3087}
3088
Sebastian Redla4071b42010-08-24 00:50:09 +00003089/// \brief Write an UPDATE_VISIBLE block for the given context.
3090///
3091/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3092/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003093/// (in C++), for namespaces, and for classes with forward-declared unscoped
3094/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003095void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003096 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3097 if (!Map || Map->empty())
3098 return;
3099
3100 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3101 ASTDeclContextNameLookupTrait Trait(*this);
3102
3103 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003104 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3105 D != DEnd; ++D) {
3106 DeclarationName Name = D->first;
3107 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003108 // For any name that appears in this table, the results are complete, i.e.
3109 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikieff7d47a2012-12-19 00:45:41 +00003110 if (!Result.empty())
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003111 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003112 }
3113
3114 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003115 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003116 uint32_t BucketOffset;
3117 {
3118 llvm::raw_svector_ostream Out(LookupTable);
3119 // Make sure that no bucket is at offset 0
3120 clang::io::Emit32(Out, 0);
3121 BucketOffset = Generator.Emit(Out, Trait);
3122 }
3123
3124 // Write the lookup table
3125 RecordData Record;
3126 Record.push_back(UPDATE_VISIBLE);
3127 Record.push_back(getDeclID(cast<Decl>(DC)));
3128 Record.push_back(BucketOffset);
3129 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3130}
3131
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003132/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3133void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3134 RecordData Record;
3135 Record.push_back(Opts.fp_contract);
3136 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3137}
3138
3139/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3140void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003141 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003142 return;
3143
3144 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3145 RecordData Record;
3146#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3147#include "clang/Basic/OpenCLExtensions.def"
3148 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3149}
3150
Douglas Gregor358cd442012-01-15 16:58:34 +00003151void ASTWriter::WriteRedeclarations() {
3152 RecordData LocalRedeclChains;
3153 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3154
3155 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3156 Decl *First = Redeclarations[I];
3157 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3158
3159 Decl *MostRecent = First->getMostRecentDecl();
3160
3161 // If we only have a single declaration, there is no point in storing
3162 // a redeclaration chain.
3163 if (First == MostRecent)
3164 continue;
3165
3166 unsigned Offset = LocalRedeclChains.size();
3167 unsigned Size = 0;
3168 LocalRedeclChains.push_back(0); // Placeholder for the size.
3169
3170 // Collect the set of local redeclarations of this declaration.
3171 for (Decl *Prev = MostRecent; Prev != First;
3172 Prev = Prev->getPreviousDecl()) {
3173 if (!Prev->isFromASTFile()) {
3174 AddDeclRef(Prev, LocalRedeclChains);
3175 ++Size;
3176 }
3177 }
3178 LocalRedeclChains[Offset] = Size;
3179
3180 // Reverse the set of local redeclarations, so that we store them in
3181 // order (since we found them in reverse order).
3182 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3183
3184 // Add the mapping from the first ID to the set of local declarations.
3185 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3186 LocalRedeclsMap.push_back(Info);
3187
3188 assert(N == Redeclarations.size() &&
3189 "Deserialized a declaration we shouldn't have");
3190 }
3191
3192 if (LocalRedeclChains.empty())
3193 return;
3194
3195 // Sort the local redeclarations map by the first declaration ID,
3196 // since the reader will be performing binary searches on this information.
3197 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3198
3199 // Emit the local redeclarations map.
3200 using namespace llvm;
3201 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3202 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3205 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3206
3207 RecordData Record;
3208 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3209 Record.push_back(LocalRedeclsMap.size());
3210 Stream.EmitRecordWithBlob(AbbrevID, Record,
3211 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3212 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3213
3214 // Emit the redeclaration chains.
3215 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3216}
3217
Douglas Gregor404cdde2012-01-27 01:47:08 +00003218void ASTWriter::WriteObjCCategories() {
3219 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3220 RecordData Categories;
3221
3222 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3223 unsigned Size = 0;
3224 unsigned StartIndex = Categories.size();
3225
3226 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3227
3228 // Allocate space for the size.
3229 Categories.push_back(0);
3230
3231 // Add the categories.
3232 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3233 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3234 assert(getDeclID(Cat) != 0 && "Bogus category");
3235 AddDeclRef(Cat, Categories);
3236 }
3237
3238 // Update the size.
3239 Categories[StartIndex] = Size;
3240
3241 // Record this interface -> category map.
3242 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3243 CategoriesMap.push_back(CatInfo);
3244 }
3245
3246 // Sort the categories map by the definition ID, since the reader will be
3247 // performing binary searches on this information.
3248 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3249
3250 // Emit the categories map.
3251 using namespace llvm;
3252 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3253 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3256 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3257
3258 RecordData Record;
3259 Record.push_back(OBJC_CATEGORIES_MAP);
3260 Record.push_back(CategoriesMap.size());
3261 Stream.EmitRecordWithBlob(AbbrevID, Record,
3262 reinterpret_cast<char*>(CategoriesMap.data()),
3263 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3264
3265 // Emit the category lists.
3266 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3267}
3268
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003269void ASTWriter::WriteMergedDecls() {
3270 if (!Chain || Chain->MergedDecls.empty())
3271 return;
3272
3273 RecordData Record;
3274 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3275 IEnd = Chain->MergedDecls.end();
3276 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003277 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003278 : getDeclID(I->first);
3279 assert(CanonID && "Merged declaration not known?");
3280
3281 Record.push_back(CanonID);
3282 Record.push_back(I->second.size());
3283 Record.append(I->second.begin(), I->second.end());
3284 }
3285 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3286}
3287
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003288//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003289// General Serialization Routines
3290//===----------------------------------------------------------------------===//
3291
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003292/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003293void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3294 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003295 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003296 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3297 e = Attrs.end(); i != e; ++i){
3298 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003299 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003300 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003301
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003302#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003303
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003304 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003305}
3306
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003307void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003308 Record.push_back(Str.size());
3309 Record.insert(Record.end(), Str.begin(), Str.end());
3310}
3311
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003312void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3313 RecordDataImpl &Record) {
3314 Record.push_back(Version.getMajor());
3315 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3316 Record.push_back(*Minor + 1);
3317 else
3318 Record.push_back(0);
3319 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3320 Record.push_back(*Subminor + 1);
3321 else
3322 Record.push_back(0);
3323}
3324
Douglas Gregore84a9da2009-04-20 20:36:09 +00003325/// \brief Note that the identifier II occurs at the given offset
3326/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003327void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003328 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003329 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003330 // up earlier in the chain and thus don't need an offset.
3331 if (ID >= FirstIdentID)
3332 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003333}
3334
Douglas Gregor95c13f52009-04-25 17:48:32 +00003335/// \brief Note that the selector Sel occurs at the given offset
3336/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003337void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003338 unsigned ID = SelectorIDs[Sel];
3339 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003340 // Don't record offsets for selectors that are also available in a different
3341 // file.
3342 if (ID < FirstSelectorID)
3343 return;
3344 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003345}
3346
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003347ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003348 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003349 WritingAST(false), DoneWritingDeclsAndTypes(false),
3350 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003351 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003352 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003353 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3354 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003355 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3356 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003357 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003358 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003359 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003360 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003361 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003362 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003363 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3364 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3365 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003366 DeclTypedefAbbrev(0),
3367 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3368 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003369{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003370}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003371
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003372ASTWriter::~ASTWriter() {
3373 for (FileDeclIDsTy::iterator
3374 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3375 delete I->second;
3376}
3377
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003378void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003379 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003380 Module *WritingModule, StringRef isysroot,
3381 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003382 WritingAST = true;
3383
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003384 ASTHasCompilerErrors = hasErrors;
3385
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003386 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003387 Stream.Emit((unsigned)'C', 8);
3388 Stream.Emit((unsigned)'P', 8);
3389 Stream.Emit((unsigned)'C', 8);
3390 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003391
Chris Lattner28fa4e62009-04-26 22:26:21 +00003392 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003393
Douglas Gregoreda8e122011-08-09 15:13:55 +00003394 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003395 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003396 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003397 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003398 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003399 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003400 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003401
3402 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003403}
3404
Douglas Gregora94a1542011-07-27 21:45:57 +00003405template<typename Vector>
3406static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3407 ASTWriter::RecordData &Record) {
3408 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3409 I != E; ++I) {
3410 Writer.AddDeclRef(*I, Record);
3411 }
3412}
3413
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003414void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003415 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003416 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003417 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003418 using namespace llvm;
3419
Douglas Gregorcf68c582011-12-01 22:20:10 +00003420 // Make sure that the AST reader knows to finalize itself.
3421 if (Chain)
3422 Chain->finalizeForWriting();
3423
Sebastian Redl143413f2010-07-12 22:02:52 +00003424 ASTContext &Context = SemaRef.Context;
3425 Preprocessor &PP = SemaRef.PP;
3426
Douglas Gregordab42432011-08-12 00:15:20 +00003427 // Set up predefined declaration IDs.
3428 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003429 if (Context.ObjCIdDecl)
3430 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003431 if (Context.ObjCSelDecl)
3432 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003433 if (Context.ObjCClassDecl)
3434 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003435 if (Context.ObjCProtocolClassDecl)
3436 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003437 if (Context.Int128Decl)
3438 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3439 if (Context.UInt128Decl)
3440 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003441 if (Context.ObjCInstanceTypeDecl)
3442 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003443 if (Context.BuiltinVaListDecl)
3444 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3445
Douglas Gregor851443c2011-08-12 01:39:19 +00003446 if (!Chain) {
3447 // Make sure that we emit IdentifierInfos (and any attached
3448 // declarations) for builtins. We don't need to do this when we're
3449 // emitting chained PCH files, because all of the builtins will be
3450 // in the original PCH file.
3451 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003452 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003453 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003454 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003455 Context.getLangOpts().NoBuiltin);
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003456 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3457 getIdentifierRef(&Table.get(BuiltinNames[I]));
3458 }
3459
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003460 // If there are any out-of-date identifiers, bring them up to date.
3461 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003462 // Find out-of-date identifiers.
3463 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003464 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3465 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003466 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003467 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003468 OutOfDate.push_back(ID->second);
3469 }
3470
3471 // Update the out-of-date identifiers.
3472 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3473 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3474 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003475 }
3476
Chris Lattner0c797362009-09-08 18:19:27 +00003477 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003478 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003479 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003480 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003481 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003482
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003483 // Build a record containing all of the file scoped decls in this file.
3484 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003485 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3486 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003487
Douglas Gregor851443c2011-08-12 01:39:19 +00003488 // Build a record containing all of the delegating constructors we still need
3489 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003490 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003491 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003492
Douglas Gregor851443c2011-08-12 01:39:19 +00003493 // Write the set of weak, undeclared identifiers. We always write the
3494 // entire table, since later PCH files in a PCH chain are only interested in
3495 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003496 RecordData WeakUndeclaredIdentifiers;
3497 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003498 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003499 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3500 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3501 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3502 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3503 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3504 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3505 }
3506 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003507
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003508 // Build a record containing all of the locally-scoped external
3509 // declarations in this header file. Generally, this record will be
3510 // empty.
3511 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003512 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003513 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003514 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003515 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3516 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003517 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003518 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003519 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3520 }
3521
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003522 // Build a record containing all of the ext_vector declarations.
3523 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003524 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003525
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003526 // Build a record containing all of the VTable uses information.
3527 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003528 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003529 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3530 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3531 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3532 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3533 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003534 }
3535
3536 // Build a record containing all of dynamic classes declarations.
3537 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003538 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003539
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003540 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003541 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003542 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003543 I = SemaRef.PendingInstantiations.begin(),
3544 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3545 AddDeclRef(I->first, PendingInstantiations);
3546 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003547 }
3548 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3549 "There are local ones at end of translation unit!");
3550
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003551 // Build a record containing some declaration references.
3552 RecordData SemaDeclRefs;
3553 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3554 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3555 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3556 }
3557
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003558 RecordData CUDASpecialDeclRefs;
3559 if (Context.getcudaConfigureCallDecl()) {
3560 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3561 }
3562
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003563 // Build a record containing all of the known namespaces.
3564 RecordData KnownNamespaces;
3565 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3566 I = SemaRef.KnownNamespaces.begin(),
3567 IEnd = SemaRef.KnownNamespaces.end();
3568 I != IEnd; ++I) {
3569 if (!I->second)
3570 AddDeclRef(I->first, KnownNamespaces);
3571 }
Douglas Gregor112b9072012-10-18 05:31:06 +00003572
3573 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00003574 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00003575
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003576 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003577 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003578 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00003579
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00003580 // This is so that older clang versions, before the introduction
3581 // of the control block, can read and reject the newer PCH format.
3582 Record.clear();
3583 Record.push_back(VERSION_MAJOR);
3584 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3585
Douglas Gregor851443c2011-08-12 01:39:19 +00003586 // Create a lexical update block containing all of the declarations in the
3587 // translation unit that do not come from other AST files.
3588 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3589 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3590 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3591 E = TU->noload_decls_end();
3592 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003593 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003594 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003595 }
3596
3597 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3598 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3599 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3600 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3601 Record.clear();
3602 Record.push_back(TU_UPDATE_LEXICAL);
3603 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3604 data(NewGlobalDecls));
3605
3606 // And a visible updates block for the translation unit.
3607 Abv = new llvm::BitCodeAbbrev();
3608 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3609 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3610 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3611 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3612 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3613 WriteDeclContextVisibleUpdate(TU);
3614
3615 // If the translation unit has an anonymous namespace, and we don't already
3616 // have an update block for it, write it as an update block.
3617 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3618 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3619 if (Record.empty()) {
3620 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003621 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003622 }
3623 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003624
3625 // Make sure visible decls, added to DeclContexts previously loaded from
3626 // an AST file, are registered for serialization.
3627 for (SmallVector<const Decl *, 16>::iterator
3628 I = UpdatingVisibleDecls.begin(),
3629 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3630 GetDeclRef(*I);
3631 }
3632
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003633 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003634 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003635
Douglas Gregor5204bde2011-08-02 16:26:37 +00003636 // Form the record of special types.
3637 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00003638 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003639 AddTypeRef(Context.getFILEType(), SpecialTypes);
3640 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3641 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3642 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3643 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003644 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003645 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003646
Douglas Gregor1970d882009-04-26 03:49:13 +00003647 // Keep writing types and declarations until all types and
3648 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003649 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003650 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003651 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3652 E = DeclsToRewrite.end();
3653 I != E; ++I)
3654 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003655 while (!DeclTypesToEmit.empty()) {
3656 DeclOrType DOT = DeclTypesToEmit.front();
3657 DeclTypesToEmit.pop();
3658 if (DOT.isType())
3659 WriteType(DOT.getType());
3660 else
3661 WriteDecl(Context, DOT.getDecl());
3662 }
3663 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003664
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003665 DoneWritingDeclsAndTypes = true;
3666
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003667 WriteFileDeclIDsMap();
3668 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003669 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003670
3671 if (Chain) {
3672 // Write the mapping information describing our module dependencies and how
3673 // each of those modules were mapped into our own offset/ID space, so that
3674 // the reader can build the appropriate mapping to its own offset/ID space.
3675 // The map consists solely of a blob with the following format:
3676 // *(module-name-len:i16 module-name:len*i8
3677 // source-location-offset:i32
3678 // identifier-id:i32
3679 // preprocessed-entity-id:i32
3680 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003681 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003682 // selector-id:i32
3683 // declaration-id:i32
3684 // c++-base-specifiers-id:i32
3685 // type-id:i32)
3686 //
3687 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3688 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3690 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003691 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003692 {
3693 llvm::raw_svector_ostream Out(Buffer);
3694 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003695 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003696 M != MEnd; ++M) {
3697 StringRef FileName = (*M)->FileName;
3698 io::Emit16(Out, FileName.size());
3699 Out.write(FileName.data(), FileName.size());
3700 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3701 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003702 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003703 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003704 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003705 io::Emit32(Out, (*M)->BaseSelectorID);
3706 io::Emit32(Out, (*M)->BaseDeclID);
3707 io::Emit32(Out, (*M)->BaseTypeIndex);
3708 }
3709 }
3710 Record.clear();
3711 Record.push_back(MODULE_OFFSET_MAP);
3712 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3713 Buffer.data(), Buffer.size());
3714 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003715 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003716 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003717 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003718 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003719 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003720 WriteFPPragmaOptions(SemaRef.getFPOptions());
3721 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003722
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003723 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003724 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003725
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003726 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003727
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003728 // If we're emitting a module, write out the submodule information.
3729 if (WritingModule)
3730 WriteSubmodules(WritingModule);
3731
Douglas Gregor5204bde2011-08-02 16:26:37 +00003732 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3733
Douglas Gregord4df8652009-04-22 22:02:47 +00003734 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003735 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003736 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003737
3738 // Write the record containing tentative definitions.
3739 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003740 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003741
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003742 // Write the record containing unused file scoped decls.
3743 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003744 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003745
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003746 // Write the record containing weak undeclared identifiers.
3747 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003748 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003749 WeakUndeclaredIdentifiers);
3750
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003751 // Write the record containing locally-scoped external definitions.
3752 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003753 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003754 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003755
3756 // Write the record containing ext_vector type names.
3757 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003758 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003759
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003760 // Write the record containing VTable uses information.
3761 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003762 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003763
3764 // Write the record containing dynamic classes declarations.
3765 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003766 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003767
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003768 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003769 if (!PendingInstantiations.empty())
3770 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003771
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003772 // Write the record containing declaration references of Sema.
3773 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003774 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003775
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003776 // Write the record containing CUDA-specific declaration references.
3777 if (!CUDASpecialDeclRefs.empty())
3778 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003779
3780 // Write the delegating constructors.
3781 if (!DelegatingCtorDecls.empty())
3782 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003783
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003784 // Write the known namespaces.
3785 if (!KnownNamespaces.empty())
3786 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3787
Douglas Gregor851443c2011-08-12 01:39:19 +00003788 // Write the visible updates to DeclContexts.
3789 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3790 I = UpdatedDeclContexts.begin(),
3791 E = UpdatedDeclContexts.end();
3792 I != E; ++I)
3793 WriteDeclContextVisibleUpdate(*I);
3794
Douglas Gregor959bb062011-12-03 01:15:29 +00003795 if (!WritingModule) {
3796 // Write the submodules that were imported, if any.
3797 RecordData ImportedModules;
3798 for (ASTContext::import_iterator I = Context.local_import_begin(),
3799 IEnd = Context.local_import_end();
3800 I != IEnd; ++I) {
3801 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3802 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3803 }
3804 if (!ImportedModules.empty()) {
3805 // Sort module IDs.
3806 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3807
3808 // Unique module IDs.
3809 ImportedModules.erase(std::unique(ImportedModules.begin(),
3810 ImportedModules.end()),
3811 ImportedModules.end());
3812
3813 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3814 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003815 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003816
3817 WriteMacroUpdates();
Douglas Gregordab42432011-08-12 00:15:20 +00003818 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003819 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003820 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003821 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003822 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003823
Douglas Gregor08f01292009-04-17 22:13:46 +00003824 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003825 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003826 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003827 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003828 Record.push_back(NumLexicalDeclContexts);
3829 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003830 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003831 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003832}
3833
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003834void ASTWriter::WriteMacroUpdates() {
3835 if (MacroUpdates.empty())
3836 return;
3837
3838 RecordData Record;
3839 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3840 E = MacroUpdates.end();
3841 I != E; ++I) {
3842 addMacroRef(I->first, Record);
3843 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregorcfa46a82012-10-12 00:16:50 +00003844 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003845 }
3846 Stream.EmitRecord(MACRO_UPDATES, Record);
3847}
3848
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003849/// \brief Go through the declaration update blocks and resolve declaration
3850/// pointers into declaration IDs.
3851void ASTWriter::ResolveDeclUpdatesBlocks() {
3852 for (DeclUpdateMap::iterator
3853 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3854 const Decl *D = I->first;
3855 UpdateRecord &URec = I->second;
3856
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003857 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003858 continue; // The decl will be written completely
3859
3860 unsigned Idx = 0, N = URec.size();
3861 while (Idx < N) {
3862 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003863 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3864 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3865 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3866 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3867 ++Idx;
3868 break;
3869
3870 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3871 ++Idx;
3872 break;
3873 }
3874 }
3875 }
3876}
3877
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003878void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003879 if (DeclUpdates.empty())
3880 return;
3881
3882 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003883 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003884 for (DeclUpdateMap::iterator
3885 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3886 const Decl *D = I->first;
3887 UpdateRecord &URec = I->second;
3888
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003889 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003890 continue; // The decl will be written completely,no need to store updates.
3891
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003892 uint64_t Offset = Stream.GetCurrentBitNo();
3893 Stream.EmitRecord(DECL_UPDATES, URec);
3894
3895 OffsetsRecord.push_back(GetDeclRef(D));
3896 OffsetsRecord.push_back(Offset);
3897 }
3898 Stream.ExitBlock();
3899 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3900}
3901
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003902void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003903 if (ReplacedDecls.empty())
3904 return;
3905
3906 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003907 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003908 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003909 Record.push_back(I->ID);
3910 Record.push_back(I->Offset);
3911 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003912 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003913 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003914}
3915
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003916void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003917 Record.push_back(Loc.getRawEncoding());
3918}
3919
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003920void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003921 AddSourceLocation(Range.getBegin(), Record);
3922 AddSourceLocation(Range.getEnd(), Record);
3923}
3924
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003925void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003926 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003927 const uint64_t *Words = Value.getRawData();
3928 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003929}
3930
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003931void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003932 Record.push_back(Value.isUnsigned());
3933 AddAPInt(Value, Record);
3934}
3935
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003936void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003937 AddAPInt(Value.bitcastToAPInt(), Record);
3938}
3939
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003940void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003941 Record.push_back(getIdentifierRef(II));
3942}
3943
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003944void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3945 Record.push_back(getMacroRef(MI));
3946}
3947
Sebastian Redl539c5062010-08-18 23:57:32 +00003948IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003949 if (II == 0)
3950 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003951
Sebastian Redl539c5062010-08-18 23:57:32 +00003952 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003953 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003954 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003955 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003956}
3957
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003958MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3959 // Don't emit builtin macros like __LINE__ to the AST file unless they
3960 // have been redefined by the header (in which case they are not
3961 // isBuiltinMacro).
3962 if (MI == 0 || MI->isBuiltinMacro())
3963 return 0;
3964
3965 MacroID &ID = MacroIDs[MI];
3966 if (ID == 0)
3967 ID = NextMacroID++;
3968 return ID;
3969}
3970
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003971void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003972 Record.push_back(getSelectorRef(SelRef));
3973}
3974
Sebastian Redl539c5062010-08-18 23:57:32 +00003975SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003976 if (Sel.getAsOpaquePtr() == 0) {
3977 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003978 }
3979
Sebastian Redl539c5062010-08-18 23:57:32 +00003980 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003981 if (SID == 0 && Chain) {
3982 // This might trigger a ReadSelector callback, which will set the ID for
3983 // this selector.
3984 Chain->LoadSelector(Sel);
3985 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003986 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003987 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003988 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003989 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003990}
3991
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003992void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003993 AddDeclRef(Temp->getDestructor(), Record);
3994}
3995
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003996void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3997 CXXBaseSpecifier const *BasesEnd,
3998 RecordDataImpl &Record) {
3999 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4000 CXXBaseSpecifiersToWrite.push_back(
4001 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4002 Bases, BasesEnd));
4003 Record.push_back(NextCXXBaseSpecifiersID++);
4004}
4005
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004006void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004007 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004008 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004009 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004010 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004011 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004012 break;
4013 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004014 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004015 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004016 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004017 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004018 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004019 break;
4020 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004021 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004022 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004023 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004024 break;
John McCall0ad16662009-10-29 08:12:44 +00004025 case TemplateArgument::Null:
4026 case TemplateArgument::Integral:
4027 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004028 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004029 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004030 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004031 break;
4032 }
4033}
4034
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004035void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004036 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004037 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004038
4039 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4040 bool InfoHasSameExpr
4041 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4042 Record.push_back(InfoHasSameExpr);
4043 if (InfoHasSameExpr)
4044 return; // Avoid storing the same expr twice.
4045 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004046 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4047 Record);
4048}
4049
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004050void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4051 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004052 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004053 AddTypeRef(QualType(), Record);
4054 return;
4055 }
4056
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004057 AddTypeLoc(TInfo->getTypeLoc(), Record);
4058}
4059
4060void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4061 AddTypeRef(TL.getType(), Record);
4062
John McCall8f115c62009-10-16 21:56:05 +00004063 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004064 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004065 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004066}
4067
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004068void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004069 Record.push_back(GetOrCreateTypeID(T));
4070}
4071
Douglas Gregoreda8e122011-08-09 15:13:55 +00004072TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4073 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004074 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4075}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004076
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004077TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00004078 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004079 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004080}
4081
4082TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4083 if (T.isNull())
4084 return TypeIdx();
4085 assert(!T.getLocalFastQualifiers());
4086
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004087 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004088 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004089 if (DoneWritingDeclsAndTypes) {
4090 assert(0 && "New type seen after serializing all the types to emit!");
4091 return TypeIdx();
4092 }
4093
Douglas Gregor1970d882009-04-26 03:49:13 +00004094 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004095 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004096 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004097 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004098 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004099 return Idx;
4100}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004101
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004102TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004103 if (T.isNull())
4104 return TypeIdx();
4105 assert(!T.getLocalFastQualifiers());
4106
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004107 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4108 assert(I != TypeIdxs.end() && "Type not emitted!");
4109 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004110}
4111
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004112void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004113 Record.push_back(GetDeclRef(D));
4114}
4115
Sebastian Redl539c5062010-08-18 23:57:32 +00004116DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004117 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4118
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004119 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004120 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004121 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004122
4123 // If D comes from an AST file, its declaration ID is already known and
4124 // fixed.
4125 if (D->isFromASTFile())
4126 return D->getGlobalID();
4127
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004128 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004129 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004130 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004131 if (DoneWritingDeclsAndTypes) {
4132 assert(0 && "New decl seen after serializing all the decls to emit!");
4133 return 0;
4134 }
4135
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004136 // We haven't seen this declaration before. Give it a new ID and
4137 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004138 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004139 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004140 }
4141
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004142 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004143}
4144
Sebastian Redl539c5062010-08-18 23:57:32 +00004145DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004146 if (D == 0)
4147 return 0;
4148
Douglas Gregorb3163e52012-01-05 22:33:30 +00004149 // If D comes from an AST file, its declaration ID is already known and
4150 // fixed.
4151 if (D->isFromASTFile())
4152 return D->getGlobalID();
4153
Douglas Gregore84a9da2009-04-20 20:36:09 +00004154 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4155 return DeclIDs[D];
4156}
4157
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004158static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4159 std::pair<unsigned, serialization::DeclID> R) {
4160 return L.first < R.first;
4161}
4162
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004163void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004164 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004165 assert(D);
4166
4167 SourceLocation Loc = D->getLocation();
4168 if (Loc.isInvalid())
4169 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004170
4171 // We only keep track of the file-level declarations of each file.
4172 if (!D->getLexicalDeclContext()->isFileContext())
4173 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004174 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4175 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004176 if (isa<ParmVarDecl>(D))
4177 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004178
4179 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004180 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004181 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004182 FileID FID;
4183 unsigned Offset;
4184 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004185 if (FID.isInvalid())
4186 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004187 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004188
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004189 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004190 if (!Info)
4191 Info = new DeclIDInFileInfo();
4192
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004193 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004194 LocDeclIDsTy &Decls = Info->DeclIDs;
4195
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004196 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004197 Decls.push_back(LocDecl);
4198 return;
4199 }
4200
4201 LocDeclIDsTy::iterator
4202 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4203
4204 Decls.insert(I, LocDecl);
4205}
4206
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004207void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004208 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004209 Record.push_back(Name.getNameKind());
4210 switch (Name.getNameKind()) {
4211 case DeclarationName::Identifier:
4212 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4213 break;
4214
4215 case DeclarationName::ObjCZeroArgSelector:
4216 case DeclarationName::ObjCOneArgSelector:
4217 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004218 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004219 break;
4220
4221 case DeclarationName::CXXConstructorName:
4222 case DeclarationName::CXXDestructorName:
4223 case DeclarationName::CXXConversionFunctionName:
4224 AddTypeRef(Name.getCXXNameType(), Record);
4225 break;
4226
4227 case DeclarationName::CXXOperatorName:
4228 Record.push_back(Name.getCXXOverloadedOperator());
4229 break;
4230
Alexis Hunt3d221f22009-11-29 07:34:05 +00004231 case DeclarationName::CXXLiteralOperatorName:
4232 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4233 break;
4234
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004235 case DeclarationName::CXXUsingDirective:
4236 // No extra data to emit
4237 break;
4238 }
4239}
Chris Lattnerca025db2010-05-07 21:43:38 +00004240
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004241void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004242 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004243 switch (Name.getNameKind()) {
4244 case DeclarationName::CXXConstructorName:
4245 case DeclarationName::CXXDestructorName:
4246 case DeclarationName::CXXConversionFunctionName:
4247 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4248 break;
4249
4250 case DeclarationName::CXXOperatorName:
4251 AddSourceLocation(
4252 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4253 Record);
4254 AddSourceLocation(
4255 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4256 Record);
4257 break;
4258
4259 case DeclarationName::CXXLiteralOperatorName:
4260 AddSourceLocation(
4261 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4262 Record);
4263 break;
4264
4265 case DeclarationName::Identifier:
4266 case DeclarationName::ObjCZeroArgSelector:
4267 case DeclarationName::ObjCOneArgSelector:
4268 case DeclarationName::ObjCMultiArgSelector:
4269 case DeclarationName::CXXUsingDirective:
4270 break;
4271 }
4272}
4273
4274void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004275 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004276 AddDeclarationName(NameInfo.getName(), Record);
4277 AddSourceLocation(NameInfo.getLoc(), Record);
4278 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4279}
4280
4281void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004282 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004283 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004284 Record.push_back(Info.NumTemplParamLists);
4285 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4286 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4287}
4288
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004289void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004290 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004291 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004292 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004293 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004294
4295 // Push each of the NNS's onto a stack for serialization in reverse order.
4296 while (NNS) {
4297 NestedNames.push_back(NNS);
4298 NNS = NNS->getPrefix();
4299 }
4300
4301 Record.push_back(NestedNames.size());
4302 while(!NestedNames.empty()) {
4303 NNS = NestedNames.pop_back_val();
4304 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4305 Record.push_back(Kind);
4306 switch (Kind) {
4307 case NestedNameSpecifier::Identifier:
4308 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4309 break;
4310
4311 case NestedNameSpecifier::Namespace:
4312 AddDeclRef(NNS->getAsNamespace(), Record);
4313 break;
4314
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004315 case NestedNameSpecifier::NamespaceAlias:
4316 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4317 break;
4318
Chris Lattnerca025db2010-05-07 21:43:38 +00004319 case NestedNameSpecifier::TypeSpec:
4320 case NestedNameSpecifier::TypeSpecWithTemplate:
4321 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4322 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4323 break;
4324
4325 case NestedNameSpecifier::Global:
4326 // Don't need to write an associated value.
4327 break;
4328 }
4329 }
4330}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004331
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004332void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4333 RecordDataImpl &Record) {
4334 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004335 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004336 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004337
4338 // Push each of the nested-name-specifiers's onto a stack for
4339 // serialization in reverse order.
4340 while (NNS) {
4341 NestedNames.push_back(NNS);
4342 NNS = NNS.getPrefix();
4343 }
4344
4345 Record.push_back(NestedNames.size());
4346 while(!NestedNames.empty()) {
4347 NNS = NestedNames.pop_back_val();
4348 NestedNameSpecifier::SpecifierKind Kind
4349 = NNS.getNestedNameSpecifier()->getKind();
4350 Record.push_back(Kind);
4351 switch (Kind) {
4352 case NestedNameSpecifier::Identifier:
4353 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4354 AddSourceRange(NNS.getLocalSourceRange(), Record);
4355 break;
4356
4357 case NestedNameSpecifier::Namespace:
4358 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4359 AddSourceRange(NNS.getLocalSourceRange(), Record);
4360 break;
4361
4362 case NestedNameSpecifier::NamespaceAlias:
4363 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4364 AddSourceRange(NNS.getLocalSourceRange(), Record);
4365 break;
4366
4367 case NestedNameSpecifier::TypeSpec:
4368 case NestedNameSpecifier::TypeSpecWithTemplate:
4369 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4370 AddTypeLoc(NNS.getTypeLoc(), Record);
4371 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4372 break;
4373
4374 case NestedNameSpecifier::Global:
4375 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4376 break;
4377 }
4378 }
4379}
4380
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004381void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004382 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004383 Record.push_back(Kind);
4384 switch (Kind) {
4385 case TemplateName::Template:
4386 AddDeclRef(Name.getAsTemplateDecl(), Record);
4387 break;
4388
4389 case TemplateName::OverloadedTemplate: {
4390 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4391 Record.push_back(OvT->size());
4392 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4393 I != E; ++I)
4394 AddDeclRef(*I, Record);
4395 break;
4396 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004397
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004398 case TemplateName::QualifiedTemplate: {
4399 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4400 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4401 Record.push_back(QualT->hasTemplateKeyword());
4402 AddDeclRef(QualT->getTemplateDecl(), Record);
4403 break;
4404 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004405
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004406 case TemplateName::DependentTemplate: {
4407 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4408 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4409 Record.push_back(DepT->isIdentifier());
4410 if (DepT->isIdentifier())
4411 AddIdentifierRef(DepT->getIdentifier(), Record);
4412 else
4413 Record.push_back(DepT->getOperator());
4414 break;
4415 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004416
4417 case TemplateName::SubstTemplateTemplateParm: {
4418 SubstTemplateTemplateParmStorage *subst
4419 = Name.getAsSubstTemplateTemplateParm();
4420 AddDeclRef(subst->getParameter(), Record);
4421 AddTemplateName(subst->getReplacement(), Record);
4422 break;
4423 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004424
4425 case TemplateName::SubstTemplateTemplateParmPack: {
4426 SubstTemplateTemplateParmPackStorage *SubstPack
4427 = Name.getAsSubstTemplateTemplateParmPack();
4428 AddDeclRef(SubstPack->getParameterPack(), Record);
4429 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4430 break;
4431 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004432 }
4433}
4434
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004435void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004436 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004437 Record.push_back(Arg.getKind());
4438 switch (Arg.getKind()) {
4439 case TemplateArgument::Null:
4440 break;
4441 case TemplateArgument::Type:
4442 AddTypeRef(Arg.getAsType(), Record);
4443 break;
4444 case TemplateArgument::Declaration:
4445 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004446 Record.push_back(Arg.isDeclForReferenceParam());
4447 break;
4448 case TemplateArgument::NullPtr:
4449 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004450 break;
4451 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004452 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004453 AddTypeRef(Arg.getIntegralType(), Record);
4454 break;
4455 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004456 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4457 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004458 case TemplateArgument::TemplateExpansion:
4459 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004460 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4461 Record.push_back(*NumExpansions + 1);
4462 else
4463 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004464 break;
4465 case TemplateArgument::Expression:
4466 AddStmt(Arg.getAsExpr());
4467 break;
4468 case TemplateArgument::Pack:
4469 Record.push_back(Arg.pack_size());
4470 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4471 I != E; ++I)
4472 AddTemplateArgument(*I, Record);
4473 break;
4474 }
4475}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004476
4477void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004478ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004479 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004480 assert(TemplateParams && "No TemplateParams!");
4481 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4482 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4483 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4484 Record.push_back(TemplateParams->size());
4485 for (TemplateParameterList::const_iterator
4486 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4487 P != PEnd; ++P)
4488 AddDeclRef(*P, Record);
4489}
4490
4491/// \brief Emit a template argument list.
4492void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004493ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004494 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004495 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004496 Record.push_back(TemplateArgs->size());
4497 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004498 AddTemplateArgument(TemplateArgs->get(i), Record);
4499}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004500
4501
4502void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004503ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004504 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004505 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004506 I = Set.begin(), E = Set.end(); I != E; ++I) {
4507 AddDeclRef(I.getDecl(), Record);
4508 Record.push_back(I.getAccess());
4509 }
4510}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004511
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004512void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004513 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004514 Record.push_back(Base.isVirtual());
4515 Record.push_back(Base.isBaseOfClass());
4516 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004517 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004518 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004519 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004520 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4521 : SourceLocation(),
4522 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004523}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004524
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004525void ASTWriter::FlushCXXBaseSpecifiers() {
4526 RecordData Record;
4527 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4528 Record.clear();
4529
4530 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004531 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004532 if (Index == CXXBaseSpecifiersOffsets.size())
4533 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4534 else {
4535 if (Index > CXXBaseSpecifiersOffsets.size())
4536 CXXBaseSpecifiersOffsets.resize(Index + 1);
4537 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4538 }
4539
4540 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4541 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4542 Record.push_back(BEnd - B);
4543 for (; B != BEnd; ++B)
4544 AddCXXBaseSpecifier(*B, Record);
4545 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004546
4547 // Flush any expressions that were written as part of the base specifiers.
4548 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004549 }
4550
4551 CXXBaseSpecifiersToWrite.clear();
4552}
4553
Alexis Hunt1d792652011-01-08 20:30:50 +00004554void ASTWriter::AddCXXCtorInitializers(
4555 const CXXCtorInitializer * const *CtorInitializers,
4556 unsigned NumCtorInitializers,
4557 RecordDataImpl &Record) {
4558 Record.push_back(NumCtorInitializers);
4559 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4560 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004561
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004562 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004563 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004564 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004565 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004566 } else if (Init->isDelegatingInitializer()) {
4567 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004568 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004569 } else if (Init->isMemberInitializer()){
4570 Record.push_back(CTOR_INITIALIZER_MEMBER);
4571 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004572 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004573 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4574 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004575 }
Francois Pichetd583da02010-12-04 09:14:42 +00004576
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004577 AddSourceLocation(Init->getMemberLocation(), Record);
4578 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004579 AddSourceLocation(Init->getLParenLoc(), Record);
4580 AddSourceLocation(Init->getRParenLoc(), Record);
4581 Record.push_back(Init->isWritten());
4582 if (Init->isWritten()) {
4583 Record.push_back(Init->getSourceOrder());
4584 } else {
4585 Record.push_back(Init->getNumArrayIndices());
4586 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4587 AddDeclRef(Init->getArrayIndex(i), Record);
4588 }
4589 }
4590}
4591
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004592void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4593 assert(D->DefinitionData);
4594 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004595 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004596 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00004597 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004598 Record.push_back(Data.Aggregate);
4599 Record.push_back(Data.PlainOldData);
4600 Record.push_back(Data.Empty);
4601 Record.push_back(Data.Polymorphic);
4602 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004603 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004604 Record.push_back(Data.HasNoNonEmptyBases);
4605 Record.push_back(Data.HasPrivateFields);
4606 Record.push_back(Data.HasProtectedFields);
4607 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004608 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004609 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00004610 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00004611 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00004612 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4613 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4614 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4615 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4616 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4617 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00004618 Record.push_back(Data.HasTrivialSpecialMembers);
4619 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004620 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004621 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00004622 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004623 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004624 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004625 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00004626 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00004627 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
4628 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
4629 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
4630 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redlb7448632011-08-31 13:59:56 +00004631 Record.push_back(Data.FailedImplicitMoveConstructor);
4632 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004633 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004634
4635 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004636 if (Data.NumBases > 0)
4637 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4638 Record);
4639
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004640 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4641 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004642 if (Data.NumVBases > 0)
4643 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4644 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004645
4646 AddUnresolvedSet(Data.Conversions, Record);
4647 AddUnresolvedSet(Data.VisibleConversions, Record);
4648 // Data.Definition is the owning decl, no need to write it.
4649 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004650
4651 // Add lambda-specific data.
4652 if (Data.IsLambda) {
4653 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004654 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004655 Record.push_back(Lambda.NumCaptures);
4656 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004657 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004658 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00004659 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004660 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4661 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4662 AddSourceLocation(Capture.getLocation(), Record);
4663 Record.push_back(Capture.isImplicit());
4664 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4665 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4666 AddDeclRef(Var, Record);
4667 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4668 : SourceLocation(),
4669 Record);
4670 }
4671 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004672}
4673
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004674void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004675 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004676 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004677 assert(FirstDeclID == NextDeclID &&
4678 FirstTypeID == NextTypeID &&
4679 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004680 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004681 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004682 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004683 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004684
Sebastian Redl07a89a82010-07-30 00:29:29 +00004685 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004686
Douglas Gregordf0c1512011-08-18 04:12:04 +00004687 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4688 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4689 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004690 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004691 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004692 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004693 NextDeclID = FirstDeclID;
4694 NextTypeID = FirstTypeID;
4695 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004696 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004697 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004698 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004699}
4700
Sebastian Redl539c5062010-08-18 23:57:32 +00004701void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004702 IdentifierIDs[II] = ID;
4703}
4704
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004705void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4706 MacroIDs[MI] = ID;
4707}
4708
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004709void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004710 // Always take the highest-numbered type index. This copes with an interesting
4711 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004712 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004713 // keep the higher-numbered entry so that we can properly write it out to
4714 // the AST file.
4715 TypeIdx &StoredIdx = TypeIdxs[T];
4716 if (Idx.getIndex() >= StoredIdx.getIndex())
4717 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004718}
4719
Sebastian Redl539c5062010-08-18 23:57:32 +00004720void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004721 SelectorIDs[S] = ID;
4722}
Douglas Gregor91096292010-10-02 19:29:26 +00004723
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004724void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004725 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004726 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004727 MacroDefinitions[MD] = ID;
4728}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004729
Douglas Gregore37a85a2011-12-02 17:30:13 +00004730void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4731 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4732 SubmoduleIDs[Mod] = ID;
4733}
4734
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004735void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4736 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4737}
4738
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004739void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004740 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004741 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004742 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4743 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004744 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004745 // A forward reference was mutated into a definition. Rewrite it.
4746 // FIXME: This happens during template instantiation, should we
4747 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004748 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004749 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004750 }
4751}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004752
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004753void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004754 assert(!WritingAST && "Already writing the AST!");
4755
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004756 // TU and namespaces are handled elsewhere.
4757 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4758 return;
4759
Douglas Gregorb3722e22011-09-09 23:01:35 +00004760 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004761 return; // Not a source decl added to a DeclContext from PCH.
4762
4763 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004764 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004765}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004766
4767void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004768 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004769 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004770 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004771 return; // Not a source member added to a class from PCH.
4772 if (!isa<CXXMethodDecl>(D))
4773 return; // We are interested in lazily declared implicit methods.
4774
4775 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004776 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004777 UpdateRecord &Record = DeclUpdates[RD];
4778 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004779 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004780}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004781
4782void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4783 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004784 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004785 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004786 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004787 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004788 return; // Not a source specialization added to a template from PCH.
4789
4790 UpdateRecord &Record = DeclUpdates[TD];
4791 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004792 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004793}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004794
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004795void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4796 const FunctionDecl *D) {
4797 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004798 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004799 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004800 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004801 return; // Not a source specialization added to a template from PCH.
4802
4803 UpdateRecord &Record = DeclUpdates[TD];
4804 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004805 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004806}
4807
Sebastian Redlab238a72011-04-24 16:28:06 +00004808void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004809 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004810 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004811 return; // Declaration not imported from PCH.
4812
4813 // Implicit decl from a PCH was defined.
4814 // FIXME: Should implicit definition be a separate FunctionDecl?
4815 RewriteDecl(D);
4816}
4817
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004818void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004819 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004820 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004821 return;
4822
4823 // Since the actual instantiation is delayed, this really means that we need
4824 // to update the instantiation location.
4825 UpdateRecord &Record = DeclUpdates[D];
4826 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4827 AddSourceLocation(
4828 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4829}
4830
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004831void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4832 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004833 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004834 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004835 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004836
4837 assert(IFD->getDefinition() && "Category on a class without a definition?");
4838 ObjCClassesWithCategories.insert(
4839 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004840}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004841
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004842
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004843void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4844 const ObjCPropertyDecl *OrigProp,
4845 const ObjCCategoryDecl *ClassExt) {
4846 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4847 if (!D)
4848 return;
4849
4850 assert(!WritingAST && "Already writing the AST!");
4851 if (!D->isFromASTFile())
4852 return; // Declaration not imported from PCH.
4853
4854 RewriteDecl(D);
4855}