blob: 3af8a87b3023b3007d3eccdc6983e9062d6abe60 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-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 Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Douglas Gregorbbf38312012-10-24 16:50:34 +000028#include "clang/Lex/HeaderSearchOptions.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000039#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000040#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000041#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000042#include "llvm/ADT/APFloat.h"
43#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000044#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000046#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000047#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000048#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000049#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000050#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000051#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000052#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000053using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000054using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000055
Sebastian Redlade50002010-07-30 17:03:48 +000056template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000057static StringRef data(const std::vector<T, Allocator> &v) {
58 if (v.empty()) return StringRef();
59 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000061}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000062
63template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000064static StringRef data(const SmallVectorImpl<T> &v) {
65 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000066 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000067}
68
Douglas Gregor2cf26342009-04-09 22:27:44 +000069//===----------------------------------------------------------------------===//
70// Type serialization
71//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000072
Douglas Gregor2cf26342009-04-09 22:27:44 +000073namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000074 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000075 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000076 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000077
78 public:
79 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000080 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000081
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000082 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000083 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000084
85 void VisitArrayType(const ArrayType *T);
86 void VisitFunctionType(const FunctionType *T);
87 void VisitTagType(const TagType *T);
88
89#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
90#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000091#include "clang/AST/TypeNodes.def"
92 };
93}
94
Sebastian Redl3397c552010-08-18 23:56:27 +000095void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000096 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000097}
98
Sebastian Redl3397c552010-08-18 23:56:27 +000099void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000101 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102}
103
Sebastian Redl3397c552010-08-18 23:56:27 +0000104void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000106 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
Sebastian Redl3397c552010-08-18 23:56:27 +0000109void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000110 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000111 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000112}
113
Sebastian Redl3397c552010-08-18 23:56:27 +0000114void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000115 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
116 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000117 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000118}
119
Sebastian Redl3397c552010-08-18 23:56:27 +0000120void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000121 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000122 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000123}
124
Sebastian Redl3397c552010-08-18 23:56:27 +0000125void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 Writer.AddTypeRef(T->getPointeeType(), Record);
127 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000128 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000129}
130
Sebastian Redl3397c552010-08-18 23:56:27 +0000131void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132 Writer.AddTypeRef(T->getElementType(), Record);
133 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000134 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135}
136
Sebastian Redl3397c552010-08-18 23:56:27 +0000137void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000138 VisitArrayType(T);
139 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000140 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000141}
142
Sebastian Redl3397c552010-08-18 23:56:27 +0000143void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000145 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146}
147
Sebastian Redl3397c552010-08-18 23:56:27 +0000148void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000149 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000150 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
151 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000152 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000153 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154}
155
Sebastian Redl3397c552010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000157 Writer.AddTypeRef(T->getElementType(), Record);
158 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000159 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000160 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000161}
162
Sebastian Redl3397c552010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000165 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166}
167
Sebastian Redl3397c552010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000170 FunctionType::ExtInfo C = T->getExtInfo();
171 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000172 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000173 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000174 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000175 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000176 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000177}
178
Sebastian Redl3397c552010-08-18 23:56:27 +0000179void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000181 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182}
183
Sebastian Redl3397c552010-08-18 23:56:27 +0000184void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000185 VisitFunctionType(T);
186 Record.push_back(T->getNumArgs());
187 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
188 Writer.AddTypeRef(T->getArgType(I), Record);
189 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000190 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000191 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000192 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000193 Record.push_back(T->getExceptionSpecType());
194 if (T->getExceptionSpecType() == EST_Dynamic) {
195 Record.push_back(T->getNumExceptions());
196 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
197 Writer.AddTypeRef(T->getExceptionType(I), Record);
198 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
199 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000200 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
201 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
202 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000203 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
204 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000205 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000206 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000207}
208
Sebastian Redl3397c552010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000210 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000211 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000212}
John McCalled976492009-12-04 22:46:56 +0000213
Sebastian Redl3397c552010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000215 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000216 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
217 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000218 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219}
220
Sebastian Redl3397c552010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000222 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000223 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000224}
225
Sebastian Redl3397c552010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000228 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229}
230
Sebastian Redl3397c552010-08-18 23:56:27 +0000231void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000232 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000233 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000234 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000235}
236
Sean Huntca63c202011-05-24 22:41:36 +0000237void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
238 Writer.AddTypeRef(T->getBaseType(), Record);
239 Writer.AddTypeRef(T->getUnderlyingType(), Record);
240 Record.push_back(T->getUTTKind());
241 Code = TYPE_UNARY_TRANSFORM;
242}
243
Richard Smith34b41d92011-02-20 03:19:35 +0000244void ASTTypeWriter::VisitAutoType(const AutoType *T) {
245 Writer.AddTypeRef(T->getDeducedType(), Record);
246 Code = TYPE_AUTO;
247}
248
Sebastian Redl3397c552010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000250 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000251 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000252 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253 "Cannot serialize in the middle of a type definition");
254}
255
Sebastian Redl3397c552010-08-18 23:56:27 +0000256void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000258 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000259}
260
Sebastian Redl3397c552010-08-18 23:56:27 +0000261void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000263 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264}
265
John McCall9d156a72011-01-06 01:58:22 +0000266void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
267 Writer.AddTypeRef(T->getModifiedType(), Record);
268 Writer.AddTypeRef(T->getEquivalentType(), Record);
269 Record.push_back(T->getAttrKind());
270 Code = TYPE_ATTRIBUTED;
271}
272
Mike Stump1eb44332009-09-09 15:08:12 +0000273void
Sebastian Redl3397c552010-08-18 23:56:27 +0000274ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000275 const SubstTemplateTypeParmType *T) {
276 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
277 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000278 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000279}
280
281void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000282ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
283 const SubstTemplateTypeParmPackType *T) {
284 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
285 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
286 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
287}
288
289void
Sebastian Redl3397c552010-08-18 23:56:27 +0000290ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000291 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000292 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000293 Writer.AddTemplateName(T->getTemplateName(), Record);
294 Record.push_back(T->getNumArgs());
295 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
296 ArgI != ArgE; ++ArgI)
297 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000298 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
299 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000300 : T->getCanonicalTypeInternal(),
301 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl3397c552010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000307 VisitArrayType(T);
308 Writer.AddStmt(T->getSizeExpr());
309 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000310 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000311}
312
313void
Sebastian Redl3397c552010-08-18 23:56:27 +0000314ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000315 const DependentSizedExtVectorType *T) {
316 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000317 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000318}
319
320void
Sebastian Redl3397c552010-08-18 23:56:27 +0000321ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000322 Record.push_back(T->getDepth());
323 Record.push_back(T->getIndex());
324 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000325 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000326 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000327}
328
329void
Sebastian Redl3397c552010-08-18 23:56:27 +0000330ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000331 Record.push_back(T->getKeyword());
332 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
333 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000334 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
335 : T->getCanonicalTypeInternal(),
336 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000337 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000338}
339
340void
Sebastian Redl3397c552010-08-18 23:56:27 +0000341ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000342 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000343 Record.push_back(T->getKeyword());
344 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
345 Writer.AddIdentifierRef(T->getIdentifier(), Record);
346 Record.push_back(T->getNumArgs());
347 for (DependentTemplateSpecializationType::iterator
348 I = T->begin(), E = T->end(); I != E; ++I)
349 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000350 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000351}
352
Douglas Gregor7536dd52010-12-20 02:24:11 +0000353void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
354 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000355 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
356 Record.push_back(*NumExpansions + 1);
357 else
358 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000359 Code = TYPE_PACK_EXPANSION;
360}
361
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000362void ASTTypeWriter::VisitParenType(const ParenType *T) {
363 Writer.AddTypeRef(T->getInnerType(), Record);
364 Code = TYPE_PAREN;
365}
366
Sebastian Redl3397c552010-08-18 23:56:27 +0000367void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000368 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000369 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
370 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000371 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000372}
373
Sebastian Redl3397c552010-08-18 23:56:27 +0000374void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000375 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000376 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000377 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000378}
379
Sebastian Redl3397c552010-08-18 23:56:27 +0000380void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000381 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000382 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000383}
384
Sebastian Redl3397c552010-08-18 23:56:27 +0000385void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000386 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000387 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000388 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000389 E = T->qual_end(); I != E; ++I)
390 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000391 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000392}
393
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000394void
Sebastian Redl3397c552010-08-18 23:56:27 +0000395ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000396 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000397 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000398}
399
Eli Friedmanb001de72011-10-06 23:00:33 +0000400void
401ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
402 Writer.AddTypeRef(T->getValueType(), Record);
403 Code = TYPE_ATOMIC;
404}
405
John McCalla1ee0c52009-10-16 21:56:05 +0000406namespace {
407
408class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000409 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000410 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000411
412public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000413 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000414 : Writer(Writer), Record(Record) { }
415
John McCall51bd8032009-10-18 01:05:36 +0000416#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000417#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000418 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000419#include "clang/AST/TypeLocNodes.def"
420
John McCall51bd8032009-10-18 01:05:36 +0000421 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
422 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000423};
424
425}
426
John McCall51bd8032009-10-18 01:05:36 +0000427void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
428 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000429}
John McCall51bd8032009-10-18 01:05:36 +0000430void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000431 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
432 if (TL.needsExtraLocalData()) {
433 Record.push_back(TL.getWrittenTypeSpec());
434 Record.push_back(TL.getWrittenSignSpec());
435 Record.push_back(TL.getWrittenWidthSpec());
436 Record.push_back(TL.hasModeAttr());
437 }
John McCalla1ee0c52009-10-16 21:56:05 +0000438}
John McCall51bd8032009-10-18 01:05:36 +0000439void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000441}
John McCall51bd8032009-10-18 01:05:36 +0000442void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000444}
John McCall51bd8032009-10-18 01:05:36 +0000445void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000447}
John McCall51bd8032009-10-18 01:05:36 +0000448void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
449 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000450}
John McCall51bd8032009-10-18 01:05:36 +0000451void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
452 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000453}
John McCall51bd8032009-10-18 01:05:36 +0000454void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000456 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000457}
John McCall51bd8032009-10-18 01:05:36 +0000458void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
460 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
461 Record.push_back(TL.getSizeExpr() ? 1 : 0);
462 if (TL.getSizeExpr())
463 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000464}
John McCall51bd8032009-10-18 01:05:36 +0000465void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
466 VisitArrayTypeLoc(TL);
467}
468void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
469 VisitArrayTypeLoc(TL);
470}
471void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
472 VisitArrayTypeLoc(TL);
473}
474void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
475 DependentSizedArrayTypeLoc TL) {
476 VisitArrayTypeLoc(TL);
477}
478void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
479 DependentSizedExtVectorTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
481}
482void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
484}
485void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
487}
488void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000489 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000490 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
491 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000492 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000493 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
494 Writer.AddDeclRef(TL.getArg(i), Record);
495}
496void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
497 VisitFunctionTypeLoc(TL);
498}
499void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
500 VisitFunctionTypeLoc(TL);
501}
John McCalled976492009-12-04 22:46:56 +0000502void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
503 Writer.AddSourceLocation(TL.getNameLoc(), Record);
504}
John McCall51bd8032009-10-18 01:05:36 +0000505void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getNameLoc(), Record);
507}
508void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000509 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
510 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
511 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000512}
513void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000514 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
515 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
516 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
517 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000518}
519void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getNameLoc(), Record);
521}
Sean Huntca63c202011-05-24 22:41:36 +0000522void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
523 Writer.AddSourceLocation(TL.getKWLoc(), Record);
524 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
525 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
526 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
527}
Richard Smith34b41d92011-02-20 03:19:35 +0000528void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
529 Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
John McCall51bd8032009-10-18 01:05:36 +0000531void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
532 Writer.AddSourceLocation(TL.getNameLoc(), Record);
533}
534void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
John McCall9d156a72011-01-06 01:58:22 +0000537void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
539 if (TL.hasAttrOperand()) {
540 SourceRange range = TL.getAttrOperandParensRange();
541 Writer.AddSourceLocation(range.getBegin(), Record);
542 Writer.AddSourceLocation(range.getEnd(), Record);
543 }
544 if (TL.hasAttrExprOperand()) {
545 Expr *operand = TL.getAttrExprOperand();
546 Record.push_back(operand ? 1 : 0);
547 if (operand) Writer.AddStmt(operand);
548 } else if (TL.hasAttrEnumOperand()) {
549 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
550 }
551}
John McCall51bd8032009-10-18 01:05:36 +0000552void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
553 Writer.AddSourceLocation(TL.getNameLoc(), Record);
554}
John McCall49a832b2009-10-18 09:09:24 +0000555void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
556 SubstTemplateTypeParmTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getNameLoc(), Record);
558}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000559void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
560 SubstTemplateTypeParmPackTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
562}
John McCall51bd8032009-10-18 01:05:36 +0000563void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
564 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000565 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000566 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
567 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
568 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
569 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000570 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
571 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000572}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000573void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
575 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
576}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000577void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000578 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000579 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000580}
John McCall3cb0ebd2010-03-10 03:28:59 +0000581void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583}
Douglas Gregor4714c122010-03-31 17:34:00 +0000584void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000585 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000586 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000587 Writer.AddSourceLocation(TL.getNameLoc(), Record);
588}
John McCall33500952010-06-11 00:33:02 +0000589void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
590 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000591 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000592 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000593 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000594 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000595 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
596 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
597 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000598 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
599 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000600}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000601void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
602 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
603}
John McCall51bd8032009-10-18 01:05:36 +0000604void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
605 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000606}
607void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
608 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000609 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
610 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
611 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
612 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000613}
John McCall54e14c42009-10-22 22:37:11 +0000614void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
615 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000616}
Eli Friedmanb001de72011-10-06 23:00:33 +0000617void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
618 Writer.AddSourceLocation(TL.getKWLoc(), Record);
619 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
620 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
621}
John McCalla1ee0c52009-10-16 21:56:05 +0000622
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000623//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000624// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000625//===----------------------------------------------------------------------===//
626
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000627static void EmitBlockID(unsigned ID, const char *Name,
628 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000629 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000630 Record.clear();
631 Record.push_back(ID);
632 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
633
634 // Emit the block name if present.
635 if (Name == 0 || Name[0] == 0) return;
636 Record.clear();
637 while (*Name)
638 Record.push_back(*Name++);
639 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
640}
641
642static void EmitRecordID(unsigned ID, const char *Name,
643 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000644 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000645 Record.clear();
646 Record.push_back(ID);
647 while (*Name)
648 Record.push_back(*Name++);
649 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000650}
651
652static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000653 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000654#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000655 RECORD(STMT_STOP);
656 RECORD(STMT_NULL_PTR);
657 RECORD(STMT_NULL);
658 RECORD(STMT_COMPOUND);
659 RECORD(STMT_CASE);
660 RECORD(STMT_DEFAULT);
661 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000662 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000663 RECORD(STMT_IF);
664 RECORD(STMT_SWITCH);
665 RECORD(STMT_WHILE);
666 RECORD(STMT_DO);
667 RECORD(STMT_FOR);
668 RECORD(STMT_GOTO);
669 RECORD(STMT_INDIRECT_GOTO);
670 RECORD(STMT_CONTINUE);
671 RECORD(STMT_BREAK);
672 RECORD(STMT_RETURN);
673 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000674 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000675 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000676 RECORD(EXPR_PREDEFINED);
677 RECORD(EXPR_DECL_REF);
678 RECORD(EXPR_INTEGER_LITERAL);
679 RECORD(EXPR_FLOATING_LITERAL);
680 RECORD(EXPR_IMAGINARY_LITERAL);
681 RECORD(EXPR_STRING_LITERAL);
682 RECORD(EXPR_CHARACTER_LITERAL);
683 RECORD(EXPR_PAREN);
684 RECORD(EXPR_UNARY_OPERATOR);
685 RECORD(EXPR_SIZEOF_ALIGN_OF);
686 RECORD(EXPR_ARRAY_SUBSCRIPT);
687 RECORD(EXPR_CALL);
688 RECORD(EXPR_MEMBER);
689 RECORD(EXPR_BINARY_OPERATOR);
690 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
691 RECORD(EXPR_CONDITIONAL_OPERATOR);
692 RECORD(EXPR_IMPLICIT_CAST);
693 RECORD(EXPR_CSTYLE_CAST);
694 RECORD(EXPR_COMPOUND_LITERAL);
695 RECORD(EXPR_EXT_VECTOR_ELEMENT);
696 RECORD(EXPR_INIT_LIST);
697 RECORD(EXPR_DESIGNATED_INIT);
698 RECORD(EXPR_IMPLICIT_VALUE_INIT);
699 RECORD(EXPR_VA_ARG);
700 RECORD(EXPR_ADDR_LABEL);
701 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000702 RECORD(EXPR_CHOOSE);
703 RECORD(EXPR_GNU_NULL);
704 RECORD(EXPR_SHUFFLE_VECTOR);
705 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000706 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000707 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000708 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000709 RECORD(EXPR_OBJC_ARRAY_LITERAL);
710 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000711 RECORD(EXPR_OBJC_ENCODE);
712 RECORD(EXPR_OBJC_SELECTOR_EXPR);
713 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
714 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
715 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
716 RECORD(EXPR_OBJC_KVC_REF_EXPR);
717 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000718 RECORD(STMT_OBJC_FOR_COLLECTION);
719 RECORD(STMT_OBJC_CATCH);
720 RECORD(STMT_OBJC_FINALLY);
721 RECORD(STMT_OBJC_AT_TRY);
722 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
723 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000724 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000725 RECORD(EXPR_CXX_OPERATOR_CALL);
726 RECORD(EXPR_CXX_CONSTRUCT);
727 RECORD(EXPR_CXX_STATIC_CAST);
728 RECORD(EXPR_CXX_DYNAMIC_CAST);
729 RECORD(EXPR_CXX_REINTERPRET_CAST);
730 RECORD(EXPR_CXX_CONST_CAST);
731 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000732 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000733 RECORD(EXPR_CXX_BOOL_LITERAL);
734 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000735 RECORD(EXPR_CXX_TYPEID_EXPR);
736 RECORD(EXPR_CXX_TYPEID_TYPE);
737 RECORD(EXPR_CXX_UUIDOF_EXPR);
738 RECORD(EXPR_CXX_UUIDOF_TYPE);
739 RECORD(EXPR_CXX_THIS);
740 RECORD(EXPR_CXX_THROW);
741 RECORD(EXPR_CXX_DEFAULT_ARG);
742 RECORD(EXPR_CXX_BIND_TEMPORARY);
743 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
744 RECORD(EXPR_CXX_NEW);
745 RECORD(EXPR_CXX_DELETE);
746 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
747 RECORD(EXPR_EXPR_WITH_CLEANUPS);
748 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
749 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
750 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
751 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
752 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
753 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
754 RECORD(EXPR_CXX_NOEXCEPT);
755 RECORD(EXPR_OPAQUE_VALUE);
756 RECORD(EXPR_BINARY_TYPE_TRAIT);
757 RECORD(EXPR_PACK_EXPANSION);
758 RECORD(EXPR_SIZEOF_PACK);
759 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000760 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000761#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000762}
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Sebastian Redla4232eb2010-08-18 23:56:21 +0000764void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000765 RecordData Record;
766 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000768#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
769#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000771 // Control Block.
772 BLOCK(CONTROL_BLOCK);
773 RECORD(METADATA);
774 RECORD(IMPORTS);
775 RECORD(LANGUAGE_OPTIONS);
776 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000777 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000778 RECORD(ORIGINAL_PCH_DIR);
Douglas Gregora930dc92012-10-22 18:42:04 +0000779 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000780 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000781 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000782 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000783
Douglas Gregorc337fef2012-10-19 00:45:00 +0000784 BLOCK(INPUT_FILES_BLOCK);
785 RECORD(INPUT_FILE);
786
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000787 // AST Top-Level Block.
788 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000789 RECORD(TYPE_OFFSET);
790 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000791 RECORD(IDENTIFIER_OFFSET);
792 RECORD(IDENTIFIER_TABLE);
793 RECORD(EXTERNAL_DEFINITIONS);
794 RECORD(SPECIAL_TYPES);
795 RECORD(STATISTICS);
796 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000797 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000798 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
799 RECORD(SELECTOR_OFFSETS);
800 RECORD(METHOD_POOL);
801 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000802 RECORD(SOURCE_LOCATION_OFFSETS);
803 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000804 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000805 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000806 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000807 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000808 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000809 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000810 RECORD(SEMA_DECL_REFS);
811 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
812 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
813 RECORD(DECL_REPLACEMENTS);
814 RECORD(UPDATE_VISIBLE);
815 RECORD(DECL_UPDATE_OFFSETS);
816 RECORD(DECL_UPDATES);
817 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
818 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000819 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000820 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000821 RECORD(FP_PRAGMA_OPTIONS);
822 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000823 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000824 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000825 RECORD(MODULE_OFFSET_MAP);
826 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000827 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000828 RECORD(FILE_SORTED_DECLS);
829 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000830 RECORD(MERGED_DECLARATIONS);
831 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000832 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000833 RECORD(MACRO_OFFSET);
834 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000835
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000836 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000837 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000838 RECORD(SM_SLOC_FILE_ENTRY);
839 RECORD(SM_SLOC_BUFFER_ENTRY);
840 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000841 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000843 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000844 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000845 RECORD(PP_MACRO_OBJECT_LIKE);
846 RECORD(PP_MACRO_FUNCTION_LIKE);
847 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000848
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000849 // Decls and Types block.
850 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000851 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000852 RECORD(TYPE_COMPLEX);
853 RECORD(TYPE_POINTER);
854 RECORD(TYPE_BLOCK_POINTER);
855 RECORD(TYPE_LVALUE_REFERENCE);
856 RECORD(TYPE_RVALUE_REFERENCE);
857 RECORD(TYPE_MEMBER_POINTER);
858 RECORD(TYPE_CONSTANT_ARRAY);
859 RECORD(TYPE_INCOMPLETE_ARRAY);
860 RECORD(TYPE_VARIABLE_ARRAY);
861 RECORD(TYPE_VECTOR);
862 RECORD(TYPE_EXT_VECTOR);
863 RECORD(TYPE_FUNCTION_PROTO);
864 RECORD(TYPE_FUNCTION_NO_PROTO);
865 RECORD(TYPE_TYPEDEF);
866 RECORD(TYPE_TYPEOF_EXPR);
867 RECORD(TYPE_TYPEOF);
868 RECORD(TYPE_RECORD);
869 RECORD(TYPE_ENUM);
870 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000871 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000872 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000873 RECORD(TYPE_DECLTYPE);
874 RECORD(TYPE_ELABORATED);
875 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
876 RECORD(TYPE_UNRESOLVED_USING);
877 RECORD(TYPE_INJECTED_CLASS_NAME);
878 RECORD(TYPE_OBJC_OBJECT);
879 RECORD(TYPE_TEMPLATE_TYPE_PARM);
880 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
881 RECORD(TYPE_DEPENDENT_NAME);
882 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
883 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
884 RECORD(TYPE_PAREN);
885 RECORD(TYPE_PACK_EXPANSION);
886 RECORD(TYPE_ATTRIBUTED);
887 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000888 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000889 RECORD(DECL_TYPEDEF);
890 RECORD(DECL_ENUM);
891 RECORD(DECL_RECORD);
892 RECORD(DECL_ENUM_CONSTANT);
893 RECORD(DECL_FUNCTION);
894 RECORD(DECL_OBJC_METHOD);
895 RECORD(DECL_OBJC_INTERFACE);
896 RECORD(DECL_OBJC_PROTOCOL);
897 RECORD(DECL_OBJC_IVAR);
898 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000899 RECORD(DECL_OBJC_CATEGORY);
900 RECORD(DECL_OBJC_CATEGORY_IMPL);
901 RECORD(DECL_OBJC_IMPLEMENTATION);
902 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
903 RECORD(DECL_OBJC_PROPERTY);
904 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000905 RECORD(DECL_FIELD);
906 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000907 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000908 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000909 RECORD(DECL_FILE_SCOPE_ASM);
910 RECORD(DECL_BLOCK);
911 RECORD(DECL_CONTEXT_LEXICAL);
912 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000913 RECORD(DECL_NAMESPACE);
914 RECORD(DECL_NAMESPACE_ALIAS);
915 RECORD(DECL_USING);
916 RECORD(DECL_USING_SHADOW);
917 RECORD(DECL_USING_DIRECTIVE);
918 RECORD(DECL_UNRESOLVED_USING_VALUE);
919 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
920 RECORD(DECL_LINKAGE_SPEC);
921 RECORD(DECL_CXX_RECORD);
922 RECORD(DECL_CXX_METHOD);
923 RECORD(DECL_CXX_CONSTRUCTOR);
924 RECORD(DECL_CXX_DESTRUCTOR);
925 RECORD(DECL_CXX_CONVERSION);
926 RECORD(DECL_ACCESS_SPEC);
927 RECORD(DECL_FRIEND);
928 RECORD(DECL_FRIEND_TEMPLATE);
929 RECORD(DECL_CLASS_TEMPLATE);
930 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
931 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
932 RECORD(DECL_FUNCTION_TEMPLATE);
933 RECORD(DECL_TEMPLATE_TYPE_PARM);
934 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
935 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
936 RECORD(DECL_STATIC_ASSERT);
937 RECORD(DECL_CXX_BASE_SPECIFIERS);
938 RECORD(DECL_INDIRECTFIELD);
939 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
940
Douglas Gregora72d8c42011-06-03 02:27:19 +0000941 // Statements and Exprs can occur in the Decls and Types block.
942 AddStmtsExprs(Stream, Record);
943
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000944 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000945 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000946 RECORD(PPD_MACRO_DEFINITION);
947 RECORD(PPD_INCLUSION_DIRECTIVE);
948
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000949#undef RECORD
950#undef BLOCK
951 Stream.ExitBlock();
952}
953
Douglas Gregore650c8c2009-07-07 00:12:59 +0000954/// \brief Adjusts the given filename to only write out the portion of the
955/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000956///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957/// \param Filename the file name to adjust.
958///
959/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
960/// the returned filename will be adjusted by this system root.
961///
962/// \returns either the original filename (if it needs no adjustment) or the
963/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000964static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000965adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000966 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Douglas Gregor832d6202011-07-22 16:35:34 +0000968 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971 // Verify that the filename and the system root have the same prefix.
972 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000973 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974 if (Filename[Pos] != isysroot[Pos])
975 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 // We hit the end of the filename before we hit the end of the system root.
978 if (!Filename[Pos])
979 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 // If the file name has a '/' at the current position, skip over the '/'.
982 // We distinguish sysroot-based includes from absolute includes by the
983 // absence of '/' at the beginning of sysroot-based includes.
984 if (Filename[Pos] == '/')
985 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Douglas Gregore650c8c2009-07-07 00:12:59 +0000987 return Filename + Pos;
988}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000989
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000990/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000991void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
992 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000993 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000994 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000995 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
996 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000997
Douglas Gregore650c8c2009-07-07 00:12:59 +0000998 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000999 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1000 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1001 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1002 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1003 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1008 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1009 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001010 Record.push_back(VERSION_MAJOR);
1011 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001012 Record.push_back(CLANG_VERSION_MAJOR);
1013 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001014 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001015 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001016 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1017 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001018
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001019 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001020 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001021 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1022 llvm::SmallVector<char, 128> ModulePaths;
1023 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001024
1025 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1026 M != MEnd; ++M) {
1027 // Skip modules that weren't directly imported.
1028 if (!(*M)->isDirectlyImported())
1029 continue;
1030
1031 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1032 // FIXME: Write import location, once it matters.
1033 // FIXME: This writes the absolute path for AST files we depend on.
1034 const std::string &FileName = (*M)->FileName;
1035 Record.push_back(FileName.size());
1036 Record.append(FileName.begin(), FileName.end());
1037 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001038 Stream.EmitRecord(IMPORTS, Record);
1039 }
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001041 // Language options.
1042 Record.clear();
1043 const LangOptions &LangOpts = Context.getLangOpts();
1044#define LANGOPT(Name, Bits, Default, Description) \
1045 Record.push_back(LangOpts.Name);
1046#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1047 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1048#include "clang/Basic/LangOptions.def"
1049
1050 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1051 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1052
1053 Record.push_back(LangOpts.CurrentModule.size());
1054 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1055 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1056
Douglas Gregoree097c12012-10-18 17:58:09 +00001057 // Target options.
1058 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001059 const TargetInfo &Target = Context.getTargetInfo();
1060 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001061 AddString(TargetOpts.Triple, Record);
1062 AddString(TargetOpts.CPU, Record);
1063 AddString(TargetOpts.ABI, Record);
1064 AddString(TargetOpts.CXXABI, Record);
1065 AddString(TargetOpts.LinkerVersion, Record);
1066 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1067 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1068 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1069 }
1070 Record.push_back(TargetOpts.Features.size());
1071 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1072 AddString(TargetOpts.Features[I], Record);
1073 }
1074 Stream.EmitRecord(TARGET_OPTIONS, Record);
1075
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001076 // Diagnostic options.
1077 Record.clear();
1078 const DiagnosticOptions &DiagOpts
1079 = Context.getDiagnostics().getDiagnosticOptions();
1080#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1081#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1082 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1083#include "clang/Basic/DiagnosticOptions.def"
1084 Record.push_back(DiagOpts.Warnings.size());
1085 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1086 AddString(DiagOpts.Warnings[I], Record);
1087 // Note: we don't serialize the log or serialization file names, because they
1088 // are generally transient files and will almost always be overridden.
1089 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1090
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001091 // File system options.
1092 Record.clear();
1093 const FileSystemOptions &FSOpts
1094 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1095 AddString(FSOpts.WorkingDir, Record);
1096 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1097
Douglas Gregorbbf38312012-10-24 16:50:34 +00001098 // Header search options.
1099 Record.clear();
1100 const HeaderSearchOptions &HSOpts
1101 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1102 AddString(HSOpts.Sysroot, Record);
1103
1104 // Include entries.
1105 Record.push_back(HSOpts.UserEntries.size());
1106 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1107 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1108 AddString(Entry.Path, Record);
1109 Record.push_back(static_cast<unsigned>(Entry.Group));
1110 Record.push_back(Entry.IsUserSupplied);
1111 Record.push_back(Entry.IsFramework);
1112 Record.push_back(Entry.IgnoreSysRoot);
1113 Record.push_back(Entry.IsInternal);
1114 Record.push_back(Entry.ImplicitExternC);
1115 }
1116
1117 // System header prefixes.
1118 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1119 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1120 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1121 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1122 }
1123
1124 AddString(HSOpts.ResourceDir, Record);
1125 AddString(HSOpts.ModuleCachePath, Record);
1126 Record.push_back(HSOpts.DisableModuleHash);
1127 Record.push_back(HSOpts.UseBuiltinIncludes);
1128 Record.push_back(HSOpts.UseStandardSystemIncludes);
1129 Record.push_back(HSOpts.UseStandardCXXIncludes);
1130 Record.push_back(HSOpts.UseLibcxx);
1131 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1132
Douglas Gregor31d375f2011-05-06 21:43:30 +00001133 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001134 SourceManager &SM = Context.getSourceManager();
1135 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1136 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001137 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1138 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001139 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1140 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1141
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001142 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001144 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001145
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001146 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001147 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001148 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001149 RecordData Record;
Douglas Gregor39c497b2012-10-18 18:36:53 +00001150 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001151 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001152 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
1153 Record.clear();
Douglas Gregorb64c1932009-05-12 01:31:05 +00001154 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001155
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001156 // Original PCH directory
1157 if (!OutputFile.empty() && OutputFile != "-") {
1158 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1159 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1161 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1162
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001163 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001164
1165 llvm::sys::fs::make_absolute(OutputPath);
1166 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1167
1168 RecordData Record;
1169 Record.push_back(ORIGINAL_PCH_DIR);
1170 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1171 }
1172
Douglas Gregor745e6f12012-10-19 00:38:02 +00001173 WriteInputFiles(Context.SourceMgr, isysroot);
1174 Stream.ExitBlock();
1175}
1176
1177void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1178 using namespace llvm;
1179 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1180 RecordData Record;
1181
1182 // Create input-file abbreviation.
1183 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1184 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001185 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001186 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1187 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001188 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001189 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1190 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1191
1192 // Write out all of the input files.
1193 std::vector<uint32_t> InputFileOffsets;
1194 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1195 // Get this source location entry.
1196 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001197 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001198
1199 // We only care about file entries that were not overridden.
1200 if (!SLoc->isFile())
1201 continue;
1202 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001203 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001204 continue;
1205
Douglas Gregora930dc92012-10-22 18:42:04 +00001206 // Record this entry's offset.
1207 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1208 InputFileIDs[Cache->OrigEntry] = InputFileOffsets.size();
1209
Douglas Gregor745e6f12012-10-19 00:38:02 +00001210 Record.clear();
1211 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001212 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001213
1214 // Emit size/modification time for this file.
1215 Record.push_back(Cache->OrigEntry->getSize());
1216 Record.push_back(Cache->OrigEntry->getModificationTime());
1217
Douglas Gregora930dc92012-10-22 18:42:04 +00001218 // Whether this file was overridden.
1219 Record.push_back(Cache->BufferOverridden);
1220
Douglas Gregor745e6f12012-10-19 00:38:02 +00001221 // Turn the file name into an absolute path, if it isn't already.
1222 const char *Filename = Cache->OrigEntry->getName();
1223 SmallString<128> FilePath(Filename);
1224
1225 // Ask the file manager to fixup the relative path for us. This will
1226 // honor the working directory.
1227 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1228
1229 // FIXME: This call to make_absolute shouldn't be necessary, the
1230 // call to FixupRelativePath should always return an absolute path.
1231 llvm::sys::fs::make_absolute(FilePath);
1232 Filename = FilePath.c_str();
1233
1234 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1235
1236 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1237 }
1238
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001239 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001240
1241 // Create input file offsets abbreviation.
1242 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1243 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1244 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1245 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1246 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1247
1248 // Write input file offsets.
1249 Record.clear();
1250 Record.push_back(INPUT_FILE_OFFSETS);
1251 Record.push_back(InputFileOffsets.size());
1252 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001253}
1254
Douglas Gregor14f79002009-04-10 03:52:48 +00001255//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001256// stat cache Serialization
1257//===----------------------------------------------------------------------===//
1258
1259namespace {
1260// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001261class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001262public:
1263 typedef const char * key_type;
1264 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Chris Lattner74e976b2010-11-23 19:28:12 +00001266 typedef struct stat data_type;
1267 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001268
1269 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001270 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001271 }
Mike Stump1eb44332009-09-09 15:08:12 +00001272
1273 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001274 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001275 data_type_ref Data) {
1276 unsigned StrLen = strlen(path);
1277 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001278 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001279 clang::io::Emit8(Out, DataLen);
1280 return std::make_pair(StrLen + 1, DataLen);
1281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Chris Lattner5f9e2722011-07-23 10:55:15 +00001283 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001284 Out.write(path, KeyLen);
1285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Chris Lattner5f9e2722011-07-23 10:55:15 +00001287 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001288 data_type_ref Data, unsigned DataLen) {
1289 using namespace clang::io;
1290 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattner74e976b2010-11-23 19:28:12 +00001292 Emit32(Out, (uint32_t) Data.st_ino);
1293 Emit32(Out, (uint32_t) Data.st_dev);
1294 Emit16(Out, (uint16_t) Data.st_mode);
1295 Emit64(Out, (uint64_t) Data.st_mtime);
1296 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001297
1298 assert(Out.tell() - Start == DataLen && "Wrong data length");
1299 }
1300};
1301} // end anonymous namespace
1302
Sebastian Redl3397c552010-08-18 23:56:27 +00001303/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001304void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001305 // Build the on-disk hash table containing information about every
1306 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001307 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001308 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001309 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001310 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001311 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001312 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001313 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001314 }
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001316 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001317 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001318 uint32_t BucketOffset;
1319 {
1320 llvm::raw_svector_ostream Out(StatCacheData);
1321 // Make sure that no bucket is at offset 0
1322 clang::io::Emit32(Out, 0);
1323 BucketOffset = Generator.Emit(Out);
1324 }
1325
1326 // Create a blob abbreviation
1327 using namespace llvm;
1328 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001329 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1333 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1334
1335 // Write the stat cache
1336 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001337 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001338 Record.push_back(BucketOffset);
1339 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001340 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001341}
1342
1343//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001344// Source Manager Serialization
1345//===----------------------------------------------------------------------===//
1346
1347/// \brief Create an abbreviation for the SLocEntry that refers to a
1348/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001349static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001350 using namespace llvm;
1351 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001352 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001357 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1361 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001362 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001363}
1364
1365/// \brief Create an abbreviation for the SLocEntry that refers to a
1366/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001367static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001368 using namespace llvm;
1369 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001370 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001376 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001377}
1378
1379/// \brief Create an abbreviation for the SLocEntry that refers to a
1380/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001381static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001382 using namespace llvm;
1383 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001384 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001386 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001387}
1388
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001389/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1390/// expansion.
1391static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001392 using namespace llvm;
1393 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001394 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001400 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001401}
1402
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001403namespace {
1404 // Trait used for the on-disk hash table of header search information.
1405 class HeaderFileInfoTrait {
1406 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001407
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001408 // Keep track of the framework names we've used during serialization.
1409 SmallVector<char, 128> FrameworkStringData;
1410 llvm::StringMap<unsigned> FrameworkNameOffset;
1411
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001412 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001413 HeaderFileInfoTrait(ASTWriter &Writer)
1414 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001415
1416 typedef const char *key_type;
1417 typedef key_type key_type_ref;
1418
1419 typedef HeaderFileInfo data_type;
1420 typedef const data_type &data_type_ref;
1421
1422 static unsigned ComputeHash(const char *path) {
1423 // The hash is based only on the filename portion of the key, so that the
1424 // reader can match based on filenames when symlinking or excess path
1425 // elements ("foo/../", "../") change the form of the name. However,
1426 // complete path is still the key.
1427 return llvm::HashString(llvm::sys::path::filename(path));
1428 }
1429
1430 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001431 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001432 data_type_ref Data) {
1433 unsigned StrLen = strlen(path);
1434 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001435 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001436 clang::io::Emit8(Out, DataLen);
1437 return std::make_pair(StrLen + 1, DataLen);
1438 }
1439
Chris Lattner5f9e2722011-07-23 10:55:15 +00001440 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001441 Out.write(path, KeyLen);
1442 }
1443
Chris Lattner5f9e2722011-07-23 10:55:15 +00001444 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001445 data_type_ref Data, unsigned DataLen) {
1446 using namespace clang::io;
1447 uint64_t Start = Out.tell(); (void)Start;
1448
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001449 unsigned char Flags = (Data.isImport << 5)
1450 | (Data.isPragmaOnce << 4)
1451 | (Data.DirInfo << 2)
1452 | (Data.Resolved << 1)
1453 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001454 Emit8(Out, (uint8_t)Flags);
1455 Emit16(Out, (uint16_t) Data.NumIncludes);
1456
1457 if (!Data.ControllingMacro)
1458 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1459 else
1460 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001461
1462 unsigned Offset = 0;
1463 if (!Data.Framework.empty()) {
1464 // If this header refers into a framework, save the framework name.
1465 llvm::StringMap<unsigned>::iterator Pos
1466 = FrameworkNameOffset.find(Data.Framework);
1467 if (Pos == FrameworkNameOffset.end()) {
1468 Offset = FrameworkStringData.size() + 1;
1469 FrameworkStringData.append(Data.Framework.begin(),
1470 Data.Framework.end());
1471 FrameworkStringData.push_back(0);
1472
1473 FrameworkNameOffset[Data.Framework] = Offset;
1474 } else
1475 Offset = Pos->second;
1476 }
1477 Emit32(Out, Offset);
1478
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001479 assert(Out.tell() - Start == DataLen && "Wrong data length");
1480 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001481
1482 const char *strings_begin() const { return FrameworkStringData.begin(); }
1483 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001484 };
1485} // end anonymous namespace
1486
1487/// \brief Write the header search block for the list of files that
1488///
1489/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001490void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001491 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001492 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1493
1494 if (FilesByUID.size() > HS.header_file_size())
1495 FilesByUID.resize(HS.header_file_size());
1496
Benjamin Kramerfacde172012-06-06 17:32:50 +00001497 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001498 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001499 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001500 unsigned NumHeaderSearchEntries = 0;
1501 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1502 const FileEntry *File = FilesByUID[UID];
1503 if (!File)
1504 continue;
1505
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001506 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1507 // from the external source if it was not provided already.
1508 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001509 if (HFI.External && Chain)
1510 continue;
1511
1512 // Turn the file name into an absolute path, if it isn't already.
1513 const char *Filename = File->getName();
1514 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1515
1516 // If we performed any translation on the file name at all, we need to
1517 // save this string, since the generator will refer to it later.
1518 if (Filename != File->getName()) {
1519 Filename = strdup(Filename);
1520 SavedStrings.push_back(Filename);
1521 }
1522
1523 Generator.insert(Filename, HFI, GeneratorTrait);
1524 ++NumHeaderSearchEntries;
1525 }
1526
1527 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001528 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001529 uint32_t BucketOffset;
1530 {
1531 llvm::raw_svector_ostream Out(TableData);
1532 // Make sure that no bucket is at offset 0
1533 clang::io::Emit32(Out, 0);
1534 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1535 }
1536
1537 // Create a blob abbreviation
1538 using namespace llvm;
1539 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1540 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1545 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1546
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001547 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001548 RecordData Record;
1549 Record.push_back(HEADER_SEARCH_TABLE);
1550 Record.push_back(BucketOffset);
1551 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001552 Record.push_back(TableData.size());
1553 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001554 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1555
1556 // Free all of the strings we had to duplicate.
1557 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1558 free((void*)SavedStrings[I]);
1559}
1560
Douglas Gregor14f79002009-04-10 03:52:48 +00001561/// \brief Writes the block containing the serialized form of the
1562/// source manager.
1563///
1564/// TODO: We should probably use an on-disk hash table (stored in a
1565/// blob), indexed based on the file name, so that we only create
1566/// entries for files that we actually need. In the common case (no
1567/// errors), we probably won't have to create file entries for any of
1568/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001569void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001570 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001571 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001572 RecordData Record;
1573
Chris Lattnerf04ad692009-04-10 17:16:57 +00001574 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001575 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001576
1577 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001578 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1579 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1580 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001581 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001582
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001583 // Write out the source location entry table. We skip the first
1584 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001585 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001586 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001587 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1588 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001589 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001590 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001591 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001592 FileID FID = FileID::get(I);
1593 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001594
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001595 // Record the offset of this source-location entry.
1596 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1597
1598 // Figure out which record code to use.
1599 unsigned Code;
1600 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001601 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1602 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001603 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001604 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001605 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001606 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001607 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001608 Record.clear();
1609 Record.push_back(Code);
1610
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001611 // Starting offset of this entry within this module, so skip the dummy.
1612 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001613 if (SLoc->isFile()) {
1614 const SrcMgr::FileInfo &File = SLoc->getFile();
1615 Record.push_back(File.getIncludeLoc().getRawEncoding());
1616 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1617 Record.push_back(File.hasLineDirectives());
1618
1619 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001620 if (Content->OrigEntry) {
1621 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001622 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001623
Douglas Gregora930dc92012-10-22 18:42:04 +00001624 // The source location entry is a file. Emit input file ID.
1625 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1626 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001628 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001629
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001630 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001631 if (FDI != FileDeclIDs.end()) {
1632 Record.push_back(FDI->second->FirstDeclIndex);
1633 Record.push_back(FDI->second->DeclIDs.size());
1634 } else {
1635 Record.push_back(0);
1636 Record.push_back(0);
1637 }
Douglas Gregora081da52011-11-16 20:05:18 +00001638
Douglas Gregora930dc92012-10-22 18:42:04 +00001639 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001640
1641 if (Content->BufferOverridden) {
1642 Record.clear();
1643 Record.push_back(SM_SLOC_BUFFER_BLOB);
1644 const llvm::MemoryBuffer *Buffer
1645 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1646 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1647 StringRef(Buffer->getBufferStart(),
1648 Buffer->getBufferSize() + 1));
1649 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001650 } else {
1651 // The source location entry is a buffer. The blob associated
1652 // with this entry contains the contents of the buffer.
1653
1654 // We add one to the size so that we capture the trailing NULL
1655 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1656 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001657 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001658 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001659 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001660 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001661 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001662 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001663 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001664 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001665 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001666 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001667
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001668 if (strcmp(Name, "<built-in>") == 0) {
1669 PreloadSLocs.push_back(SLocEntryOffsets.size());
1670 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001671 }
1672 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001673 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001674 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001675 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1676 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001677 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1678 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001679
1680 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001681 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001682 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001683 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001684 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001685 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001686 }
1687 }
1688
Douglas Gregorc9490c02009-04-16 22:23:12 +00001689 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001690
1691 if (SLocEntryOffsets.empty())
1692 return;
1693
Sebastian Redl3397c552010-08-18 23:56:27 +00001694 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001695 // table is used for lazily loading source-location information.
1696 using namespace llvm;
1697 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001698 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001701 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1702 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001704 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001705 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001706 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001707 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001708 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001709
Sebastian Redl3397c552010-08-18 23:56:27 +00001710 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001711 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001712 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001713
1714 // Write the line table. It depends on remapping working, so it must come
1715 // after the source location offsets.
1716 if (SourceMgr.hasLineTable()) {
1717 LineTableInfo &LineTable = SourceMgr.getLineTable();
1718
1719 Record.clear();
1720 // Emit the file names
1721 Record.push_back(LineTable.getNumFilenames());
1722 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1723 // Emit the file name
1724 const char *Filename = LineTable.getFilename(I);
1725 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1726 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1727 Record.push_back(FilenameLen);
1728 if (FilenameLen)
1729 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1730 }
1731
1732 // Emit the line entries
1733 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1734 L != LEnd; ++L) {
1735 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001736 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001737 continue;
1738
1739 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001740 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001741
1742 // Emit the line entries
1743 Record.push_back(L->second.size());
1744 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1745 LEEnd = L->second.end();
1746 LE != LEEnd; ++LE) {
1747 Record.push_back(LE->FileOffset);
1748 Record.push_back(LE->LineNo);
1749 Record.push_back(LE->FilenameID);
1750 Record.push_back((unsigned)LE->FileKind);
1751 Record.push_back(LE->IncludeOffset);
1752 }
1753 }
1754 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1755 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001756}
1757
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001758//===----------------------------------------------------------------------===//
1759// Preprocessor Serialization
1760//===----------------------------------------------------------------------===//
1761
Douglas Gregor9c736102011-02-10 18:20:09 +00001762static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1763 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1764 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1765 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1766 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1767 return X.first->getName().compare(Y.first->getName());
1768}
1769
Chris Lattner0b1fb982009-04-10 17:15:23 +00001770/// \brief Writes the block containing the serialized form of the
1771/// preprocessor.
1772///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001773void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001774 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1775 if (PPRec)
1776 WritePreprocessorDetail(*PPRec);
1777
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001778 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001779
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001780 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1781 if (PP.getCounterValue() != 0) {
1782 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001783 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001784 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001785 }
1786
1787 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001788 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Sebastian Redl3397c552010-08-18 23:56:27 +00001790 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001791 // FIXME: use diagnostics subsystem for localization etc.
1792 if (PP.SawDateOrTime())
1793 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Douglas Gregorecdcb882010-10-20 22:00:55 +00001795
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001796 // Loop over all the macro definitions that are live at the end of the file,
1797 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001798
Douglas Gregor9c736102011-02-10 18:20:09 +00001799 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001800 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001801 MacrosToEmit;
1802 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001803 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001804 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001805 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001806 if (!IsModule || I->second->isPublic()) {
1807 MacroDefinitionsSeen.insert(I->first);
1808 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001809 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001810 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001811
Douglas Gregor9c736102011-02-10 18:20:09 +00001812 // Sort the set of macro definitions that need to be serialized by the
1813 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001814 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001815 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001816
Douglas Gregora8235d62012-10-09 23:05:51 +00001817 /// \brief Offsets of each of the macros into the bitstream, indexed by
1818 /// the local macro ID
1819 ///
1820 /// For each identifier that is associated with a macro, this map
1821 /// provides the offset into the bitstream where that macro is
1822 /// defined.
1823 std::vector<uint32_t> MacroOffsets;
1824
Douglas Gregor9c736102011-02-10 18:20:09 +00001825 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1826 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001827
Douglas Gregora8235d62012-10-09 23:05:51 +00001828 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1829 MI = MI->getPreviousDefinition()) {
1830 MacroID ID = getMacroRef(MI);
1831 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001832 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Douglas Gregora8235d62012-10-09 23:05:51 +00001834 // Skip macros from a AST file if we're chaining.
1835 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1836 continue;
1837
1838 if (ID < FirstMacroID) {
1839 // This will have been dealt with via an update record.
1840 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1841 continue;
1842 }
1843
1844 // Record the local offset of this macro.
1845 unsigned Index = ID - FirstMacroID;
1846 if (Index == MacroOffsets.size())
1847 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1848 else {
1849 if (Index > MacroOffsets.size())
1850 MacroOffsets.resize(Index + 1);
1851
1852 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1853 }
1854
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001855 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001856 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001857 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001858 AddSourceLocation(MI->getDefinitionLoc(), Record);
1859 AddSourceLocation(MI->getUndefLoc(), Record);
1860 Record.push_back(MI->isUsed());
1861 Record.push_back(MI->isPublic());
1862 AddSourceLocation(MI->getVisibilityLocation(), Record);
1863 unsigned Code;
1864 if (MI->isObjectLike()) {
1865 Code = PP_MACRO_OBJECT_LIKE;
1866 } else {
1867 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001868
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001869 Record.push_back(MI->isC99Varargs());
1870 Record.push_back(MI->isGNUVarargs());
1871 Record.push_back(MI->getNumArgs());
1872 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1873 I != E; ++I)
1874 AddIdentifierRef(*I, Record);
1875 }
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001877 // If we have a detailed preprocessing record, record the macro definition
1878 // ID that corresponds to this macro.
1879 if (PPRec)
1880 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1881
1882 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001883 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001884
1885 // Emit the tokens array.
1886 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1887 // Note that we know that the preprocessor does not have any annotation
1888 // tokens in it because they are created by the parser, and thus can't
1889 // be in a macro definition.
1890 const Token &Tok = MI->getReplacementToken(TokNo);
1891
1892 Record.push_back(Tok.getLocation().getRawEncoding());
1893 Record.push_back(Tok.getLength());
1894
1895 // FIXME: When reading literal tokens, reconstruct the literal pointer
1896 // if it is needed.
1897 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1898 // FIXME: Should translate token kind to a stable encoding.
1899 Record.push_back(Tok.getKind());
1900 // FIXME: Should translate token flags to a stable encoding.
1901 Record.push_back(Tok.getFlags());
1902
1903 Stream.EmitRecord(PP_TOKEN, Record);
1904 Record.clear();
1905 }
1906 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001907 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001908 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001909 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001910
1911 // Write the offsets table for macro IDs.
1912 using namespace llvm;
1913 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1914 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1918
1919 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1920 Record.clear();
1921 Record.push_back(MACRO_OFFSET);
1922 Record.push_back(MacroOffsets.size());
1923 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1924 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1925 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001926}
1927
1928void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001929 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001930 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001931
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001932 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001933
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001934 // Enter the preprocessor block.
1935 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001936
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001937 // If the preprocessor has a preprocessing record, emit it.
1938 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001939 using namespace llvm;
1940
1941 // Set up the abbreviation for
1942 unsigned InclusionAbbrev = 0;
1943 {
1944 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1945 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1951 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1952 }
1953
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001954 unsigned FirstPreprocessorEntityID
1955 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1956 + NUM_PREDEF_PP_ENTITY_IDS;
1957 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001958 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001959 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1960 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001961 E != EEnd;
1962 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001963 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001964
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001965 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1966 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001967
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001968 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001969 // Record this macro definition's ID.
1970 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001971
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001972 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001973 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1974 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001975 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001976
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001977 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001978 Record.push_back(ME->isBuiltinMacro());
1979 if (ME->isBuiltinMacro())
1980 AddIdentifierRef(ME->getName(), Record);
1981 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001982 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001983 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001984 continue;
1985 }
1986
1987 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1988 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001989 Record.push_back(ID->getFileName().size());
1990 Record.push_back(ID->wasInQuotes());
1991 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001992 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001993 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001994 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001995 // Check that the FileEntry is not null because it was not resolved and
1996 // we create a PCH even with compiler errors.
1997 if (ID->getFile())
1998 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001999 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2000 continue;
2001 }
2002
2003 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2004 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002005 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002006
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002007 // Write the offsets table for the preprocessing record.
2008 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002009 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2010
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002011 // Write the offsets table for identifier IDs.
2012 using namespace llvm;
2013 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002014 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002017 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002018
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002019 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002020 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002021 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002022 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2023 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002024 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002025}
2026
Douglas Gregore209e502011-12-06 01:10:29 +00002027unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2028 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2029 if (Known != SubmoduleIDs.end())
2030 return Known->second;
2031
2032 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2033}
2034
Douglas Gregor26ced122011-12-01 00:59:36 +00002035/// \brief Compute the number of modules within the given tree (including the
2036/// given module).
2037static unsigned getNumberOfModules(Module *Mod) {
2038 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002039 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2040 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002041 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002042 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002043
2044 return ChildModules + 1;
2045}
2046
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002047void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002048 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002049 // FIXME: This feels like it belongs somewhere else, but there are no
2050 // other consumers of this information.
2051 SourceManager &SrcMgr = PP->getSourceManager();
2052 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2053 for (ASTContext::import_iterator I = Context->local_import_begin(),
2054 IEnd = Context->local_import_end();
2055 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002056 if (Module *ImportedFrom
2057 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2058 SrcMgr))) {
2059 ImportedFrom->Imports.push_back(I->getImportedModule());
2060 }
2061 }
2062
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002063 // Enter the submodule description block.
2064 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2065
2066 // Write the abbreviations needed for the submodules block.
2067 using namespace llvm;
2068 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2069 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002077 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002078 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2079 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2080
2081 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002082 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2084 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2085
2086 Abbrev = new BitCodeAbbrev();
2087 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2089 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002090
2091 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002092 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2094 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2095
2096 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002097 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2099 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2100
Douglas Gregor51f564f2011-12-31 04:05:44 +00002101 Abbrev = new BitCodeAbbrev();
2102 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2104 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2105
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002106 Abbrev = new BitCodeAbbrev();
2107 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2108 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2109 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2110
Douglas Gregor26ced122011-12-01 00:59:36 +00002111 // Write the submodule metadata block.
2112 RecordData Record;
2113 Record.push_back(getNumberOfModules(WritingModule));
2114 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2115 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2116
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002117 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002118 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002119 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002120 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002121 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002122 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002123 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002124
2125 // Emit the definition of the block.
2126 Record.clear();
2127 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002128 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002129 if (Mod->Parent) {
2130 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2131 Record.push_back(SubmoduleIDs[Mod->Parent]);
2132 } else {
2133 Record.push_back(0);
2134 }
2135 Record.push_back(Mod->IsFramework);
2136 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002137 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002138 Record.push_back(Mod->InferSubmodules);
2139 Record.push_back(Mod->InferExplicitSubmodules);
2140 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002141 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2142
Douglas Gregor51f564f2011-12-31 04:05:44 +00002143 // Emit the requirements.
2144 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2145 Record.clear();
2146 Record.push_back(SUBMODULE_REQUIRES);
2147 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2148 Mod->Requires[I].data(),
2149 Mod->Requires[I].size());
2150 }
2151
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002152 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002153 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002154 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002155 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002156 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002157 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002158 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2159 Record.clear();
2160 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2161 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2162 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002163 }
2164
2165 // Emit the headers.
2166 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2167 Record.clear();
2168 Record.push_back(SUBMODULE_HEADER);
2169 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2170 Mod->Headers[I]->getName());
2171 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002172 // Emit the excluded headers.
2173 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2174 Record.clear();
2175 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2176 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2177 Mod->ExcludedHeaders[I]->getName());
2178 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002179 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2180 Record.clear();
2181 Record.push_back(SUBMODULE_TOPHEADER);
2182 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2183 Mod->TopHeaders[I]->getName());
2184 }
Douglas Gregor55988682011-12-05 16:33:54 +00002185
2186 // Emit the imports.
2187 if (!Mod->Imports.empty()) {
2188 Record.clear();
2189 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002190 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002191 assert(ImportedID && "Unknown submodule!");
2192 Record.push_back(ImportedID);
2193 }
2194 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2195 }
2196
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002197 // Emit the exports.
2198 if (!Mod->Exports.empty()) {
2199 Record.clear();
2200 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002201 if (Module *Exported = Mod->Exports[I].getPointer()) {
2202 unsigned ExportedID = SubmoduleIDs[Exported];
2203 assert(ExportedID > 0 && "Unknown submodule ID?");
2204 Record.push_back(ExportedID);
2205 } else {
2206 Record.push_back(0);
2207 }
2208
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002209 Record.push_back(Mod->Exports[I].getInt());
2210 }
2211 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2212 }
2213
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002214 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002215 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2216 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002217 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002218 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002219 }
2220
2221 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002222
2223 assert((NextSubmoduleID - FirstSubmoduleID
2224 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002225}
2226
Douglas Gregor185dbd72011-12-01 02:07:58 +00002227serialization::SubmoduleID
2228ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002229 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002230 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002231
2232 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002233 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002234 Module *OwningMod
2235 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002236 if (!OwningMod)
2237 return 0;
2238
Douglas Gregore209e502011-12-06 01:10:29 +00002239 // Check whether this submodule is part of our own module.
2240 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002241 return 0;
2242
Douglas Gregore209e502011-12-06 01:10:29 +00002243 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002244}
2245
David Blaikied6471f72011-09-25 23:23:43 +00002246void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002247 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002248 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002249 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2250 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002251 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002252 if (point.Loc.isInvalid())
2253 continue;
2254
2255 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002256 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002257 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002258 if (I->second.isPragma()) {
2259 Record.push_back(I->first);
2260 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002261 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002262 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002263 Record.push_back(-1); // mark the end of the diag/map pairs for this
2264 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002265 }
2266
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002267 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002268 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002269}
2270
Anders Carlssonc8505782011-03-06 18:41:18 +00002271void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2272 if (CXXBaseSpecifiersOffsets.empty())
2273 return;
2274
2275 RecordData Record;
2276
2277 // Create a blob abbreviation for the C++ base specifiers offsets.
2278 using namespace llvm;
2279
2280 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2281 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2284 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2285
Douglas Gregore92b8a12011-08-04 00:01:48 +00002286 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002287 Record.clear();
2288 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2289 Record.push_back(CXXBaseSpecifiersOffsets.size());
2290 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002291 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002292}
2293
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002294//===----------------------------------------------------------------------===//
2295// Type Serialization
2296//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002297
Sebastian Redl3397c552010-08-18 23:56:27 +00002298/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002299void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002300 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002301 if (Idx.getIndex() == 0) // we haven't seen this type before.
2302 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002303
Douglas Gregor97475832010-10-05 18:37:06 +00002304 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002305
Douglas Gregor2cf26342009-04-09 22:27:44 +00002306 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002307 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002308 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002309 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002310 else if (TypeOffsets.size() < Index) {
2311 TypeOffsets.resize(Index + 1);
2312 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002313 }
2314
2315 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Douglas Gregor2cf26342009-04-09 22:27:44 +00002317 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002318 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002319
Douglas Gregora4923eb2009-11-16 21:35:15 +00002320 if (T.hasLocalNonFastQualifiers()) {
2321 Qualifiers Qs = T.getLocalQualifiers();
2322 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002323 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002324 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002325 } else {
2326 switch (T->getTypeClass()) {
2327 // For all of the concrete, non-dependent types, call the
2328 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002329#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002330 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002331#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002332#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002333 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002334 }
2335
2336 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002337 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002338
2339 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002340 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002341}
2342
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002343//===----------------------------------------------------------------------===//
2344// Declaration Serialization
2345//===----------------------------------------------------------------------===//
2346
Douglas Gregor2cf26342009-04-09 22:27:44 +00002347/// \brief Write the block containing all of the declaration IDs
2348/// lexically declared within the given DeclContext.
2349///
2350/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2351/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002352uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002353 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002354 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002355 return 0;
2356
Douglas Gregorc9490c02009-04-16 22:23:12 +00002357 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002358 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002359 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002360 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002361 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2362 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002363 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002364
Douglas Gregor25123082009-04-22 22:34:57 +00002365 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002366 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002367 return Offset;
2368}
2369
Sebastian Redla4232eb2010-08-18 23:56:21 +00002370void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002371 using namespace llvm;
2372 RecordData Record;
2373
2374 // Write the type offsets array
2375 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002376 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2380 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2381 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002382 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002383 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002384 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002385 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002386
2387 // Write the declaration offsets array
2388 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002389 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2393 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2394 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002395 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002396 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002397 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002398 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002399}
2400
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002401void ASTWriter::WriteFileDeclIDsMap() {
2402 using namespace llvm;
2403 RecordData Record;
2404
2405 // Join the vectors of DeclIDs from all files.
2406 SmallVector<DeclID, 256> FileSortedIDs;
2407 for (FileDeclIDsTy::iterator
2408 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2409 DeclIDInFileInfo &Info = *FI->second;
2410 Info.FirstDeclIndex = FileSortedIDs.size();
2411 for (LocDeclIDsTy::iterator
2412 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2413 FileSortedIDs.push_back(DI->second);
2414 }
2415
2416 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2417 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2420 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2421 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002422 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002423 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2424}
2425
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002426void ASTWriter::WriteComments() {
2427 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002428 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002429 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002430 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2431 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002432 I != E; ++I) {
2433 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002434 AddSourceRange((*I)->getSourceRange(), Record);
2435 Record.push_back((*I)->getKind());
2436 Record.push_back((*I)->isTrailingComment());
2437 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002438 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2439 }
2440 Stream.ExitBlock();
2441}
2442
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002443//===----------------------------------------------------------------------===//
2444// Global Method Pool and Selector Serialization
2445//===----------------------------------------------------------------------===//
2446
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002447namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002448// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002449class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002450 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002451
2452public:
2453 typedef Selector key_type;
2454 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Sebastian Redl5d050072010-08-04 17:20:04 +00002456 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002457 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002458 ObjCMethodList Instance, Factory;
2459 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002460 typedef const data_type& data_type_ref;
2461
Sebastian Redl3397c552010-08-18 23:56:27 +00002462 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002463
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002464 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002465 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002466 }
Mike Stump1eb44332009-09-09 15:08:12 +00002467
2468 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002469 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002470 data_type_ref Methods) {
2471 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2472 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002473 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2474 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002475 Method = Method->Next)
2476 if (Method->Method)
2477 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002478 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002479 Method = Method->Next)
2480 if (Method->Method)
2481 DataLen += 4;
2482 clang::io::Emit16(Out, DataLen);
2483 return std::make_pair(KeyLen, DataLen);
2484 }
Mike Stump1eb44332009-09-09 15:08:12 +00002485
Chris Lattner5f9e2722011-07-23 10:55:15 +00002486 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002487 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002488 assert((Start >> 32) == 0 && "Selector key offset too large");
2489 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002490 unsigned N = Sel.getNumArgs();
2491 clang::io::Emit16(Out, N);
2492 if (N == 0)
2493 N = 1;
2494 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002495 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002496 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2497 }
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Chris Lattner5f9e2722011-07-23 10:55:15 +00002499 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002500 data_type_ref Methods, unsigned DataLen) {
2501 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002502 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002503 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002504 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002505 Method = Method->Next)
2506 if (Method->Method)
2507 ++NumInstanceMethods;
2508
2509 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002510 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002511 Method = Method->Next)
2512 if (Method->Method)
2513 ++NumFactoryMethods;
2514
2515 clang::io::Emit16(Out, NumInstanceMethods);
2516 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002517 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002518 Method = Method->Next)
2519 if (Method->Method)
2520 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002521 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002522 Method = Method->Next)
2523 if (Method->Method)
2524 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002525
2526 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002527 }
2528};
2529} // end anonymous namespace
2530
Sebastian Redl059612d2010-08-03 21:58:15 +00002531/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002532///
2533/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002534/// in an on-disk hash table indexed by the selector. The hash table also
2535/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002536void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002537 using namespace llvm;
2538
Sebastian Redl059612d2010-08-03 21:58:15 +00002539 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002540 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002541 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002542 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002543 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002544 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002545 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002546 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002547
Sebastian Redl059612d2010-08-03 21:58:15 +00002548 // Create the on-disk hash table representation. We walk through every
2549 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002550 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002551 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002552 I = SelectorIDs.begin(), E = SelectorIDs.end();
2553 I != E; ++I) {
2554 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002555 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002556 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002557 I->second,
2558 ObjCMethodList(),
2559 ObjCMethodList()
2560 };
2561 if (F != SemaRef.MethodPool.end()) {
2562 Data.Instance = F->second.first;
2563 Data.Factory = F->second.second;
2564 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002565 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002566 // changed.
2567 if (Chain && I->second < FirstSelectorID) {
2568 // Selector already exists. Did it change?
2569 bool changed = false;
2570 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2571 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002572 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002573 changed = true;
2574 }
2575 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2576 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002577 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002578 changed = true;
2579 }
2580 if (!changed)
2581 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002582 } else if (Data.Instance.Method || Data.Factory.Method) {
2583 // A new method pool entry.
2584 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002585 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002586 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002587 }
2588
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002589 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002590 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002591 uint32_t BucketOffset;
2592 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002593 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002594 llvm::raw_svector_ostream Out(MethodPool);
2595 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002596 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002597 BucketOffset = Generator.Emit(Out, Trait);
2598 }
2599
2600 // Create a blob abbreviation
2601 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002602 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2606 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2607
Douglas Gregor83941df2009-04-25 17:48:32 +00002608 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002609 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002610 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002611 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002612 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002613 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002614
2615 // Create a blob abbreviation for the selector table offsets.
2616 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002617 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2621 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2622
2623 // Write the selector offsets table.
2624 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002625 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002626 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002627 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002628 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002629 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002630 }
2631}
2632
Sebastian Redl3397c552010-08-18 23:56:27 +00002633/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002634void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002635 using namespace llvm;
2636 if (SemaRef.ReferencedSelectors.empty())
2637 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002638
Fariborz Jahanian32019832010-07-23 19:11:11 +00002639 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002640
Sebastian Redl3397c552010-08-18 23:56:27 +00002641 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002642 // very tricky to fix, and given that @selector shouldn't really appear in
2643 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002644 for (DenseMap<Selector, SourceLocation>::iterator S =
2645 SemaRef.ReferencedSelectors.begin(),
2646 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2647 Selector Sel = (*S).first;
2648 SourceLocation Loc = (*S).second;
2649 AddSelectorRef(Sel, Record);
2650 AddSourceLocation(Loc, Record);
2651 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002652 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002653}
2654
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002655//===----------------------------------------------------------------------===//
2656// Identifier Table Serialization
2657//===----------------------------------------------------------------------===//
2658
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002659namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002660class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002661 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002662 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002663 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002664 bool IsModule;
2665
Douglas Gregora92193e2009-04-28 21:18:29 +00002666 /// \brief Determines whether this is an "interesting" identifier
2667 /// that needs a full IdentifierInfo structure written into the hash
2668 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002669 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002670 if (II->isPoisoned() ||
2671 II->isExtensionToken() ||
2672 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002673 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002674 II->getFETokenInfo<void>())
2675 return true;
2676
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002677 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002678 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002679
2680 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2681 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002682 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002683
2684 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002685 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002686
2687 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002688 }
2689
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002690public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002691 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002692 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002694 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002695 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Douglas Gregoreee242f2011-10-27 09:33:13 +00002697 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2698 IdentifierResolver &IdResolver, bool IsModule)
2699 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002700
2701 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002702 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002703 }
Mike Stump1eb44332009-09-09 15:08:12 +00002704
2705 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002706 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002707 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002708 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002709 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002710 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002711 DataLen += 2; // 2 bytes for builtin ID
2712 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002713 if (hadMacroDefinition(II, Macro)) {
2714 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2715 if (Writer.getMacroRef(M) != 0)
2716 DataLen += 4;
2717 }
2718
2719 DataLen += 4;
2720 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002721
Douglas Gregoreee242f2011-10-27 09:33:13 +00002722 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2723 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002724 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002725 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002726 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002727 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002728 // We emit the key length after the data length so that every
2729 // string is preceded by a 16-bit length. This matches the PTH
2730 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002731 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002732 return std::make_pair(KeyLen, DataLen);
2733 }
Mike Stump1eb44332009-09-09 15:08:12 +00002734
Chris Lattner5f9e2722011-07-23 10:55:15 +00002735 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002736 unsigned KeyLen) {
2737 // Record the location of the key data. This is used when generating
2738 // the mapping from persistent IDs to strings.
2739 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002740 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002741 }
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Douglas Gregor7143aab2011-09-01 17:04:32 +00002743 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002744 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002745 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002746 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002747 clang::io::Emit32(Out, ID << 1);
2748 return;
2749 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002750
Douglas Gregora92193e2009-04-28 21:18:29 +00002751 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002752 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2753 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2754 clang::io::Emit16(Out, Bits);
2755 Bits = 0;
2756 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002757 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002758 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2759 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002760 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002761 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002762 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002763
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002764 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002765 // Write all of the macro IDs associated with this identifier.
2766 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2767 if (MacroID ID = Writer.getMacroRef(M))
2768 clang::io::Emit32(Out, ID);
2769 }
2770
2771 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002772 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002773
Douglas Gregor668c1a42009-04-21 22:25:48 +00002774 // Emit the declaration IDs in reverse order, because the
2775 // IdentifierResolver provides the declarations as they would be
2776 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002777 // "stat"), but the ASTReader adds declarations to the end of the list
2778 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002779 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002780 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2781 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002782 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002783 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002784 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002785 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002786 }
2787};
2788} // end anonymous namespace
2789
Sebastian Redl3397c552010-08-18 23:56:27 +00002790/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002791///
2792/// The identifier table consists of a blob containing string data
2793/// (the actual identifiers themselves) and a separate "offsets" index
2794/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002795void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2796 IdentifierResolver &IdResolver,
2797 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002798 using namespace llvm;
2799
2800 // Create and write out the blob that contains the identifier
2801 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002802 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002803 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002804 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002805
Douglas Gregor92b059e2009-04-28 20:33:11 +00002806 // Look for any identifiers that were named while processing the
2807 // headers, but are otherwise not needed. We add these to the hash
2808 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002809 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002810 // file.
2811 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2812 IDEnd = PP.getIdentifierTable().end();
2813 ID != IDEnd; ++ID)
2814 getIdentifierRef(ID->second);
2815
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002816 // Create the on-disk hash table representation. We only store offsets
2817 // for identifiers that appear here for the first time.
2818 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002819 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002820 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2821 ID != IDEnd; ++ID) {
2822 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002823 if (!Chain || !ID->first->isFromAST() ||
2824 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002825 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2826 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002827 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002828
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002829 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002830 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002831 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002832 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002833 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002834 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002835 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002836 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002837 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002838 }
2839
2840 // Create a blob abbreviation
2841 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002842 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002845 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002846
2847 // Write the identifier table
2848 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002849 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002850 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002851 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002852 }
2853
2854 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002855 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002856 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002857 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002858 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002859 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2860 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2861
2862 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002863 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002864 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002865 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002866 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002867 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002868}
2869
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002870//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002871// DeclContext's Name Lookup Table Serialization
2872//===----------------------------------------------------------------------===//
2873
2874namespace {
2875// Trait used for the on-disk hash table used in the method pool.
2876class ASTDeclContextNameLookupTrait {
2877 ASTWriter &Writer;
2878
2879public:
2880 typedef DeclarationName key_type;
2881 typedef key_type key_type_ref;
2882
2883 typedef DeclContext::lookup_result data_type;
2884 typedef const data_type& data_type_ref;
2885
2886 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2887
2888 unsigned ComputeHash(DeclarationName Name) {
2889 llvm::FoldingSetNodeID ID;
2890 ID.AddInteger(Name.getNameKind());
2891
2892 switch (Name.getNameKind()) {
2893 case DeclarationName::Identifier:
2894 ID.AddString(Name.getAsIdentifierInfo()->getName());
2895 break;
2896 case DeclarationName::ObjCZeroArgSelector:
2897 case DeclarationName::ObjCOneArgSelector:
2898 case DeclarationName::ObjCMultiArgSelector:
2899 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2900 break;
2901 case DeclarationName::CXXConstructorName:
2902 case DeclarationName::CXXDestructorName:
2903 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002904 break;
2905 case DeclarationName::CXXOperatorName:
2906 ID.AddInteger(Name.getCXXOverloadedOperator());
2907 break;
2908 case DeclarationName::CXXLiteralOperatorName:
2909 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2910 case DeclarationName::CXXUsingDirective:
2911 break;
2912 }
2913
2914 return ID.ComputeHash();
2915 }
2916
2917 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002918 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002919 data_type_ref Lookup) {
2920 unsigned KeyLen = 1;
2921 switch (Name.getNameKind()) {
2922 case DeclarationName::Identifier:
2923 case DeclarationName::ObjCZeroArgSelector:
2924 case DeclarationName::ObjCOneArgSelector:
2925 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002926 case DeclarationName::CXXLiteralOperatorName:
2927 KeyLen += 4;
2928 break;
2929 case DeclarationName::CXXOperatorName:
2930 KeyLen += 1;
2931 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002932 case DeclarationName::CXXConstructorName:
2933 case DeclarationName::CXXDestructorName:
2934 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002935 case DeclarationName::CXXUsingDirective:
2936 break;
2937 }
2938 clang::io::Emit16(Out, KeyLen);
2939
2940 // 2 bytes for num of decls and 4 for each DeclID.
2941 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2942 clang::io::Emit16(Out, DataLen);
2943
2944 return std::make_pair(KeyLen, DataLen);
2945 }
2946
Chris Lattner5f9e2722011-07-23 10:55:15 +00002947 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002948 using namespace clang::io;
2949
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002950 Emit8(Out, Name.getNameKind());
2951 switch (Name.getNameKind()) {
2952 case DeclarationName::Identifier:
2953 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002954 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002955 case DeclarationName::ObjCZeroArgSelector:
2956 case DeclarationName::ObjCOneArgSelector:
2957 case DeclarationName::ObjCMultiArgSelector:
2958 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002959 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002960 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002961 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2962 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002963 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002964 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002965 case DeclarationName::CXXLiteralOperatorName:
2966 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002967 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002968 case DeclarationName::CXXConstructorName:
2969 case DeclarationName::CXXDestructorName:
2970 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002971 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002972 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002973 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002974
2975 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002976 }
2977
Chris Lattner5f9e2722011-07-23 10:55:15 +00002978 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002979 data_type Lookup, unsigned DataLen) {
2980 uint64_t Start = Out.tell(); (void)Start;
2981 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2982 for (; Lookup.first != Lookup.second; ++Lookup.first)
2983 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2984
2985 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2986 }
2987};
2988} // end anonymous namespace
2989
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002990/// \brief Write the block containing all of the declaration IDs
2991/// visible from the given DeclContext.
2992///
2993/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002994/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002995uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2996 DeclContext *DC) {
2997 if (DC->getPrimaryContext() != DC)
2998 return 0;
2999
3000 // Since there is no name lookup into functions or methods, don't bother to
3001 // build a visible-declarations table for these entities.
3002 if (DC->isFunctionOrMethod())
3003 return 0;
3004
3005 // If not in C++, we perform name lookup for the translation unit via the
3006 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3007 // FIXME: In C++ we need the visible declarations in order to "see" the
3008 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00003009 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003010 return 0;
3011
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003012 // Serialize the contents of the mapping used for lookup. Note that,
3013 // although we have two very different code paths, the serialized
3014 // representation is the same for both cases: a declaration name,
3015 // followed by a size, followed by references to the visible
3016 // declarations that have that name.
3017 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003018 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003019 if (!Map || Map->empty())
3020 return 0;
3021
3022 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3023 ASTDeclContextNameLookupTrait Trait(*this);
3024
3025 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003026 DeclarationName ConversionName;
3027 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003028 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3029 D != DEnd; ++D) {
3030 DeclarationName Name = D->first;
3031 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00003032 if (Result.first != Result.second) {
3033 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3034 // Hash all conversion function names to the same name. The actual
3035 // type information in conversion function name is not used in the
3036 // key (since such type information is not stable across different
3037 // modules), so the intended effect is to coalesce all of the conversion
3038 // functions under a single key.
3039 if (!ConversionName)
3040 ConversionName = Name;
3041 ConversionDecls.append(Result.first, Result.second);
3042 continue;
3043 }
3044
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003045 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003046 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003047 }
3048
Douglas Gregore5a54b62011-08-30 20:49:19 +00003049 // Add the conversion functions
3050 if (!ConversionDecls.empty()) {
3051 Generator.insert(ConversionName,
3052 DeclContext::lookup_result(ConversionDecls.begin(),
3053 ConversionDecls.end()),
3054 Trait);
3055 }
3056
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003057 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003058 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003059 uint32_t BucketOffset;
3060 {
3061 llvm::raw_svector_ostream Out(LookupTable);
3062 // Make sure that no bucket is at offset 0
3063 clang::io::Emit32(Out, 0);
3064 BucketOffset = Generator.Emit(Out, Trait);
3065 }
3066
3067 // Write the lookup table
3068 RecordData Record;
3069 Record.push_back(DECL_CONTEXT_VISIBLE);
3070 Record.push_back(BucketOffset);
3071 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3072 LookupTable.str());
3073
3074 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3075 ++NumVisibleDeclContexts;
3076 return Offset;
3077}
3078
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003079/// \brief Write an UPDATE_VISIBLE block for the given context.
3080///
3081/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3082/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003083/// (in C++), for namespaces, and for classes with forward-declared unscoped
3084/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003085void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003086 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3087 if (!Map || Map->empty())
3088 return;
3089
3090 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3091 ASTDeclContextNameLookupTrait Trait(*this);
3092
3093 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003094 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3095 D != DEnd; ++D) {
3096 DeclarationName Name = D->first;
3097 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003098 // For any name that appears in this table, the results are complete, i.e.
3099 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003100 if (Result.first != Result.second)
3101 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003102 }
3103
3104 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003105 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003106 uint32_t BucketOffset;
3107 {
3108 llvm::raw_svector_ostream Out(LookupTable);
3109 // Make sure that no bucket is at offset 0
3110 clang::io::Emit32(Out, 0);
3111 BucketOffset = Generator.Emit(Out, Trait);
3112 }
3113
3114 // Write the lookup table
3115 RecordData Record;
3116 Record.push_back(UPDATE_VISIBLE);
3117 Record.push_back(getDeclID(cast<Decl>(DC)));
3118 Record.push_back(BucketOffset);
3119 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3120}
3121
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003122/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3123void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3124 RecordData Record;
3125 Record.push_back(Opts.fp_contract);
3126 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3127}
3128
3129/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3130void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003131 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003132 return;
3133
3134 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3135 RecordData Record;
3136#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3137#include "clang/Basic/OpenCLExtensions.def"
3138 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3139}
3140
Douglas Gregor2171bf12012-01-15 16:58:34 +00003141void ASTWriter::WriteRedeclarations() {
3142 RecordData LocalRedeclChains;
3143 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3144
3145 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3146 Decl *First = Redeclarations[I];
3147 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3148
3149 Decl *MostRecent = First->getMostRecentDecl();
3150
3151 // If we only have a single declaration, there is no point in storing
3152 // a redeclaration chain.
3153 if (First == MostRecent)
3154 continue;
3155
3156 unsigned Offset = LocalRedeclChains.size();
3157 unsigned Size = 0;
3158 LocalRedeclChains.push_back(0); // Placeholder for the size.
3159
3160 // Collect the set of local redeclarations of this declaration.
3161 for (Decl *Prev = MostRecent; Prev != First;
3162 Prev = Prev->getPreviousDecl()) {
3163 if (!Prev->isFromASTFile()) {
3164 AddDeclRef(Prev, LocalRedeclChains);
3165 ++Size;
3166 }
3167 }
3168 LocalRedeclChains[Offset] = Size;
3169
3170 // Reverse the set of local redeclarations, so that we store them in
3171 // order (since we found them in reverse order).
3172 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3173
3174 // Add the mapping from the first ID to the set of local declarations.
3175 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3176 LocalRedeclsMap.push_back(Info);
3177
3178 assert(N == Redeclarations.size() &&
3179 "Deserialized a declaration we shouldn't have");
3180 }
3181
3182 if (LocalRedeclChains.empty())
3183 return;
3184
3185 // Sort the local redeclarations map by the first declaration ID,
3186 // since the reader will be performing binary searches on this information.
3187 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3188
3189 // Emit the local redeclarations map.
3190 using namespace llvm;
3191 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3192 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3195 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3196
3197 RecordData Record;
3198 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3199 Record.push_back(LocalRedeclsMap.size());
3200 Stream.EmitRecordWithBlob(AbbrevID, Record,
3201 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3202 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3203
3204 // Emit the redeclaration chains.
3205 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3206}
3207
Douglas Gregorcff9f262012-01-27 01:47:08 +00003208void ASTWriter::WriteObjCCategories() {
3209 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3210 RecordData Categories;
3211
3212 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3213 unsigned Size = 0;
3214 unsigned StartIndex = Categories.size();
3215
3216 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3217
3218 // Allocate space for the size.
3219 Categories.push_back(0);
3220
3221 // Add the categories.
3222 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3223 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3224 assert(getDeclID(Cat) != 0 && "Bogus category");
3225 AddDeclRef(Cat, Categories);
3226 }
3227
3228 // Update the size.
3229 Categories[StartIndex] = Size;
3230
3231 // Record this interface -> category map.
3232 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3233 CategoriesMap.push_back(CatInfo);
3234 }
3235
3236 // Sort the categories map by the definition ID, since the reader will be
3237 // performing binary searches on this information.
3238 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3239
3240 // Emit the categories map.
3241 using namespace llvm;
3242 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3243 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3246 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3247
3248 RecordData Record;
3249 Record.push_back(OBJC_CATEGORIES_MAP);
3250 Record.push_back(CategoriesMap.size());
3251 Stream.EmitRecordWithBlob(AbbrevID, Record,
3252 reinterpret_cast<char*>(CategoriesMap.data()),
3253 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3254
3255 // Emit the category lists.
3256 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3257}
3258
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003259void ASTWriter::WriteMergedDecls() {
3260 if (!Chain || Chain->MergedDecls.empty())
3261 return;
3262
3263 RecordData Record;
3264 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3265 IEnd = Chain->MergedDecls.end();
3266 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003267 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003268 : getDeclID(I->first);
3269 assert(CanonID && "Merged declaration not known?");
3270
3271 Record.push_back(CanonID);
3272 Record.push_back(I->second.size());
3273 Record.append(I->second.begin(), I->second.end());
3274 }
3275 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3276}
3277
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003278//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003279// General Serialization Routines
3280//===----------------------------------------------------------------------===//
3281
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003282/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003283void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3284 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003285 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003286 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3287 e = Attrs.end(); i != e; ++i){
3288 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003289 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003290 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003291
Sean Huntcf807c42010-08-18 23:23:40 +00003292#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003293
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003294 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003295}
3296
Chris Lattner5f9e2722011-07-23 10:55:15 +00003297void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003298 Record.push_back(Str.size());
3299 Record.insert(Record.end(), Str.begin(), Str.end());
3300}
3301
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003302void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3303 RecordDataImpl &Record) {
3304 Record.push_back(Version.getMajor());
3305 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3306 Record.push_back(*Minor + 1);
3307 else
3308 Record.push_back(0);
3309 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3310 Record.push_back(*Subminor + 1);
3311 else
3312 Record.push_back(0);
3313}
3314
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003315/// \brief Note that the identifier II occurs at the given offset
3316/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003317void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003318 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003319 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003320 // up earlier in the chain and thus don't need an offset.
3321 if (ID >= FirstIdentID)
3322 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003323}
3324
Douglas Gregor83941df2009-04-25 17:48:32 +00003325/// \brief Note that the selector Sel occurs at the given offset
3326/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003327void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003328 unsigned ID = SelectorIDs[Sel];
3329 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003330 // Don't record offsets for selectors that are also available in a different
3331 // file.
3332 if (ID < FirstSelectorID)
3333 return;
3334 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003335}
3336
Sebastian Redla4232eb2010-08-18 23:56:21 +00003337ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003338 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003339 WritingAST(false), DoneWritingDeclsAndTypes(false),
3340 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003341 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003342 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003343 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3344 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003345 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3346 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003347 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003348 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003349 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003350 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003351 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003352 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003353 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3354 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3355 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003356 DeclTypedefAbbrev(0),
3357 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3358 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003359{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003360}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003361
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003362ASTWriter::~ASTWriter() {
3363 for (FileDeclIDsTy::iterator
3364 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3365 delete I->second;
3366}
3367
Sebastian Redla4232eb2010-08-18 23:56:21 +00003368void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003369 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003370 Module *WritingModule, StringRef isysroot,
3371 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003372 WritingAST = true;
3373
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003374 ASTHasCompilerErrors = hasErrors;
3375
Douglas Gregor2cf26342009-04-09 22:27:44 +00003376 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003377 Stream.Emit((unsigned)'C', 8);
3378 Stream.Emit((unsigned)'P', 8);
3379 Stream.Emit((unsigned)'C', 8);
3380 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003381
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003382 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003383
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003384 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003385 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003386 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003387 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003388 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003389 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003390 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003391
3392 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003393}
3394
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003395template<typename Vector>
3396static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3397 ASTWriter::RecordData &Record) {
3398 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3399 I != E; ++I) {
3400 Writer.AddDeclRef(*I, Record);
3401 }
3402}
3403
Sebastian Redla4232eb2010-08-18 23:56:21 +00003404void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003405 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003406 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003407 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003408 using namespace llvm;
3409
Douglas Gregorecc2c092011-12-01 22:20:10 +00003410 // Make sure that the AST reader knows to finalize itself.
3411 if (Chain)
3412 Chain->finalizeForWriting();
3413
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003414 ASTContext &Context = SemaRef.Context;
3415 Preprocessor &PP = SemaRef.PP;
3416
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003417 // Set up predefined declaration IDs.
3418 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003419 if (Context.ObjCIdDecl)
3420 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003421 if (Context.ObjCSelDecl)
3422 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003423 if (Context.ObjCClassDecl)
3424 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003425 if (Context.ObjCProtocolClassDecl)
3426 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003427 if (Context.Int128Decl)
3428 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3429 if (Context.UInt128Decl)
3430 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003431 if (Context.ObjCInstanceTypeDecl)
3432 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003433 if (Context.BuiltinVaListDecl)
3434 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3435
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003436 if (!Chain) {
3437 // Make sure that we emit IdentifierInfos (and any attached
3438 // declarations) for builtins. We don't need to do this when we're
3439 // emitting chained PCH files, because all of the builtins will be
3440 // in the original PCH file.
3441 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003442 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003443 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003444 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003445 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003446 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3447 getIdentifierRef(&Table.get(BuiltinNames[I]));
3448 }
3449
Douglas Gregoreee242f2011-10-27 09:33:13 +00003450 // If there are any out-of-date identifiers, bring them up to date.
3451 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3452 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3453 IDEnd = PP.getIdentifierTable().end();
3454 ID != IDEnd; ++ID)
3455 if (ID->second->isOutOfDate())
3456 ExtSource->updateOutOfDateIdentifier(*ID->second);
3457 }
3458
Chris Lattner63d65f82009-09-08 18:19:27 +00003459 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003460 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003461 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003462 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003463 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003464
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003465 // Build a record containing all of the file scoped decls in this file.
3466 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003467 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3468 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003469
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003470 // Build a record containing all of the delegating constructors we still need
3471 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003472 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003473 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003474
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003475 // Write the set of weak, undeclared identifiers. We always write the
3476 // entire table, since later PCH files in a PCH chain are only interested in
3477 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003478 RecordData WeakUndeclaredIdentifiers;
3479 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003480 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003481 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3482 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3483 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3484 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3485 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3486 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3487 }
3488 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003489
Douglas Gregor14c22f22009-04-22 22:18:58 +00003490 // Build a record containing all of the locally-scoped external
3491 // declarations in this header file. Generally, this record will be
3492 // empty.
3493 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003494 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003495 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003496 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003497 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3498 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003499 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003500 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003501 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3502 }
3503
Douglas Gregorb81c1702009-04-27 20:06:05 +00003504 // Build a record containing all of the ext_vector declarations.
3505 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003506 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003507
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003508 // Build a record containing all of the VTable uses information.
3509 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003510 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003511 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3512 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3513 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3514 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3515 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003516 }
3517
3518 // Build a record containing all of dynamic classes declarations.
3519 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003520 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003521
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003522 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003523 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003524 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003525 I = SemaRef.PendingInstantiations.begin(),
3526 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3527 AddDeclRef(I->first, PendingInstantiations);
3528 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003529 }
3530 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3531 "There are local ones at end of translation unit!");
3532
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003533 // Build a record containing some declaration references.
3534 RecordData SemaDeclRefs;
3535 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3536 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3537 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3538 }
3539
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003540 RecordData CUDASpecialDeclRefs;
3541 if (Context.getcudaConfigureCallDecl()) {
3542 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3543 }
3544
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003545 // Build a record containing all of the known namespaces.
3546 RecordData KnownNamespaces;
3547 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3548 I = SemaRef.KnownNamespaces.begin(),
3549 IEnd = SemaRef.KnownNamespaces.end();
3550 I != IEnd; ++I) {
3551 if (!I->second)
3552 AddDeclRef(I->first, KnownNamespaces);
3553 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003554
3555 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003556 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003557
Sebastian Redl3397c552010-08-18 23:56:27 +00003558 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003559 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003560 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor832d6202011-07-22 16:35:34 +00003561 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003562 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003563
3564 // Create a lexical update block containing all of the declarations in the
3565 // translation unit that do not come from other AST files.
3566 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3567 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3568 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3569 E = TU->noload_decls_end();
3570 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003571 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003572 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003573 }
3574
3575 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3576 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3577 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3578 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3579 Record.clear();
3580 Record.push_back(TU_UPDATE_LEXICAL);
3581 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3582 data(NewGlobalDecls));
3583
3584 // And a visible updates block for the translation unit.
3585 Abv = new llvm::BitCodeAbbrev();
3586 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3587 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3588 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3589 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3590 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3591 WriteDeclContextVisibleUpdate(TU);
3592
3593 // If the translation unit has an anonymous namespace, and we don't already
3594 // have an update block for it, write it as an update block.
3595 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3596 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3597 if (Record.empty()) {
3598 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003599 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003600 }
3601 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003602
3603 // Make sure visible decls, added to DeclContexts previously loaded from
3604 // an AST file, are registered for serialization.
3605 for (SmallVector<const Decl *, 16>::iterator
3606 I = UpdatingVisibleDecls.begin(),
3607 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3608 GetDeclRef(*I);
3609 }
3610
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003611 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003612 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003613
Douglas Gregora119da02011-08-02 16:26:37 +00003614 // Form the record of special types.
3615 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003616 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003617 AddTypeRef(Context.getFILEType(), SpecialTypes);
3618 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3619 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3620 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3621 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003622 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003623 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003624
Douglas Gregor366809a2009-04-26 03:49:13 +00003625 // Keep writing types and declarations until all types and
3626 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003627 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003628 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003629 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3630 E = DeclsToRewrite.end();
3631 I != E; ++I)
3632 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003633 while (!DeclTypesToEmit.empty()) {
3634 DeclOrType DOT = DeclTypesToEmit.front();
3635 DeclTypesToEmit.pop();
3636 if (DOT.isType())
3637 WriteType(DOT.getType());
3638 else
3639 WriteDecl(Context, DOT.getDecl());
3640 }
3641 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003642
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003643 DoneWritingDeclsAndTypes = true;
3644
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003645 WriteFileDeclIDsMap();
3646 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003647 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003648
3649 if (Chain) {
3650 // Write the mapping information describing our module dependencies and how
3651 // each of those modules were mapped into our own offset/ID space, so that
3652 // the reader can build the appropriate mapping to its own offset/ID space.
3653 // The map consists solely of a blob with the following format:
3654 // *(module-name-len:i16 module-name:len*i8
3655 // source-location-offset:i32
3656 // identifier-id:i32
3657 // preprocessed-entity-id:i32
3658 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003659 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003660 // selector-id:i32
3661 // declaration-id:i32
3662 // c++-base-specifiers-id:i32
3663 // type-id:i32)
3664 //
3665 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3666 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3668 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003669 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003670 {
3671 llvm::raw_svector_ostream Out(Buffer);
3672 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003673 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003674 M != MEnd; ++M) {
3675 StringRef FileName = (*M)->FileName;
3676 io::Emit16(Out, FileName.size());
3677 Out.write(FileName.data(), FileName.size());
3678 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3679 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003680 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003681 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003682 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003683 io::Emit32(Out, (*M)->BaseSelectorID);
3684 io::Emit32(Out, (*M)->BaseDeclID);
3685 io::Emit32(Out, (*M)->BaseTypeIndex);
3686 }
3687 }
3688 Record.clear();
3689 Record.push_back(MODULE_OFFSET_MAP);
3690 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3691 Buffer.data(), Buffer.size());
3692 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003693 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003694 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003695 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003696 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003697 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003698 WriteFPPragmaOptions(SemaRef.getFPOptions());
3699 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003700
Sebastian Redl1476ed42010-07-16 16:36:56 +00003701 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003702 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003703
Anders Carlssonc8505782011-03-06 18:41:18 +00003704 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003705
Douglas Gregore209e502011-12-06 01:10:29 +00003706 // If we're emitting a module, write out the submodule information.
3707 if (WritingModule)
3708 WriteSubmodules(WritingModule);
3709
Douglas Gregora119da02011-08-02 16:26:37 +00003710 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3711
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003712 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003713 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003714 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003715
3716 // Write the record containing tentative definitions.
3717 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003718 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003719
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003720 // Write the record containing unused file scoped decls.
3721 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003722 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003723
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003724 // Write the record containing weak undeclared identifiers.
3725 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003726 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003727 WeakUndeclaredIdentifiers);
3728
Douglas Gregor14c22f22009-04-22 22:18:58 +00003729 // Write the record containing locally-scoped external definitions.
3730 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003731 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003732 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003733
3734 // Write the record containing ext_vector type names.
3735 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003736 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003737
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003738 // Write the record containing VTable uses information.
3739 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003740 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003741
3742 // Write the record containing dynamic classes declarations.
3743 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003744 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003745
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003746 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003747 if (!PendingInstantiations.empty())
3748 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003749
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003750 // Write the record containing declaration references of Sema.
3751 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003752 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003753
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003754 // Write the record containing CUDA-specific declaration references.
3755 if (!CUDASpecialDeclRefs.empty())
3756 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003757
3758 // Write the delegating constructors.
3759 if (!DelegatingCtorDecls.empty())
3760 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003761
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003762 // Write the known namespaces.
3763 if (!KnownNamespaces.empty())
3764 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3765
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003766 // Write the visible updates to DeclContexts.
3767 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3768 I = UpdatedDeclContexts.begin(),
3769 E = UpdatedDeclContexts.end();
3770 I != E; ++I)
3771 WriteDeclContextVisibleUpdate(*I);
3772
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003773 if (!WritingModule) {
3774 // Write the submodules that were imported, if any.
3775 RecordData ImportedModules;
3776 for (ASTContext::import_iterator I = Context.local_import_begin(),
3777 IEnd = Context.local_import_end();
3778 I != IEnd; ++I) {
3779 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3780 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3781 }
3782 if (!ImportedModules.empty()) {
3783 // Sort module IDs.
3784 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3785
3786 // Unique module IDs.
3787 ImportedModules.erase(std::unique(ImportedModules.begin(),
3788 ImportedModules.end()),
3789 ImportedModules.end());
3790
3791 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3792 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003793 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003794
3795 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003796 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003797 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003798 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003799 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003800 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003801
Douglas Gregor3e1af842009-04-17 22:13:46 +00003802 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003803 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003804 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003805 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003806 Record.push_back(NumLexicalDeclContexts);
3807 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003808 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003809 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003810}
3811
Douglas Gregora8235d62012-10-09 23:05:51 +00003812void ASTWriter::WriteMacroUpdates() {
3813 if (MacroUpdates.empty())
3814 return;
3815
3816 RecordData Record;
3817 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3818 E = MacroUpdates.end();
3819 I != E; ++I) {
3820 addMacroRef(I->first, Record);
3821 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003822 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003823 }
3824 Stream.EmitRecord(MACRO_UPDATES, Record);
3825}
3826
Douglas Gregor61c5e342011-09-17 00:05:03 +00003827/// \brief Go through the declaration update blocks and resolve declaration
3828/// pointers into declaration IDs.
3829void ASTWriter::ResolveDeclUpdatesBlocks() {
3830 for (DeclUpdateMap::iterator
3831 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3832 const Decl *D = I->first;
3833 UpdateRecord &URec = I->second;
3834
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003835 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003836 continue; // The decl will be written completely
3837
3838 unsigned Idx = 0, N = URec.size();
3839 while (Idx < N) {
3840 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003841 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3842 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3843 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3844 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3845 ++Idx;
3846 break;
3847
3848 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3849 ++Idx;
3850 break;
3851 }
3852 }
3853 }
3854}
3855
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003856void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003857 if (DeclUpdates.empty())
3858 return;
3859
3860 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003861 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003862 for (DeclUpdateMap::iterator
3863 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3864 const Decl *D = I->first;
3865 UpdateRecord &URec = I->second;
3866
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003867 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003868 continue; // The decl will be written completely,no need to store updates.
3869
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003870 uint64_t Offset = Stream.GetCurrentBitNo();
3871 Stream.EmitRecord(DECL_UPDATES, URec);
3872
3873 OffsetsRecord.push_back(GetDeclRef(D));
3874 OffsetsRecord.push_back(Offset);
3875 }
3876 Stream.ExitBlock();
3877 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3878}
3879
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003880void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003881 if (ReplacedDecls.empty())
3882 return;
3883
3884 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003885 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003886 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003887 Record.push_back(I->ID);
3888 Record.push_back(I->Offset);
3889 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003890 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003891 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003892}
3893
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003894void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003895 Record.push_back(Loc.getRawEncoding());
3896}
3897
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003898void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003899 AddSourceLocation(Range.getBegin(), Record);
3900 AddSourceLocation(Range.getEnd(), Record);
3901}
3902
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003903void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003904 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003905 const uint64_t *Words = Value.getRawData();
3906 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003907}
3908
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003909void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003910 Record.push_back(Value.isUnsigned());
3911 AddAPInt(Value, Record);
3912}
3913
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003914void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003915 AddAPInt(Value.bitcastToAPInt(), Record);
3916}
3917
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003918void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003919 Record.push_back(getIdentifierRef(II));
3920}
3921
Douglas Gregora8235d62012-10-09 23:05:51 +00003922void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3923 Record.push_back(getMacroRef(MI));
3924}
3925
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003926IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003927 if (II == 0)
3928 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003929
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003930 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003931 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003932 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003933 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003934}
3935
Douglas Gregora8235d62012-10-09 23:05:51 +00003936MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3937 // Don't emit builtin macros like __LINE__ to the AST file unless they
3938 // have been redefined by the header (in which case they are not
3939 // isBuiltinMacro).
3940 if (MI == 0 || MI->isBuiltinMacro())
3941 return 0;
3942
3943 MacroID &ID = MacroIDs[MI];
3944 if (ID == 0)
3945 ID = NextMacroID++;
3946 return ID;
3947}
3948
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003949void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003950 Record.push_back(getSelectorRef(SelRef));
3951}
3952
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003953SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003954 if (Sel.getAsOpaquePtr() == 0) {
3955 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003956 }
3957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003958 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003959 if (SID == 0 && Chain) {
3960 // This might trigger a ReadSelector callback, which will set the ID for
3961 // this selector.
3962 Chain->LoadSelector(Sel);
3963 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003964 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003965 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003966 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003967 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003968}
3969
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003970void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003971 AddDeclRef(Temp->getDestructor(), Record);
3972}
3973
Douglas Gregor7c789c12010-10-29 22:39:52 +00003974void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3975 CXXBaseSpecifier const *BasesEnd,
3976 RecordDataImpl &Record) {
3977 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3978 CXXBaseSpecifiersToWrite.push_back(
3979 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3980 Bases, BasesEnd));
3981 Record.push_back(NextCXXBaseSpecifiersID++);
3982}
3983
Sebastian Redla4232eb2010-08-18 23:56:21 +00003984void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003985 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003986 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003987 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003988 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003989 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003990 break;
3991 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003992 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003993 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003994 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003995 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003996 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003997 break;
3998 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003999 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004000 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004001 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004002 break;
John McCall833ca992009-10-29 08:12:44 +00004003 case TemplateArgument::Null:
4004 case TemplateArgument::Integral:
4005 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004006 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004007 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004008 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004009 break;
4010 }
4011}
4012
Sebastian Redla4232eb2010-08-18 23:56:21 +00004013void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004014 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004015 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004016
4017 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4018 bool InfoHasSameExpr
4019 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4020 Record.push_back(InfoHasSameExpr);
4021 if (InfoHasSameExpr)
4022 return; // Avoid storing the same expr twice.
4023 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004024 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4025 Record);
4026}
4027
Douglas Gregordc355712011-02-25 00:36:19 +00004028void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4029 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004030 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004031 AddTypeRef(QualType(), Record);
4032 return;
4033 }
4034
Douglas Gregordc355712011-02-25 00:36:19 +00004035 AddTypeLoc(TInfo->getTypeLoc(), Record);
4036}
4037
4038void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4039 AddTypeRef(TL.getType(), Record);
4040
John McCalla1ee0c52009-10-16 21:56:05 +00004041 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004042 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004043 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004044}
4045
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004046void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004047 Record.push_back(GetOrCreateTypeID(T));
4048}
4049
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004050TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4051 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004052 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4053}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004054
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004055TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004056 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004057 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004058}
4059
4060TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4061 if (T.isNull())
4062 return TypeIdx();
4063 assert(!T.getLocalFastQualifiers());
4064
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004065 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004066 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004067 if (DoneWritingDeclsAndTypes) {
4068 assert(0 && "New type seen after serializing all the types to emit!");
4069 return TypeIdx();
4070 }
4071
Douglas Gregor366809a2009-04-26 03:49:13 +00004072 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004073 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004074 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004075 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004076 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004077 return Idx;
4078}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004079
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004080TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004081 if (T.isNull())
4082 return TypeIdx();
4083 assert(!T.getLocalFastQualifiers());
4084
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004085 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4086 assert(I != TypeIdxs.end() && "Type not emitted!");
4087 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004088}
4089
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004090void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004091 Record.push_back(GetDeclRef(D));
4092}
4093
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004094DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004095 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4096
Douglas Gregor2cf26342009-04-09 22:27:44 +00004097 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004098 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004099 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004100
4101 // If D comes from an AST file, its declaration ID is already known and
4102 // fixed.
4103 if (D->isFromASTFile())
4104 return D->getGlobalID();
4105
Douglas Gregor97475832010-10-05 18:37:06 +00004106 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004107 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004108 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004109 if (DoneWritingDeclsAndTypes) {
4110 assert(0 && "New decl seen after serializing all the decls to emit!");
4111 return 0;
4112 }
4113
Douglas Gregor2cf26342009-04-09 22:27:44 +00004114 // We haven't seen this declaration before. Give it a new ID and
4115 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004116 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004117 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004118 }
4119
Sebastian Redl681d7232010-07-27 00:17:23 +00004120 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004121}
4122
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004123DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004124 if (D == 0)
4125 return 0;
4126
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004127 // If D comes from an AST file, its declaration ID is already known and
4128 // fixed.
4129 if (D->isFromASTFile())
4130 return D->getGlobalID();
4131
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004132 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4133 return DeclIDs[D];
4134}
4135
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004136static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4137 std::pair<unsigned, serialization::DeclID> R) {
4138 return L.first < R.first;
4139}
4140
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004141void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004142 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004143 assert(D);
4144
4145 SourceLocation Loc = D->getLocation();
4146 if (Loc.isInvalid())
4147 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004148
4149 // We only keep track of the file-level declarations of each file.
4150 if (!D->getLexicalDeclContext()->isFileContext())
4151 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004152 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4153 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004154 if (isa<ParmVarDecl>(D))
4155 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004156
4157 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004158 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004159 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004160 FileID FID;
4161 unsigned Offset;
4162 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004163 if (FID.isInvalid())
4164 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004165 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004166
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004167 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004168 if (!Info)
4169 Info = new DeclIDInFileInfo();
4170
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004171 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004172 LocDeclIDsTy &Decls = Info->DeclIDs;
4173
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004174 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004175 Decls.push_back(LocDecl);
4176 return;
4177 }
4178
4179 LocDeclIDsTy::iterator
4180 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4181
4182 Decls.insert(I, LocDecl);
4183}
4184
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004185void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004186 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004187 Record.push_back(Name.getNameKind());
4188 switch (Name.getNameKind()) {
4189 case DeclarationName::Identifier:
4190 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4191 break;
4192
4193 case DeclarationName::ObjCZeroArgSelector:
4194 case DeclarationName::ObjCOneArgSelector:
4195 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004196 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004197 break;
4198
4199 case DeclarationName::CXXConstructorName:
4200 case DeclarationName::CXXDestructorName:
4201 case DeclarationName::CXXConversionFunctionName:
4202 AddTypeRef(Name.getCXXNameType(), Record);
4203 break;
4204
4205 case DeclarationName::CXXOperatorName:
4206 Record.push_back(Name.getCXXOverloadedOperator());
4207 break;
4208
Sean Hunt3e518bd2009-11-29 07:34:05 +00004209 case DeclarationName::CXXLiteralOperatorName:
4210 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4211 break;
4212
Douglas Gregor2cf26342009-04-09 22:27:44 +00004213 case DeclarationName::CXXUsingDirective:
4214 // No extra data to emit
4215 break;
4216 }
4217}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004218
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004219void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004220 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004221 switch (Name.getNameKind()) {
4222 case DeclarationName::CXXConstructorName:
4223 case DeclarationName::CXXDestructorName:
4224 case DeclarationName::CXXConversionFunctionName:
4225 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4226 break;
4227
4228 case DeclarationName::CXXOperatorName:
4229 AddSourceLocation(
4230 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4231 Record);
4232 AddSourceLocation(
4233 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4234 Record);
4235 break;
4236
4237 case DeclarationName::CXXLiteralOperatorName:
4238 AddSourceLocation(
4239 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4240 Record);
4241 break;
4242
4243 case DeclarationName::Identifier:
4244 case DeclarationName::ObjCZeroArgSelector:
4245 case DeclarationName::ObjCOneArgSelector:
4246 case DeclarationName::ObjCMultiArgSelector:
4247 case DeclarationName::CXXUsingDirective:
4248 break;
4249 }
4250}
4251
4252void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004253 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004254 AddDeclarationName(NameInfo.getName(), Record);
4255 AddSourceLocation(NameInfo.getLoc(), Record);
4256 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4257}
4258
4259void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004260 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004261 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004262 Record.push_back(Info.NumTemplParamLists);
4263 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4264 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4265}
4266
Sebastian Redla4232eb2010-08-18 23:56:21 +00004267void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004268 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004269 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004270 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004271 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004272
4273 // Push each of the NNS's onto a stack for serialization in reverse order.
4274 while (NNS) {
4275 NestedNames.push_back(NNS);
4276 NNS = NNS->getPrefix();
4277 }
4278
4279 Record.push_back(NestedNames.size());
4280 while(!NestedNames.empty()) {
4281 NNS = NestedNames.pop_back_val();
4282 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4283 Record.push_back(Kind);
4284 switch (Kind) {
4285 case NestedNameSpecifier::Identifier:
4286 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4287 break;
4288
4289 case NestedNameSpecifier::Namespace:
4290 AddDeclRef(NNS->getAsNamespace(), Record);
4291 break;
4292
Douglas Gregor14aba762011-02-24 02:36:08 +00004293 case NestedNameSpecifier::NamespaceAlias:
4294 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4295 break;
4296
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004297 case NestedNameSpecifier::TypeSpec:
4298 case NestedNameSpecifier::TypeSpecWithTemplate:
4299 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4300 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4301 break;
4302
4303 case NestedNameSpecifier::Global:
4304 // Don't need to write an associated value.
4305 break;
4306 }
4307 }
4308}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004309
Douglas Gregordc355712011-02-25 00:36:19 +00004310void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4311 RecordDataImpl &Record) {
4312 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004313 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004314 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004315
4316 // Push each of the nested-name-specifiers's onto a stack for
4317 // serialization in reverse order.
4318 while (NNS) {
4319 NestedNames.push_back(NNS);
4320 NNS = NNS.getPrefix();
4321 }
4322
4323 Record.push_back(NestedNames.size());
4324 while(!NestedNames.empty()) {
4325 NNS = NestedNames.pop_back_val();
4326 NestedNameSpecifier::SpecifierKind Kind
4327 = NNS.getNestedNameSpecifier()->getKind();
4328 Record.push_back(Kind);
4329 switch (Kind) {
4330 case NestedNameSpecifier::Identifier:
4331 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4332 AddSourceRange(NNS.getLocalSourceRange(), Record);
4333 break;
4334
4335 case NestedNameSpecifier::Namespace:
4336 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4337 AddSourceRange(NNS.getLocalSourceRange(), Record);
4338 break;
4339
4340 case NestedNameSpecifier::NamespaceAlias:
4341 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4342 AddSourceRange(NNS.getLocalSourceRange(), Record);
4343 break;
4344
4345 case NestedNameSpecifier::TypeSpec:
4346 case NestedNameSpecifier::TypeSpecWithTemplate:
4347 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4348 AddTypeLoc(NNS.getTypeLoc(), Record);
4349 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4350 break;
4351
4352 case NestedNameSpecifier::Global:
4353 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4354 break;
4355 }
4356 }
4357}
4358
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004359void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004360 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004361 Record.push_back(Kind);
4362 switch (Kind) {
4363 case TemplateName::Template:
4364 AddDeclRef(Name.getAsTemplateDecl(), Record);
4365 break;
4366
4367 case TemplateName::OverloadedTemplate: {
4368 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4369 Record.push_back(OvT->size());
4370 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4371 I != E; ++I)
4372 AddDeclRef(*I, Record);
4373 break;
4374 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004375
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004376 case TemplateName::QualifiedTemplate: {
4377 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4378 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4379 Record.push_back(QualT->hasTemplateKeyword());
4380 AddDeclRef(QualT->getTemplateDecl(), Record);
4381 break;
4382 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004383
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004384 case TemplateName::DependentTemplate: {
4385 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4386 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4387 Record.push_back(DepT->isIdentifier());
4388 if (DepT->isIdentifier())
4389 AddIdentifierRef(DepT->getIdentifier(), Record);
4390 else
4391 Record.push_back(DepT->getOperator());
4392 break;
4393 }
John McCall14606042011-06-30 08:33:18 +00004394
4395 case TemplateName::SubstTemplateTemplateParm: {
4396 SubstTemplateTemplateParmStorage *subst
4397 = Name.getAsSubstTemplateTemplateParm();
4398 AddDeclRef(subst->getParameter(), Record);
4399 AddTemplateName(subst->getReplacement(), Record);
4400 break;
4401 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004402
4403 case TemplateName::SubstTemplateTemplateParmPack: {
4404 SubstTemplateTemplateParmPackStorage *SubstPack
4405 = Name.getAsSubstTemplateTemplateParmPack();
4406 AddDeclRef(SubstPack->getParameterPack(), Record);
4407 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4408 break;
4409 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004410 }
4411}
4412
Michael J. Spencer20249a12010-10-21 03:16:25 +00004413void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004414 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004415 Record.push_back(Arg.getKind());
4416 switch (Arg.getKind()) {
4417 case TemplateArgument::Null:
4418 break;
4419 case TemplateArgument::Type:
4420 AddTypeRef(Arg.getAsType(), Record);
4421 break;
4422 case TemplateArgument::Declaration:
4423 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004424 Record.push_back(Arg.isDeclForReferenceParam());
4425 break;
4426 case TemplateArgument::NullPtr:
4427 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004428 break;
4429 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004430 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004431 AddTypeRef(Arg.getIntegralType(), Record);
4432 break;
4433 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004434 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4435 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004436 case TemplateArgument::TemplateExpansion:
4437 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004438 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4439 Record.push_back(*NumExpansions + 1);
4440 else
4441 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004442 break;
4443 case TemplateArgument::Expression:
4444 AddStmt(Arg.getAsExpr());
4445 break;
4446 case TemplateArgument::Pack:
4447 Record.push_back(Arg.pack_size());
4448 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4449 I != E; ++I)
4450 AddTemplateArgument(*I, Record);
4451 break;
4452 }
4453}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004454
4455void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004456ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004457 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004458 assert(TemplateParams && "No TemplateParams!");
4459 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4460 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4461 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4462 Record.push_back(TemplateParams->size());
4463 for (TemplateParameterList::const_iterator
4464 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4465 P != PEnd; ++P)
4466 AddDeclRef(*P, Record);
4467}
4468
4469/// \brief Emit a template argument list.
4470void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004471ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004472 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004473 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004474 Record.push_back(TemplateArgs->size());
4475 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004476 AddTemplateArgument(TemplateArgs->get(i), Record);
4477}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004478
4479
4480void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004481ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004482 Record.push_back(Set.size());
4483 for (UnresolvedSetImpl::const_iterator
4484 I = Set.begin(), E = Set.end(); I != E; ++I) {
4485 AddDeclRef(I.getDecl(), Record);
4486 Record.push_back(I.getAccess());
4487 }
4488}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004489
Sebastian Redla4232eb2010-08-18 23:56:21 +00004490void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004491 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004492 Record.push_back(Base.isVirtual());
4493 Record.push_back(Base.isBaseOfClass());
4494 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004495 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004496 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004497 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004498 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4499 : SourceLocation(),
4500 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004501}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004502
Douglas Gregor7c789c12010-10-29 22:39:52 +00004503void ASTWriter::FlushCXXBaseSpecifiers() {
4504 RecordData Record;
4505 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4506 Record.clear();
4507
4508 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004509 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004510 if (Index == CXXBaseSpecifiersOffsets.size())
4511 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4512 else {
4513 if (Index > CXXBaseSpecifiersOffsets.size())
4514 CXXBaseSpecifiersOffsets.resize(Index + 1);
4515 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4516 }
4517
4518 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4519 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4520 Record.push_back(BEnd - B);
4521 for (; B != BEnd; ++B)
4522 AddCXXBaseSpecifier(*B, Record);
4523 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004524
4525 // Flush any expressions that were written as part of the base specifiers.
4526 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004527 }
4528
4529 CXXBaseSpecifiersToWrite.clear();
4530}
4531
Sean Huntcbb67482011-01-08 20:30:50 +00004532void ASTWriter::AddCXXCtorInitializers(
4533 const CXXCtorInitializer * const *CtorInitializers,
4534 unsigned NumCtorInitializers,
4535 RecordDataImpl &Record) {
4536 Record.push_back(NumCtorInitializers);
4537 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4538 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004539
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004540 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004541 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004542 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004543 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004544 } else if (Init->isDelegatingInitializer()) {
4545 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004546 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004547 } else if (Init->isMemberInitializer()){
4548 Record.push_back(CTOR_INITIALIZER_MEMBER);
4549 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004550 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004551 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4552 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004553 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004554
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004555 AddSourceLocation(Init->getMemberLocation(), Record);
4556 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004557 AddSourceLocation(Init->getLParenLoc(), Record);
4558 AddSourceLocation(Init->getRParenLoc(), Record);
4559 Record.push_back(Init->isWritten());
4560 if (Init->isWritten()) {
4561 Record.push_back(Init->getSourceOrder());
4562 } else {
4563 Record.push_back(Init->getNumArrayIndices());
4564 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4565 AddDeclRef(Init->getArrayIndex(i), Record);
4566 }
4567 }
4568}
4569
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004570void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4571 assert(D->DefinitionData);
4572 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004573 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004574 Record.push_back(Data.UserDeclaredConstructor);
4575 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004576 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004577 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004578 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004579 Record.push_back(Data.UserDeclaredDestructor);
4580 Record.push_back(Data.Aggregate);
4581 Record.push_back(Data.PlainOldData);
4582 Record.push_back(Data.Empty);
4583 Record.push_back(Data.Polymorphic);
4584 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004585 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004586 Record.push_back(Data.HasNoNonEmptyBases);
4587 Record.push_back(Data.HasPrivateFields);
4588 Record.push_back(Data.HasProtectedFields);
4589 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004590 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004591 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004592 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004593 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004594 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004595 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004596 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004597 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004598 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004599 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004600 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004601 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004602 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004603 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004604 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004605 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004606 Record.push_back(Data.DeclaredDefaultConstructor);
4607 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004608 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004609 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004610 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004611 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004612 Record.push_back(Data.FailedImplicitMoveConstructor);
4613 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004614 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004615
4616 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004617 if (Data.NumBases > 0)
4618 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4619 Record);
4620
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004621 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4622 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004623 if (Data.NumVBases > 0)
4624 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4625 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004626
4627 AddUnresolvedSet(Data.Conversions, Record);
4628 AddUnresolvedSet(Data.VisibleConversions, Record);
4629 // Data.Definition is the owning decl, no need to write it.
4630 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004631
4632 // Add lambda-specific data.
4633 if (Data.IsLambda) {
4634 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004635 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004636 Record.push_back(Lambda.NumCaptures);
4637 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004638 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004639 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004640 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004641 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4642 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4643 AddSourceLocation(Capture.getLocation(), Record);
4644 Record.push_back(Capture.isImplicit());
4645 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4646 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4647 AddDeclRef(Var, Record);
4648 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4649 : SourceLocation(),
4650 Record);
4651 }
4652 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004653}
4654
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004655void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004656 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004657 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004658 assert(FirstDeclID == NextDeclID &&
4659 FirstTypeID == NextTypeID &&
4660 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004661 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004662 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004663 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004664 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004665
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004666 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004667
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004668 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4669 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4670 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004671 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004672 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004673 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004674 NextDeclID = FirstDeclID;
4675 NextTypeID = FirstTypeID;
4676 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004677 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004678 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004679 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004680}
4681
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004682void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004683 IdentifierIDs[II] = ID;
4684}
4685
Douglas Gregora8235d62012-10-09 23:05:51 +00004686void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4687 MacroIDs[MI] = ID;
4688}
4689
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004690void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004691 // Always take the highest-numbered type index. This copes with an interesting
4692 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004693 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004694 // keep the higher-numbered entry so that we can properly write it out to
4695 // the AST file.
4696 TypeIdx &StoredIdx = TypeIdxs[T];
4697 if (Idx.getIndex() >= StoredIdx.getIndex())
4698 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004699}
4700
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004701void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004702 SelectorIDs[S] = ID;
4703}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004704
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004705void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004706 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004707 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004708 MacroDefinitions[MD] = ID;
4709}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004710
Douglas Gregora015cab2011-12-02 17:30:13 +00004711void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4712 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4713 SubmoduleIDs[Mod] = ID;
4714}
4715
Douglas Gregora8235d62012-10-09 23:05:51 +00004716void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4717 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4718}
4719
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004720void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004721 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004722 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004723 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4724 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004725 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004726 // A forward reference was mutated into a definition. Rewrite it.
4727 // FIXME: This happens during template instantiation, should we
4728 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004729 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004730 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004731 }
4732}
Douglas Gregora8235d62012-10-09 23:05:51 +00004733
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004734void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004735 assert(!WritingAST && "Already writing the AST!");
4736
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004737 // TU and namespaces are handled elsewhere.
4738 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4739 return;
4740
Douglas Gregor919814d2011-09-09 23:01:35 +00004741 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004742 return; // Not a source decl added to a DeclContext from PCH.
4743
4744 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004745 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004746}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004747
4748void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004749 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004750 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004751 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004752 return; // Not a source member added to a class from PCH.
4753 if (!isa<CXXMethodDecl>(D))
4754 return; // We are interested in lazily declared implicit methods.
4755
4756 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004757 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004758 UpdateRecord &Record = DeclUpdates[RD];
4759 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004760 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004761}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004762
4763void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4764 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004765 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004766 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004767 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004768 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004769 return; // Not a source specialization added to a template from PCH.
4770
4771 UpdateRecord &Record = DeclUpdates[TD];
4772 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004773 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004774}
Douglas Gregor89d99802010-11-30 06:16:57 +00004775
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004776void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4777 const FunctionDecl *D) {
4778 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004779 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004780 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004781 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004782 return; // Not a source specialization added to a template from PCH.
4783
4784 UpdateRecord &Record = DeclUpdates[TD];
4785 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004786 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004787}
4788
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004789void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004790 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004791 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004792 return; // Declaration not imported from PCH.
4793
4794 // Implicit decl from a PCH was defined.
4795 // FIXME: Should implicit definition be a separate FunctionDecl?
4796 RewriteDecl(D);
4797}
4798
Sebastian Redlf79a7192011-04-29 08:19:30 +00004799void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004800 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004801 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004802 return;
4803
4804 // Since the actual instantiation is delayed, this really means that we need
4805 // to update the instantiation location.
4806 UpdateRecord &Record = DeclUpdates[D];
4807 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4808 AddSourceLocation(
4809 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4810}
4811
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004812void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4813 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004814 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004815 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004816 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004817
4818 assert(IFD->getDefinition() && "Category on a class without a definition?");
4819 ObjCClassesWithCategories.insert(
4820 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004821}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004822
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004823
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004824void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4825 const ObjCPropertyDecl *OrigProp,
4826 const ObjCCategoryDecl *ClassExt) {
4827 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4828 if (!D)
4829 return;
4830
4831 assert(!WritingAST && "Already writing the AST!");
4832 if (!D->isFromASTFile())
4833 return; // Declaration not imported from PCH.
4834
4835 RewriteDecl(D);
4836}