blob: aaf4678ba29bf6999f14d042758ad890a3772f18 [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"
Douglas Gregor89d99802010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.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 Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
Sebastian Redlade50002010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Benjamin Kramer6e089c62011-04-24 17:44:50 +000056static llvm::StringRef data(const std::vector<T, Allocator> &v) {
57 if (v.empty()) return llvm::StringRef();
58 return llvm::StringRef(reinterpret_cast<const char*>(&v[0]),
59 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000060}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000061
62template <typename T>
63static llvm::StringRef data(const llvm::SmallVectorImpl<T> &v) {
64 return llvm::StringRef(reinterpret_cast<const char*>(v.data()),
65 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000066}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000071
Douglas Gregor2cf26342009-04-09 22:27:44 +000072namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000080
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
84 void VisitArrayType(const ArrayType *T);
85 void VisitFunctionType(const FunctionType *T);
86 void VisitTagType(const TagType *T);
87
88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
89#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl3397c552010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000095 assert(false && "Built-in types are never serialized");
96}
97
Sebastian Redl3397c552010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101}
102
Sebastian Redl3397c552010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106}
107
Sebastian Redl3397c552010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111}
112
Sebastian Redl3397c552010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117}
118
Sebastian Redl3397c552010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000122}
123
Sebastian Redl3397c552010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128}
129
Sebastian Redl3397c552010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134}
135
Sebastian Redl3397c552010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140}
141
Sebastian Redl3397c552010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145}
146
Sebastian Redl3397c552010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153}
154
Sebastian Redl3397c552010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160}
161
Sebastian Redl3397c552010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176}
177
Sebastian Redl3397c552010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181}
182
Sebastian Redl3397c552010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184 VisitFunctionType(T);
185 Record.push_back(T->getNumArgs());
186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
187 Writer.AddTypeRef(T->getArgType(I), Record);
188 Record.push_back(T->isVariadic());
189 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
198 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000199 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200}
201
Sebastian Redl3397c552010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000203 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000204 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000205}
John McCalled976492009-12-04 22:46:56 +0000206
Sebastian Redl3397c552010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000211 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000212}
213
Sebastian Redl3397c552010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000215 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000216 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217}
218
Sebastian Redl3397c552010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222}
223
Sebastian Redl3397c552010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000225 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000227}
228
Sean Huntca63c202011-05-24 22:41:36 +0000229void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
230 Writer.AddTypeRef(T->getBaseType(), Record);
231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
232 Record.push_back(T->getUTTKind());
233 Code = TYPE_UNARY_TRANSFORM;
234}
235
Richard Smith34b41d92011-02-20 03:19:35 +0000236void ASTTypeWriter::VisitAutoType(const AutoType *T) {
237 Writer.AddTypeRef(T->getDeducedType(), Record);
238 Code = TYPE_AUTO;
239}
240
Sebastian Redl3397c552010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000242 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000243 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000244 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000245 "Cannot serialize in the middle of a type definition");
246}
247
Sebastian Redl3397c552010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000250 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251}
252
Sebastian Redl3397c552010-08-18 23:56:27 +0000253void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000255 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256}
257
John McCall9d156a72011-01-06 01:58:22 +0000258void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
259 Writer.AddTypeRef(T->getModifiedType(), Record);
260 Writer.AddTypeRef(T->getEquivalentType(), Record);
261 Record.push_back(T->getAttrKind());
262 Code = TYPE_ATTRIBUTED;
263}
264
Mike Stump1eb44332009-09-09 15:08:12 +0000265void
Sebastian Redl3397c552010-08-18 23:56:27 +0000266ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000267 const SubstTemplateTypeParmType *T) {
268 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
269 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000270 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000271}
272
273void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000274ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
275 const SubstTemplateTypeParmPackType *T) {
276 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
277 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
278 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
279}
280
281void
Sebastian Redl3397c552010-08-18 23:56:27 +0000282ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000283 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000284 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000285 Writer.AddTemplateName(T->getTemplateName(), Record);
286 Record.push_back(T->getNumArgs());
287 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
288 ArgI != ArgE; ++ArgI)
289 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000290 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
291 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000292 : T->getCanonicalTypeInternal(),
293 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000294 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000295}
296
297void
Sebastian Redl3397c552010-08-18 23:56:27 +0000298ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000299 VisitArrayType(T);
300 Writer.AddStmt(T->getSizeExpr());
301 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl3397c552010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000307 const DependentSizedExtVectorType *T) {
308 // FIXME: Serialize this type (C++ only)
309 assert(false && "Cannot serialize dependent sized extended vector types");
310}
311
312void
Sebastian Redl3397c552010-08-18 23:56:27 +0000313ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314 Record.push_back(T->getDepth());
315 Record.push_back(T->getIndex());
316 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000317 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000318 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl3397c552010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000323 Record.push_back(T->getKeyword());
324 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
325 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000326 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
327 : T->getCanonicalTypeInternal(),
328 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000329 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000330}
331
332void
Sebastian Redl3397c552010-08-18 23:56:27 +0000333ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000334 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000335 Record.push_back(T->getKeyword());
336 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
337 Writer.AddIdentifierRef(T->getIdentifier(), Record);
338 Record.push_back(T->getNumArgs());
339 for (DependentTemplateSpecializationType::iterator
340 I = T->begin(), E = T->end(); I != E; ++I)
341 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000342 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000343}
344
Douglas Gregor7536dd52010-12-20 02:24:11 +0000345void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
346 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000347 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
348 Record.push_back(*NumExpansions + 1);
349 else
350 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000351 Code = TYPE_PACK_EXPANSION;
352}
353
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000354void ASTTypeWriter::VisitParenType(const ParenType *T) {
355 Writer.AddTypeRef(T->getInnerType(), Record);
356 Code = TYPE_PAREN;
357}
358
Sebastian Redl3397c552010-08-18 23:56:27 +0000359void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000360 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000361 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
362 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000363 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000364}
365
Sebastian Redl3397c552010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000367 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000368 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000369 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000370}
371
Sebastian Redl3397c552010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000373 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000374 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000375}
376
Sebastian Redl3397c552010-08-18 23:56:27 +0000377void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000378 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000379 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000380 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000381 E = T->qual_end(); I != E; ++I)
382 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000384}
385
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000386void
Sebastian Redl3397c552010-08-18 23:56:27 +0000387ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000390}
391
John McCalla1ee0c52009-10-16 21:56:05 +0000392namespace {
393
394class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000395 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000396 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000397
398public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000399 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000400 : Writer(Writer), Record(Record) { }
401
John McCall51bd8032009-10-18 01:05:36 +0000402#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000403#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000404 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000405#include "clang/AST/TypeLocNodes.def"
406
John McCall51bd8032009-10-18 01:05:36 +0000407 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
408 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000409};
410
411}
412
John McCall51bd8032009-10-18 01:05:36 +0000413void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
414 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000415}
John McCall51bd8032009-10-18 01:05:36 +0000416void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000417 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
418 if (TL.needsExtraLocalData()) {
419 Record.push_back(TL.getWrittenTypeSpec());
420 Record.push_back(TL.getWrittenSignSpec());
421 Record.push_back(TL.getWrittenWidthSpec());
422 Record.push_back(TL.hasModeAttr());
423 }
John McCalla1ee0c52009-10-16 21:56:05 +0000424}
John McCall51bd8032009-10-18 01:05:36 +0000425void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000430}
John McCall51bd8032009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000433}
John McCall51bd8032009-10-18 01:05:36 +0000434void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000436}
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000442 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
446 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
447 Record.push_back(TL.getSizeExpr() ? 1 : 0);
448 if (TL.getSizeExpr())
449 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000450}
John McCall51bd8032009-10-18 01:05:36 +0000451void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
452 VisitArrayTypeLoc(TL);
453}
454void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
455 VisitArrayTypeLoc(TL);
456}
457void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
458 VisitArrayTypeLoc(TL);
459}
460void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
461 DependentSizedArrayTypeLoc TL) {
462 VisitArrayTypeLoc(TL);
463}
464void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
465 DependentSizedExtVectorTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
468void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
474void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000475 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
476 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000477 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000478 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
479 Writer.AddDeclRef(TL.getArg(i), Record);
480}
481void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
482 VisitFunctionTypeLoc(TL);
483}
484void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
485 VisitFunctionTypeLoc(TL);
486}
John McCalled976492009-12-04 22:46:56 +0000487void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
John McCall51bd8032009-10-18 01:05:36 +0000490void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getNameLoc(), Record);
492}
493void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000494 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
495 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
496 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000497}
498void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
500 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
501 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
502 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000503}
504void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
Sean Huntca63c202011-05-24 22:41:36 +0000507void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getKWLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
511 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
512}
Richard Smith34b41d92011-02-20 03:19:35 +0000513void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
John McCall51bd8032009-10-18 01:05:36 +0000516void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
519void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getNameLoc(), Record);
521}
John McCall9d156a72011-01-06 01:58:22 +0000522void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
523 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
524 if (TL.hasAttrOperand()) {
525 SourceRange range = TL.getAttrOperandParensRange();
526 Writer.AddSourceLocation(range.getBegin(), Record);
527 Writer.AddSourceLocation(range.getEnd(), Record);
528 }
529 if (TL.hasAttrExprOperand()) {
530 Expr *operand = TL.getAttrExprOperand();
531 Record.push_back(operand ? 1 : 0);
532 if (operand) Writer.AddStmt(operand);
533 } else if (TL.hasAttrEnumOperand()) {
534 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
535 }
536}
John McCall51bd8032009-10-18 01:05:36 +0000537void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
John McCall49a832b2009-10-18 09:09:24 +0000540void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
541 SubstTemplateTypeParmTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000544void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
545 SubstTemplateTypeParmPackTypeLoc TL) {
546 Writer.AddSourceLocation(TL.getNameLoc(), Record);
547}
John McCall51bd8032009-10-18 01:05:36 +0000548void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
549 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000550 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
551 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
552 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
553 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000554 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
555 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000556}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000557void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
559 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
560}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000561void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000562 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000563 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000564}
John McCall3cb0ebd2010-03-10 03:28:59 +0000565void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
567}
Douglas Gregor4714c122010-03-31 17:34:00 +0000568void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000569 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000570 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCall33500952010-06-11 00:33:02 +0000573void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
574 DependentTemplateSpecializationTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000576 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
579 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
580 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000581 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
582 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000583}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000584void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
586}
John McCall51bd8032009-10-18 01:05:36 +0000587void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000589}
590void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
591 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000592 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
593 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
594 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
595 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000596}
John McCall54e14c42009-10-22 22:37:11 +0000597void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000599}
John McCalla1ee0c52009-10-16 21:56:05 +0000600
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000601//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000602// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000603//===----------------------------------------------------------------------===//
604
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000605static void EmitBlockID(unsigned ID, const char *Name,
606 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000607 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000608 Record.clear();
609 Record.push_back(ID);
610 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
611
612 // Emit the block name if present.
613 if (Name == 0 || Name[0] == 0) return;
614 Record.clear();
615 while (*Name)
616 Record.push_back(*Name++);
617 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
618}
619
620static void EmitRecordID(unsigned ID, const char *Name,
621 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000622 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000623 Record.clear();
624 Record.push_back(ID);
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000628}
629
630static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000632#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000633 RECORD(STMT_STOP);
634 RECORD(STMT_NULL_PTR);
635 RECORD(STMT_NULL);
636 RECORD(STMT_COMPOUND);
637 RECORD(STMT_CASE);
638 RECORD(STMT_DEFAULT);
639 RECORD(STMT_LABEL);
640 RECORD(STMT_IF);
641 RECORD(STMT_SWITCH);
642 RECORD(STMT_WHILE);
643 RECORD(STMT_DO);
644 RECORD(STMT_FOR);
645 RECORD(STMT_GOTO);
646 RECORD(STMT_INDIRECT_GOTO);
647 RECORD(STMT_CONTINUE);
648 RECORD(STMT_BREAK);
649 RECORD(STMT_RETURN);
650 RECORD(STMT_DECL);
651 RECORD(STMT_ASM);
652 RECORD(EXPR_PREDEFINED);
653 RECORD(EXPR_DECL_REF);
654 RECORD(EXPR_INTEGER_LITERAL);
655 RECORD(EXPR_FLOATING_LITERAL);
656 RECORD(EXPR_IMAGINARY_LITERAL);
657 RECORD(EXPR_STRING_LITERAL);
658 RECORD(EXPR_CHARACTER_LITERAL);
659 RECORD(EXPR_PAREN);
660 RECORD(EXPR_UNARY_OPERATOR);
661 RECORD(EXPR_SIZEOF_ALIGN_OF);
662 RECORD(EXPR_ARRAY_SUBSCRIPT);
663 RECORD(EXPR_CALL);
664 RECORD(EXPR_MEMBER);
665 RECORD(EXPR_BINARY_OPERATOR);
666 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
667 RECORD(EXPR_CONDITIONAL_OPERATOR);
668 RECORD(EXPR_IMPLICIT_CAST);
669 RECORD(EXPR_CSTYLE_CAST);
670 RECORD(EXPR_COMPOUND_LITERAL);
671 RECORD(EXPR_EXT_VECTOR_ELEMENT);
672 RECORD(EXPR_INIT_LIST);
673 RECORD(EXPR_DESIGNATED_INIT);
674 RECORD(EXPR_IMPLICIT_VALUE_INIT);
675 RECORD(EXPR_VA_ARG);
676 RECORD(EXPR_ADDR_LABEL);
677 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000678 RECORD(EXPR_CHOOSE);
679 RECORD(EXPR_GNU_NULL);
680 RECORD(EXPR_SHUFFLE_VECTOR);
681 RECORD(EXPR_BLOCK);
682 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000683 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000684 RECORD(EXPR_OBJC_STRING_LITERAL);
685 RECORD(EXPR_OBJC_ENCODE);
686 RECORD(EXPR_OBJC_SELECTOR_EXPR);
687 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
688 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
689 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
690 RECORD(EXPR_OBJC_KVC_REF_EXPR);
691 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000692 RECORD(STMT_OBJC_FOR_COLLECTION);
693 RECORD(STMT_OBJC_CATCH);
694 RECORD(STMT_OBJC_FINALLY);
695 RECORD(STMT_OBJC_AT_TRY);
696 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
697 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000698 RECORD(EXPR_CXX_OPERATOR_CALL);
699 RECORD(EXPR_CXX_CONSTRUCT);
700 RECORD(EXPR_CXX_STATIC_CAST);
701 RECORD(EXPR_CXX_DYNAMIC_CAST);
702 RECORD(EXPR_CXX_REINTERPRET_CAST);
703 RECORD(EXPR_CXX_CONST_CAST);
704 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
705 RECORD(EXPR_CXX_BOOL_LITERAL);
706 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000707 RECORD(EXPR_CXX_TYPEID_EXPR);
708 RECORD(EXPR_CXX_TYPEID_TYPE);
709 RECORD(EXPR_CXX_UUIDOF_EXPR);
710 RECORD(EXPR_CXX_UUIDOF_TYPE);
711 RECORD(EXPR_CXX_THIS);
712 RECORD(EXPR_CXX_THROW);
713 RECORD(EXPR_CXX_DEFAULT_ARG);
714 RECORD(EXPR_CXX_BIND_TEMPORARY);
715 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
716 RECORD(EXPR_CXX_NEW);
717 RECORD(EXPR_CXX_DELETE);
718 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
719 RECORD(EXPR_EXPR_WITH_CLEANUPS);
720 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
721 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
722 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
723 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
724 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
725 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
726 RECORD(EXPR_CXX_NOEXCEPT);
727 RECORD(EXPR_OPAQUE_VALUE);
728 RECORD(EXPR_BINARY_TYPE_TRAIT);
729 RECORD(EXPR_PACK_EXPANSION);
730 RECORD(EXPR_SIZEOF_PACK);
731 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000732 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000733#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000734}
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Sebastian Redla4232eb2010-08-18 23:56:21 +0000736void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000737 RecordData Record;
738 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000740#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
741#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Sebastian Redl3397c552010-08-18 23:56:27 +0000743 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000744 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000745 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000746 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000747 RECORD(TYPE_OFFSET);
748 RECORD(DECL_OFFSET);
749 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000750 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000751 RECORD(IDENTIFIER_OFFSET);
752 RECORD(IDENTIFIER_TABLE);
753 RECORD(EXTERNAL_DEFINITIONS);
754 RECORD(SPECIAL_TYPES);
755 RECORD(STATISTICS);
756 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000757 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000758 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
759 RECORD(SELECTOR_OFFSETS);
760 RECORD(METHOD_POOL);
761 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000762 RECORD(SOURCE_LOCATION_OFFSETS);
763 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000764 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000765 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000766 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000767 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000768 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000769 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000770 RECORD(TU_UPDATE_LEXICAL);
771 RECORD(REDECLS_UPDATE_LATEST);
772 RECORD(SEMA_DECL_REFS);
773 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
774 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
775 RECORD(DECL_REPLACEMENTS);
776 RECORD(UPDATE_VISIBLE);
777 RECORD(DECL_UPDATE_OFFSETS);
778 RECORD(DECL_UPDATES);
779 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
780 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000781 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000782 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000783 RECORD(FP_PRAGMA_OPTIONS);
784 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000785 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000786 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
787 RECORD(KNOWN_NAMESPACES);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000788
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000789 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000790 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000791 RECORD(SM_SLOC_FILE_ENTRY);
792 RECORD(SM_SLOC_BUFFER_ENTRY);
793 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000794 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000796 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000797 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000798 RECORD(PP_MACRO_OBJECT_LIKE);
799 RECORD(PP_MACRO_FUNCTION_LIKE);
800 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000801
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000802 // Decls and Types block.
803 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000804 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000805 RECORD(TYPE_COMPLEX);
806 RECORD(TYPE_POINTER);
807 RECORD(TYPE_BLOCK_POINTER);
808 RECORD(TYPE_LVALUE_REFERENCE);
809 RECORD(TYPE_RVALUE_REFERENCE);
810 RECORD(TYPE_MEMBER_POINTER);
811 RECORD(TYPE_CONSTANT_ARRAY);
812 RECORD(TYPE_INCOMPLETE_ARRAY);
813 RECORD(TYPE_VARIABLE_ARRAY);
814 RECORD(TYPE_VECTOR);
815 RECORD(TYPE_EXT_VECTOR);
816 RECORD(TYPE_FUNCTION_PROTO);
817 RECORD(TYPE_FUNCTION_NO_PROTO);
818 RECORD(TYPE_TYPEDEF);
819 RECORD(TYPE_TYPEOF_EXPR);
820 RECORD(TYPE_TYPEOF);
821 RECORD(TYPE_RECORD);
822 RECORD(TYPE_ENUM);
823 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000824 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000825 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000826 RECORD(TYPE_DECLTYPE);
827 RECORD(TYPE_ELABORATED);
828 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
829 RECORD(TYPE_UNRESOLVED_USING);
830 RECORD(TYPE_INJECTED_CLASS_NAME);
831 RECORD(TYPE_OBJC_OBJECT);
832 RECORD(TYPE_TEMPLATE_TYPE_PARM);
833 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
834 RECORD(TYPE_DEPENDENT_NAME);
835 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
836 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
837 RECORD(TYPE_PAREN);
838 RECORD(TYPE_PACK_EXPANSION);
839 RECORD(TYPE_ATTRIBUTED);
840 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000841 RECORD(DECL_TRANSLATION_UNIT);
842 RECORD(DECL_TYPEDEF);
843 RECORD(DECL_ENUM);
844 RECORD(DECL_RECORD);
845 RECORD(DECL_ENUM_CONSTANT);
846 RECORD(DECL_FUNCTION);
847 RECORD(DECL_OBJC_METHOD);
848 RECORD(DECL_OBJC_INTERFACE);
849 RECORD(DECL_OBJC_PROTOCOL);
850 RECORD(DECL_OBJC_IVAR);
851 RECORD(DECL_OBJC_AT_DEFS_FIELD);
852 RECORD(DECL_OBJC_CLASS);
853 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
854 RECORD(DECL_OBJC_CATEGORY);
855 RECORD(DECL_OBJC_CATEGORY_IMPL);
856 RECORD(DECL_OBJC_IMPLEMENTATION);
857 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
858 RECORD(DECL_OBJC_PROPERTY);
859 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000860 RECORD(DECL_FIELD);
861 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000862 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000863 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000864 RECORD(DECL_FILE_SCOPE_ASM);
865 RECORD(DECL_BLOCK);
866 RECORD(DECL_CONTEXT_LEXICAL);
867 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000868 RECORD(DECL_NAMESPACE);
869 RECORD(DECL_NAMESPACE_ALIAS);
870 RECORD(DECL_USING);
871 RECORD(DECL_USING_SHADOW);
872 RECORD(DECL_USING_DIRECTIVE);
873 RECORD(DECL_UNRESOLVED_USING_VALUE);
874 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
875 RECORD(DECL_LINKAGE_SPEC);
876 RECORD(DECL_CXX_RECORD);
877 RECORD(DECL_CXX_METHOD);
878 RECORD(DECL_CXX_CONSTRUCTOR);
879 RECORD(DECL_CXX_DESTRUCTOR);
880 RECORD(DECL_CXX_CONVERSION);
881 RECORD(DECL_ACCESS_SPEC);
882 RECORD(DECL_FRIEND);
883 RECORD(DECL_FRIEND_TEMPLATE);
884 RECORD(DECL_CLASS_TEMPLATE);
885 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
886 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
887 RECORD(DECL_FUNCTION_TEMPLATE);
888 RECORD(DECL_TEMPLATE_TYPE_PARM);
889 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
890 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
891 RECORD(DECL_STATIC_ASSERT);
892 RECORD(DECL_CXX_BASE_SPECIFIERS);
893 RECORD(DECL_INDIRECTFIELD);
894 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
895
Douglas Gregora72d8c42011-06-03 02:27:19 +0000896 // Statements and Exprs can occur in the Decls and Types block.
897 AddStmtsExprs(Stream, Record);
898
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000899 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000900 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000901 RECORD(PPD_MACRO_DEFINITION);
902 RECORD(PPD_INCLUSION_DIRECTIVE);
903
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000904#undef RECORD
905#undef BLOCK
906 Stream.ExitBlock();
907}
908
Douglas Gregore650c8c2009-07-07 00:12:59 +0000909/// \brief Adjusts the given filename to only write out the portion of the
910/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000911///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000912/// \param Filename the file name to adjust.
913///
914/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
915/// the returned filename will be adjusted by this system root.
916///
917/// \returns either the original filename (if it needs no adjustment) or the
918/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000919static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000920adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
921 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregore650c8c2009-07-07 00:12:59 +0000923 if (!isysroot)
924 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Douglas Gregore650c8c2009-07-07 00:12:59 +0000926 // Verify that the filename and the system root have the same prefix.
927 unsigned Pos = 0;
928 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
929 if (Filename[Pos] != isysroot[Pos])
930 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Douglas Gregore650c8c2009-07-07 00:12:59 +0000932 // We hit the end of the filename before we hit the end of the system root.
933 if (!Filename[Pos])
934 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregore650c8c2009-07-07 00:12:59 +0000936 // If the file name has a '/' at the current position, skip over the '/'.
937 // We distinguish sysroot-based includes from absolute includes by the
938 // absence of '/' at the beginning of sysroot-based includes.
939 if (Filename[Pos] == '/')
940 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregore650c8c2009-07-07 00:12:59 +0000942 return Filename + Pos;
943}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000944
Sebastian Redl3397c552010-08-18 23:56:27 +0000945/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000946void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
947 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000948 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000949
Douglas Gregore650c8c2009-07-07 00:12:59 +0000950 // Metadata
951 const TargetInfo &Target = Context.Target;
952 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000953 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000954 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000955 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
956 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
959 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000960 // Target triple or chained PCH name
961 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000965 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
966 Record.push_back(VERSION_MAJOR);
967 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 Record.push_back(CLANG_VERSION_MAJOR);
969 Record.push_back(CLANG_VERSION_MINOR);
970 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000971 // FIXME: This writes the absolute path for chained headers.
972 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
973 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregor31d375f2011-05-06 21:43:30 +0000975 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +0000976 SourceManager &SM = Context.getSourceManager();
977 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
978 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000979 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000980 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
981 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
982
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000983 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000985 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000986
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000987 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000988 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000989 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000990 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000991 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000992 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000993
994 Record.clear();
995 Record.push_back(SM.getMainFileID().getOpaqueValue());
996 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000997 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000998
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000999 // Original PCH directory
1000 if (!OutputFile.empty() && OutputFile != "-") {
1001 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1002 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1004 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1005
1006 llvm::SmallString<128> OutputPath(OutputFile);
1007
1008 llvm::sys::fs::make_absolute(OutputPath);
1009 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1010
1011 RecordData Record;
1012 Record.push_back(ORIGINAL_PCH_DIR);
1013 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1014 }
1015
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001016 // Repository branch/version information.
1017 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001018 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001019 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1020 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001021 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001022 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001023 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1024 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001025}
1026
1027/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001028void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001029 RecordData Record;
1030 Record.push_back(LangOpts.Trigraphs);
1031 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1032 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1033 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1034 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001035 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001036 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1037 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1038 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1039 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00001040 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001041 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001042 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1043 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001044 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1045 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001046 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001048 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1049 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001050 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001051 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001052 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001053 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001054 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001055 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1056 // properties enabled.
Douglas Gregor74da19f2011-06-14 23:20:43 +00001057 Record.push_back(LangOpts.ObjCInferRelatedResultType);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001058 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001060 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001061 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1062 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001063 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001064 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001065 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001066 Record.push_back(LangOpts.CXXExceptions);
1067 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001068
Douglas Gregor6f755502011-02-01 15:15:22 +00001069 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001070 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1071 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1072 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1073
Chris Lattnerea5ce472009-04-27 07:35:58 +00001074 // Whether static initializers are protected by locks.
1075 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001076 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001077 Record.push_back(LangOpts.Blocks); // block extension to C
1078 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1079 // they are unused.
1080 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1081 // (modulo the platform support).
1082
Chris Lattnera4d71452010-06-26 21:25:03 +00001083 Record.push_back(LangOpts.getSignedOverflowBehavior());
1084 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001085
1086 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001087 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001088 // defined.
1089 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1090 // opposed to __DYNAMIC__).
1091 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1092
1093 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1094 // used (instead of C99 semantics).
1095 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00001096 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001097 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1098 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001099 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1100 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001101 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001102 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1103 // to the smallest integer type with
1104 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001105 Record.push_back(LangOpts.getGCMode());
1106 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001107 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001108 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001109 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001110 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001111 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001112 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001113 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001114 Record.push_back(LangOpts.SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00001115 Record.push_back(LangOpts.MRTD);
John McCallf85e1932011-06-15 23:02:42 +00001116 Record.push_back(LangOpts.ObjCAutoRefCount);
1117 Record.push_back(LangOpts.ObjCInferRelatedReturnType);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001118 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001119}
1120
Douglas Gregor14f79002009-04-10 03:52:48 +00001121//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001122// stat cache Serialization
1123//===----------------------------------------------------------------------===//
1124
1125namespace {
1126// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001127class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128public:
1129 typedef const char * key_type;
1130 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Chris Lattner74e976b2010-11-23 19:28:12 +00001132 typedef struct stat data_type;
1133 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001134
1135 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001136 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
1139 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001140 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1141 data_type_ref Data) {
1142 unsigned StrLen = strlen(path);
1143 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001144 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001145 clang::io::Emit8(Out, DataLen);
1146 return std::make_pair(StrLen + 1, DataLen);
1147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001149 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1150 Out.write(path, KeyLen);
1151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattner74e976b2010-11-23 19:28:12 +00001153 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001154 data_type_ref Data, unsigned DataLen) {
1155 using namespace clang::io;
1156 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattner74e976b2010-11-23 19:28:12 +00001158 Emit32(Out, (uint32_t) Data.st_ino);
1159 Emit32(Out, (uint32_t) Data.st_dev);
1160 Emit16(Out, (uint16_t) Data.st_mode);
1161 Emit64(Out, (uint64_t) Data.st_mtime);
1162 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001163
1164 assert(Out.tell() - Start == DataLen && "Wrong data length");
1165 }
1166};
1167} // end anonymous namespace
1168
Sebastian Redl3397c552010-08-18 23:56:27 +00001169/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001170void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001171 // Build the on-disk hash table containing information about every
1172 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001173 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001174 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001175 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001176 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001177 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001178 llvm::StringRef Filename = Stat->first();
1179 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001180 }
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001182 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001183 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001184 uint32_t BucketOffset;
1185 {
1186 llvm::raw_svector_ostream Out(StatCacheData);
1187 // Make sure that no bucket is at offset 0
1188 clang::io::Emit32(Out, 0);
1189 BucketOffset = Generator.Emit(Out);
1190 }
1191
1192 // Create a blob abbreviation
1193 using namespace llvm;
1194 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001195 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1199 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1200
1201 // Write the stat cache
1202 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001203 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001204 Record.push_back(BucketOffset);
1205 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001206 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001207}
1208
1209//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001210// Source Manager Serialization
1211//===----------------------------------------------------------------------===//
1212
1213/// \brief Create an abbreviation for the SLocEntry that refers to a
1214/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001215static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001216 using namespace llvm;
1217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001218 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001223 // FileEntry fields.
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001227 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001228}
1229
1230/// \brief Create an abbreviation for the SLocEntry that refers to a
1231/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001232static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001233 using namespace llvm;
1234 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001235 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001241 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001242}
1243
1244/// \brief Create an abbreviation for the SLocEntry that refers to a
1245/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001246static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001247 using namespace llvm;
1248 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001249 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001250 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001251 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001252}
1253
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001254/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1255/// expansion.
1256static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001257 using namespace llvm;
1258 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001259 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001265 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001266}
1267
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001268namespace {
1269 // Trait used for the on-disk hash table of header search information.
1270 class HeaderFileInfoTrait {
1271 ASTWriter &Writer;
1272 HeaderSearch &HS;
1273
1274 public:
1275 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1276 : Writer(Writer), HS(HS) { }
1277
1278 typedef const char *key_type;
1279 typedef key_type key_type_ref;
1280
1281 typedef HeaderFileInfo data_type;
1282 typedef const data_type &data_type_ref;
1283
1284 static unsigned ComputeHash(const char *path) {
1285 // The hash is based only on the filename portion of the key, so that the
1286 // reader can match based on filenames when symlinking or excess path
1287 // elements ("foo/../", "../") change the form of the name. However,
1288 // complete path is still the key.
1289 return llvm::HashString(llvm::sys::path::filename(path));
1290 }
1291
1292 std::pair<unsigned,unsigned>
1293 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1294 data_type_ref Data) {
1295 unsigned StrLen = strlen(path);
1296 clang::io::Emit16(Out, StrLen);
1297 unsigned DataLen = 1 + 2 + 4;
1298 clang::io::Emit8(Out, DataLen);
1299 return std::make_pair(StrLen + 1, DataLen);
1300 }
1301
1302 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1303 Out.write(path, KeyLen);
1304 }
1305
1306 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1307 data_type_ref Data, unsigned DataLen) {
1308 using namespace clang::io;
1309 uint64_t Start = Out.tell(); (void)Start;
1310
Douglas Gregordd3e5542011-05-04 00:14:37 +00001311 unsigned char Flags = (Data.isImport << 4)
1312 | (Data.isPragmaOnce << 3)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001313 | (Data.DirInfo << 1)
1314 | Data.Resolved;
1315 Emit8(Out, (uint8_t)Flags);
1316 Emit16(Out, (uint16_t) Data.NumIncludes);
1317
1318 if (!Data.ControllingMacro)
1319 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1320 else
1321 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1322 assert(Out.tell() - Start == DataLen && "Wrong data length");
1323 }
1324 };
1325} // end anonymous namespace
1326
1327/// \brief Write the header search block for the list of files that
1328///
1329/// \param HS The header search structure to save.
1330///
1331/// \param Chain Whether we're creating a chained AST file.
1332void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1333 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1334 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1335
1336 if (FilesByUID.size() > HS.header_file_size())
1337 FilesByUID.resize(HS.header_file_size());
1338
1339 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1340 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1341 llvm::SmallVector<const char *, 4> SavedStrings;
1342 unsigned NumHeaderSearchEntries = 0;
1343 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1344 const FileEntry *File = FilesByUID[UID];
1345 if (!File)
1346 continue;
1347
1348 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1349 if (HFI.External && Chain)
1350 continue;
1351
1352 // Turn the file name into an absolute path, if it isn't already.
1353 const char *Filename = File->getName();
1354 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1355
1356 // If we performed any translation on the file name at all, we need to
1357 // save this string, since the generator will refer to it later.
1358 if (Filename != File->getName()) {
1359 Filename = strdup(Filename);
1360 SavedStrings.push_back(Filename);
1361 }
1362
1363 Generator.insert(Filename, HFI, GeneratorTrait);
1364 ++NumHeaderSearchEntries;
1365 }
1366
1367 // Create the on-disk hash table in a buffer.
1368 llvm::SmallString<4096> TableData;
1369 uint32_t BucketOffset;
1370 {
1371 llvm::raw_svector_ostream Out(TableData);
1372 // Make sure that no bucket is at offset 0
1373 clang::io::Emit32(Out, 0);
1374 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1375 }
1376
1377 // Create a blob abbreviation
1378 using namespace llvm;
1379 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1380 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1384 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1385
1386 // Write the stat cache
1387 RecordData Record;
1388 Record.push_back(HEADER_SEARCH_TABLE);
1389 Record.push_back(BucketOffset);
1390 Record.push_back(NumHeaderSearchEntries);
1391 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1392
1393 // Free all of the strings we had to duplicate.
1394 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1395 free((void*)SavedStrings[I]);
1396}
1397
Douglas Gregor14f79002009-04-10 03:52:48 +00001398/// \brief Writes the block containing the serialized form of the
1399/// source manager.
1400///
1401/// TODO: We should probably use an on-disk hash table (stored in a
1402/// blob), indexed based on the file name, so that we only create
1403/// entries for files that we actually need. In the common case (no
1404/// errors), we probably won't have to create file entries for any of
1405/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001406void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001407 const Preprocessor &PP,
1408 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001409 RecordData Record;
1410
Chris Lattnerf04ad692009-04-10 17:16:57 +00001411 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001412 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001413
1414 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001415 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1416 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1417 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001418 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001419
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001420 // Write out the source location entry table. We skip the first
1421 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001422 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001423 // Write out the offsets of only source location file entries.
1424 // We will go through them in ASTReader::validateFileEntries().
1425 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001426 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001427 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1428 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001429 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001430 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001431 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001432
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001433 // Record the offset of this source-location entry.
1434 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1435
1436 // Figure out which record code to use.
1437 unsigned Code;
1438 if (SLoc->isFile()) {
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001439 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001440 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001441 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1442 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001443 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001444 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001445 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001446 Record.clear();
1447 Record.push_back(Code);
1448
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001449 // Starting offset of this entry within this module, so skip the dummy.
1450 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001451 if (SLoc->isFile()) {
1452 const SrcMgr::FileInfo &File = SLoc->getFile();
1453 Record.push_back(File.getIncludeLoc().getRawEncoding());
1454 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1455 Record.push_back(File.hasLineDirectives());
1456
1457 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001458 if (Content->OrigEntry) {
1459 assert(Content->OrigEntry == Content->ContentsEntry &&
1460 "Writing to AST an overriden file is not supported");
1461
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001462 // The source location entry is a file. The blob associated
1463 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Douglas Gregor2d52be52010-03-21 22:49:54 +00001465 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001466 Record.push_back(Content->OrigEntry->getSize());
1467 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001468
Douglas Gregore650c8c2009-07-07 00:12:59 +00001469 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001470 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001471 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001472
1473 // Ask the file manager to fixup the relative path for us. This will
1474 // honor the working directory.
1475 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1476
1477 // FIXME: This call to make_absolute shouldn't be necessary, the
1478 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001479 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001480 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Douglas Gregore650c8c2009-07-07 00:12:59 +00001482 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001483 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001484 } else {
1485 // The source location entry is a buffer. The blob associated
1486 // with this entry contains the contents of the buffer.
1487
1488 // We add one to the size so that we capture the trailing NULL
1489 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1490 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001491 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001492 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001493 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001494 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1495 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001496 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001497 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001498 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001499 llvm::StringRef(Buffer->getBufferStart(),
1500 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001501
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001502 if (strcmp(Name, "<built-in>") == 0) {
1503 PreloadSLocs.push_back(SLocEntryOffsets.size());
1504 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001505 }
1506 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001507 // The source location entry is a macro expansion.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001508 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1509 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1510 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1511 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1512
1513 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001514 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001515 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001516 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001517 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001518 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001519 }
1520 }
1521
Douglas Gregorc9490c02009-04-16 22:23:12 +00001522 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523
1524 if (SLocEntryOffsets.empty())
1525 return;
1526
Sebastian Redl3397c552010-08-18 23:56:27 +00001527 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001528 // table is used for lazily loading source-location information.
1529 using namespace llvm;
1530 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001531 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1535 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001537 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001538 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001539 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001540 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001541 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001542
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001543 // If we have module dependencies, write the mapping from source locations to
1544 // their containing modules, so that the reader can build the remapping.
1545 if (Chain) {
1546 // The map consists solely of a blob with the following format:
1547 // *(offset:i32 len:i16 name:len*i8)
1548 // Sorted by offset.
1549 typedef std::pair<uint32_t, llvm::StringRef> ModuleOffset;
1550 llvm::SmallVector<ModuleOffset, 16> Modules;
1551 Modules.reserve(Chain->Modules.size());
1552 for (llvm::StringMap<ASTReader::PerFileData*>::const_iterator
1553 I = Chain->Modules.begin(), E = Chain->Modules.end();
1554 I != E; ++I) {
1555 Modules.push_back(ModuleOffset(I->getValue()->SLocEntryBaseOffset,
1556 I->getKey()));
1557 }
1558 std::sort(Modules.begin(), Modules.end());
1559
1560 Abbrev = new BitCodeAbbrev();
1561 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_MAP));
1562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1563 unsigned SLocMapAbbrev = Stream.EmitAbbrev(Abbrev);
1564 llvm::SmallString<2048> Buffer;
1565 {
1566 llvm::raw_svector_ostream Out(Buffer);
1567 for (llvm::SmallVector<ModuleOffset, 16>::iterator I = Modules.begin(),
1568 E = Modules.end();
1569 I != E; ++I) {
1570 io::Emit32(Out, I->first);
1571 io::Emit16(Out, I->second.size());
1572 Out.write(I->second.data(), I->second.size());
1573 }
1574 }
1575 Record.clear();
1576 Record.push_back(SOURCE_LOCATION_MAP);
1577 Stream.EmitRecordWithBlob(SLocMapAbbrev, Record,
1578 Buffer.data(), Buffer.size());
1579 }
1580
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001581 Abbrev = new BitCodeAbbrev();
1582 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1585 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1586
1587 Record.clear();
1588 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1589 Record.push_back(SLocFileEntryOffsets.size());
1590 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1591 data(SLocFileEntryOffsets));
1592
Sebastian Redl3397c552010-08-18 23:56:27 +00001593 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001594 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001595 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001596
1597 // Write the line table. It depends on remapping working, so it must come
1598 // after the source location offsets.
1599 if (SourceMgr.hasLineTable()) {
1600 LineTableInfo &LineTable = SourceMgr.getLineTable();
1601
1602 Record.clear();
1603 // Emit the file names
1604 Record.push_back(LineTable.getNumFilenames());
1605 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1606 // Emit the file name
1607 const char *Filename = LineTable.getFilename(I);
1608 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1609 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1610 Record.push_back(FilenameLen);
1611 if (FilenameLen)
1612 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1613 }
1614
1615 // Emit the line entries
1616 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1617 L != LEnd; ++L) {
1618 // Only emit entries for local files.
1619 if (L->first < 0)
1620 continue;
1621
1622 // Emit the file ID
1623 Record.push_back(L->first);
1624
1625 // Emit the line entries
1626 Record.push_back(L->second.size());
1627 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1628 LEEnd = L->second.end();
1629 LE != LEEnd; ++LE) {
1630 Record.push_back(LE->FileOffset);
1631 Record.push_back(LE->LineNo);
1632 Record.push_back(LE->FilenameID);
1633 Record.push_back((unsigned)LE->FileKind);
1634 Record.push_back(LE->IncludeOffset);
1635 }
1636 }
1637 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1638 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001639}
1640
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001641//===----------------------------------------------------------------------===//
1642// Preprocessor Serialization
1643//===----------------------------------------------------------------------===//
1644
Douglas Gregor9c736102011-02-10 18:20:09 +00001645static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1646 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1647 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1648 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1649 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1650 return X.first->getName().compare(Y.first->getName());
1651}
1652
Chris Lattner0b1fb982009-04-10 17:15:23 +00001653/// \brief Writes the block containing the serialized form of the
1654/// preprocessor.
1655///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001656void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001657 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001658
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001659 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1660 if (PP.getCounterValue() != 0) {
1661 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001662 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001663 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001664 }
1665
1666 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001667 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Sebastian Redl3397c552010-08-18 23:56:27 +00001669 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001670 // FIXME: use diagnostics subsystem for localization etc.
1671 if (PP.SawDateOrTime())
1672 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Douglas Gregorecdcb882010-10-20 22:00:55 +00001674
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001675 // Loop over all the macro definitions that are live at the end of the file,
1676 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001677 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001678
Douglas Gregor9c736102011-02-10 18:20:09 +00001679 // Construct the list of macro definitions that need to be serialized.
1680 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1681 MacrosToEmit;
1682 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001683 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1684 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001685 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001686 MacroDefinitionsSeen.insert(I->first);
1687 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1688 }
1689
1690 // Sort the set of macro definitions that need to be serialized by the
1691 // name of the macro, to provide a stable ordering.
1692 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1693 &compareMacroDefinitions);
1694
Douglas Gregor040a8042011-02-11 00:26:14 +00001695 // Resolve any identifiers that defined macros at the time they were
1696 // deserialized, adding them to the list of macros to emit (if appropriate).
1697 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1698 IdentifierInfo *Name
1699 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1700 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1701 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1702 }
1703
Douglas Gregor9c736102011-02-10 18:20:09 +00001704 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1705 const IdentifierInfo *Name = MacrosToEmit[I].first;
1706 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001707 if (!MI)
1708 continue;
1709
Sebastian Redl3397c552010-08-18 23:56:27 +00001710 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001711 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001712 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001713
1714 // FIXME: There is a (probably minor) optimization we could do here, if
1715 // the macro comes from the original PCH but the identifier comes from a
1716 // chained PCH, by storing the offset into the original PCH rather than
1717 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001718 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001719 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001720 continue;
1721
Douglas Gregor9c736102011-02-10 18:20:09 +00001722 AddIdentifierRef(Name, Record);
1723 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001724 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1725 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001727 unsigned Code;
1728 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001729 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001730 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001731 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001733 Record.push_back(MI->isC99Varargs());
1734 Record.push_back(MI->isGNUVarargs());
1735 Record.push_back(MI->getNumArgs());
1736 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1737 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001738 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001739 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001740
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001741 // If we have a detailed preprocessing record, record the macro definition
1742 // ID that corresponds to this macro.
1743 if (PPRec)
1744 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001745
Douglas Gregorc9490c02009-04-16 22:23:12 +00001746 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001747 Record.clear();
1748
Chris Lattnerdf961c22009-04-10 18:08:30 +00001749 // Emit the tokens array.
1750 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1751 // Note that we know that the preprocessor does not have any annotation
1752 // tokens in it because they are created by the parser, and thus can't be
1753 // in a macro definition.
1754 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Chris Lattnerdf961c22009-04-10 18:08:30 +00001756 Record.push_back(Tok.getLocation().getRawEncoding());
1757 Record.push_back(Tok.getLength());
1758
Chris Lattnerdf961c22009-04-10 18:08:30 +00001759 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1760 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001761 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001762 // FIXME: Should translate token kind to a stable encoding.
1763 Record.push_back(Tok.getKind());
1764 // FIXME: Should translate token flags to a stable encoding.
1765 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001767 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001768 Record.clear();
1769 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001770 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001771 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001772 Stream.ExitBlock();
1773
1774 if (PPRec)
1775 WritePreprocessorDetail(*PPRec);
1776}
1777
1778void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1779 if (PPRec.begin(Chain) == PPRec.end(Chain))
1780 return;
1781
1782 // Enter the preprocessor block.
1783 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001784
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001785 // If the preprocessor has a preprocessing record, emit it.
1786 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001787 using namespace llvm;
1788
1789 // Set up the abbreviation for
1790 unsigned InclusionAbbrev = 0;
1791 {
1792 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1793 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1800 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1801 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1802 }
1803
Douglas Gregor4c30bb12011-07-21 00:47:40 +00001804 unsigned IndexBase = Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001805 RecordData Record;
1806 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1807 EEnd = PPRec.end(Chain);
1808 E != EEnd; ++E) {
1809 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001810
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001811 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1812 // Record this macro definition's location.
1813 MacroID ID = getMacroDefinitionID(MD);
1814
1815 // Don't write the macro definition if it is from another AST file.
1816 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001817 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001818
Douglas Gregor89d99802010-11-30 06:16:57 +00001819 // Notify the serialization listener that we're serializing this entity.
1820 if (SerializationListener)
1821 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001822 Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001823
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001824 unsigned Position = ID - FirstMacroID;
1825 if (Position != MacroDefinitionOffsets.size()) {
1826 if (Position > MacroDefinitionOffsets.size())
1827 MacroDefinitionOffsets.resize(Position + 1);
1828
1829 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1830 } else
1831 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001832
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001833 Record.push_back(IndexBase + NumPreprocessingRecords++);
1834 Record.push_back(ID);
1835 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1836 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1837 AddIdentifierRef(MD->getName(), Record);
1838 AddSourceLocation(MD->getLocation(), Record);
1839 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1840 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001841 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001842
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001843 // Notify the serialization listener that we're serializing this entity.
1844 if (SerializationListener)
1845 SerializationListener->SerializedPreprocessedEntity(*E,
1846 Stream.GetCurrentBitNo());
1847
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001848 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001849 Record.push_back(IndexBase + NumPreprocessingRecords++);
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001850 AddSourceLocation(ME->getSourceRange().getBegin(), Record);
1851 AddSourceLocation(ME->getSourceRange().getEnd(), Record);
1852 AddIdentifierRef(ME->getName(), Record);
1853 Record.push_back(getMacroDefinitionID(ME->getDefinition()));
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001854 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001855 continue;
1856 }
1857
1858 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1859 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1860 Record.push_back(IndexBase + NumPreprocessingRecords++);
1861 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1862 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1863 Record.push_back(ID->getFileName().size());
1864 Record.push_back(ID->wasInQuotes());
1865 Record.push_back(static_cast<unsigned>(ID->getKind()));
1866 llvm::SmallString<64> Buffer;
1867 Buffer += ID->getFileName();
1868 Buffer += ID->getFile()->getName();
1869 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1870 continue;
1871 }
1872
1873 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1874 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001875 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001876
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001877 // Write the offsets table for the preprocessing record.
1878 if (NumPreprocessingRecords > 0) {
1879 // Write the offsets table for identifier IDs.
1880 using namespace llvm;
1881 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001882 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1886 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001887
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001888 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001889 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001890 Record.push_back(NumPreprocessingRecords);
1891 Record.push_back(MacroDefinitionOffsets.size());
1892 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001893 data(MacroDefinitionOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001894 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001895}
1896
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001897void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001898 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001899 for (Diagnostic::DiagStatePointsTy::const_iterator
1900 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1901 I != E; ++I) {
1902 const Diagnostic::DiagStatePoint &point = *I;
1903 if (point.Loc.isInvalid())
1904 continue;
1905
1906 Record.push_back(point.Loc.getRawEncoding());
1907 for (Diagnostic::DiagState::iterator
1908 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1909 unsigned diag = I->first, map = I->second;
1910 if (map & 0x10) { // mapping from a diagnostic pragma.
1911 Record.push_back(diag);
1912 Record.push_back(map & 0x7);
1913 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001914 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001915 Record.push_back(-1); // mark the end of the diag/map pairs for this
1916 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001917 }
1918
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001919 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001920 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001921}
1922
Anders Carlssonc8505782011-03-06 18:41:18 +00001923void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1924 if (CXXBaseSpecifiersOffsets.empty())
1925 return;
1926
1927 RecordData Record;
1928
1929 // Create a blob abbreviation for the C++ base specifiers offsets.
1930 using namespace llvm;
1931
1932 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1933 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1936 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1937
1938 // Write the selector offsets table.
1939 Record.clear();
1940 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1941 Record.push_back(CXXBaseSpecifiersOffsets.size());
1942 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001943 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00001944}
1945
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001946//===----------------------------------------------------------------------===//
1947// Type Serialization
1948//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001949
Sebastian Redl3397c552010-08-18 23:56:27 +00001950/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001951void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001952 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001953 if (Idx.getIndex() == 0) // we haven't seen this type before.
1954 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Douglas Gregor97475832010-10-05 18:37:06 +00001956 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001957
Douglas Gregor2cf26342009-04-09 22:27:44 +00001958 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001959 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001960 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001961 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001962 else if (TypeOffsets.size() < Index) {
1963 TypeOffsets.resize(Index + 1);
1964 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001965 }
1966
1967 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Douglas Gregor2cf26342009-04-09 22:27:44 +00001969 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001970 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001971
Douglas Gregora4923eb2009-11-16 21:35:15 +00001972 if (T.hasLocalNonFastQualifiers()) {
1973 Qualifiers Qs = T.getLocalQualifiers();
1974 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001975 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001977 } else {
1978 switch (T->getTypeClass()) {
1979 // For all of the concrete, non-dependent types, call the
1980 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001981#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001982 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001983#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001984#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001985 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001986 }
1987
1988 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001989 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001990
1991 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001992 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001993}
1994
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001995//===----------------------------------------------------------------------===//
1996// Declaration Serialization
1997//===----------------------------------------------------------------------===//
1998
Douglas Gregor2cf26342009-04-09 22:27:44 +00001999/// \brief Write the block containing all of the declaration IDs
2000/// lexically declared within the given DeclContext.
2001///
2002/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2003/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002004uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002005 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002006 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002007 return 0;
2008
Douglas Gregorc9490c02009-04-16 22:23:12 +00002009 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002010 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002011 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002012 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002013 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2014 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002015 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002016
Douglas Gregor25123082009-04-22 22:34:57 +00002017 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002018 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002019 return Offset;
2020}
2021
Sebastian Redla4232eb2010-08-18 23:56:21 +00002022void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002023 using namespace llvm;
2024 RecordData Record;
2025
2026 // Write the type offsets array
2027 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002028 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2031 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2032 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002033 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002034 Record.push_back(TypeOffsets.size());
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002035 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002036
2037 // Write the declaration offsets array
2038 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002039 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2042 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2043 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002044 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002045 Record.push_back(DeclOffsets.size());
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002046 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002047}
2048
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002049//===----------------------------------------------------------------------===//
2050// Global Method Pool and Selector Serialization
2051//===----------------------------------------------------------------------===//
2052
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002053namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002054// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002055class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002056 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002057
2058public:
2059 typedef Selector key_type;
2060 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Sebastian Redl5d050072010-08-04 17:20:04 +00002062 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002063 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002064 ObjCMethodList Instance, Factory;
2065 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002066 typedef const data_type& data_type_ref;
2067
Sebastian Redl3397c552010-08-18 23:56:27 +00002068 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002070 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002071 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002072 }
Mike Stump1eb44332009-09-09 15:08:12 +00002073
2074 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002075 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
2076 data_type_ref Methods) {
2077 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2078 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002079 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2080 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002081 Method = Method->Next)
2082 if (Method->Method)
2083 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002084 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002085 Method = Method->Next)
2086 if (Method->Method)
2087 DataLen += 4;
2088 clang::io::Emit16(Out, DataLen);
2089 return std::make_pair(KeyLen, DataLen);
2090 }
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Douglas Gregor83941df2009-04-25 17:48:32 +00002092 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002093 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002094 assert((Start >> 32) == 0 && "Selector key offset too large");
2095 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002096 unsigned N = Sel.getNumArgs();
2097 clang::io::Emit16(Out, N);
2098 if (N == 0)
2099 N = 1;
2100 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002101 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002102 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2103 }
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002105 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002106 data_type_ref Methods, unsigned DataLen) {
2107 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002108 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002109 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002110 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002111 Method = Method->Next)
2112 if (Method->Method)
2113 ++NumInstanceMethods;
2114
2115 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002116 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002117 Method = Method->Next)
2118 if (Method->Method)
2119 ++NumFactoryMethods;
2120
2121 clang::io::Emit16(Out, NumInstanceMethods);
2122 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002123 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002124 Method = Method->Next)
2125 if (Method->Method)
2126 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002127 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002128 Method = Method->Next)
2129 if (Method->Method)
2130 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002131
2132 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002133 }
2134};
2135} // end anonymous namespace
2136
Sebastian Redl059612d2010-08-03 21:58:15 +00002137/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002138///
2139/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002140/// in an on-disk hash table indexed by the selector. The hash table also
2141/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002142void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002143 using namespace llvm;
2144
Sebastian Redl059612d2010-08-03 21:58:15 +00002145 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002146 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002147 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002148 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002149 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002150 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002151 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002152 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002153
Sebastian Redl059612d2010-08-03 21:58:15 +00002154 // Create the on-disk hash table representation. We walk through every
2155 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002156 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002157 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002158 I = SelectorIDs.begin(), E = SelectorIDs.end();
2159 I != E; ++I) {
2160 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002161 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002162 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002163 I->second,
2164 ObjCMethodList(),
2165 ObjCMethodList()
2166 };
2167 if (F != SemaRef.MethodPool.end()) {
2168 Data.Instance = F->second.first;
2169 Data.Factory = F->second.second;
2170 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002171 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002172 // changed.
2173 if (Chain && I->second < FirstSelectorID) {
2174 // Selector already exists. Did it change?
2175 bool changed = false;
2176 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2177 M = M->Next) {
2178 if (M->Method->getPCHLevel() == 0)
2179 changed = true;
2180 }
2181 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2182 M = M->Next) {
2183 if (M->Method->getPCHLevel() == 0)
2184 changed = true;
2185 }
2186 if (!changed)
2187 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002188 } else if (Data.Instance.Method || Data.Factory.Method) {
2189 // A new method pool entry.
2190 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002191 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002192 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002193 }
2194
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002195 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002196 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002197 uint32_t BucketOffset;
2198 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002199 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002200 llvm::raw_svector_ostream Out(MethodPool);
2201 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002202 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002203 BucketOffset = Generator.Emit(Out, Trait);
2204 }
2205
2206 // Create a blob abbreviation
2207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002208 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2212 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2213
Douglas Gregor83941df2009-04-25 17:48:32 +00002214 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002215 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002216 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002217 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002218 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002219 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002220
2221 // Create a blob abbreviation for the selector table offsets.
2222 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002223 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00002225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2226 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2227
2228 // Write the selector offsets table.
2229 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002230 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002231 Record.push_back(SelectorOffsets.size());
2232 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002233 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002234 }
2235}
2236
Sebastian Redl3397c552010-08-18 23:56:27 +00002237/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002238void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002239 using namespace llvm;
2240 if (SemaRef.ReferencedSelectors.empty())
2241 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002242
Fariborz Jahanian32019832010-07-23 19:11:11 +00002243 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002244
Sebastian Redl3397c552010-08-18 23:56:27 +00002245 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002246 // very tricky to fix, and given that @selector shouldn't really appear in
2247 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002248 for (DenseMap<Selector, SourceLocation>::iterator S =
2249 SemaRef.ReferencedSelectors.begin(),
2250 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2251 Selector Sel = (*S).first;
2252 SourceLocation Loc = (*S).second;
2253 AddSelectorRef(Sel, Record);
2254 AddSourceLocation(Loc, Record);
2255 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002256 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002257}
2258
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002259//===----------------------------------------------------------------------===//
2260// Identifier Table Serialization
2261//===----------------------------------------------------------------------===//
2262
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002263namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002264class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002265 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002266 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002267
Douglas Gregora92193e2009-04-28 21:18:29 +00002268 /// \brief Determines whether this is an "interesting" identifier
2269 /// that needs a full IdentifierInfo structure written into the hash
2270 /// table.
2271 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2272 return II->isPoisoned() ||
2273 II->isExtensionToken() ||
2274 II->hasMacroDefinition() ||
2275 II->getObjCOrBuiltinID() ||
2276 II->getFETokenInfo<void>();
2277 }
2278
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002279public:
2280 typedef const IdentifierInfo* key_type;
2281 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002283 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002284 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Sebastian Redl3397c552010-08-18 23:56:27 +00002286 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002287 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002288
2289 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002290 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002291 }
Mike Stump1eb44332009-09-09 15:08:12 +00002292
2293 std::pair<unsigned,unsigned>
2294 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002295 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002296 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002297 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2298 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002299 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002300 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002301 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002302 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002303 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2304 DEnd = IdentifierResolver::end();
2305 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002306 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002307 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002308 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002309 // We emit the key length after the data length so that every
2310 // string is preceded by a 16-bit length. This matches the PTH
2311 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002312 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002313 return std::make_pair(KeyLen, DataLen);
2314 }
Mike Stump1eb44332009-09-09 15:08:12 +00002315
2316 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002317 unsigned KeyLen) {
2318 // Record the location of the key data. This is used when generating
2319 // the mapping from persistent IDs to strings.
2320 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002321 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002322 }
Mike Stump1eb44332009-09-09 15:08:12 +00002323
2324 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002325 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002326 if (!isInterestingIdentifier(II)) {
2327 clang::io::Emit32(Out, ID << 1);
2328 return;
2329 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002330
Douglas Gregora92193e2009-04-28 21:18:29 +00002331 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002332 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002333 bool hasMacroDefinition =
2334 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002335 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002336 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002337 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2338 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2339 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002340 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002341 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002342 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002343
Douglas Gregor37e26842009-04-21 23:56:24 +00002344 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002345 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002346
Douglas Gregor668c1a42009-04-21 22:25:48 +00002347 // Emit the declaration IDs in reverse order, because the
2348 // IdentifierResolver provides the declarations as they would be
2349 // visible (e.g., the function "stat" would come before the struct
2350 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2351 // adds declarations to the end of the list (so we need to see the
2352 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002353 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00002354 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002355 IdentifierResolver::end());
2356 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2357 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002358 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002359 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002360 }
2361};
2362} // end anonymous namespace
2363
Sebastian Redl3397c552010-08-18 23:56:27 +00002364/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002365///
2366/// The identifier table consists of a blob containing string data
2367/// (the actual identifiers themselves) and a separate "offsets" index
2368/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002369void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002370 using namespace llvm;
2371
2372 // Create and write out the blob that contains the identifier
2373 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002374 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002375 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002376 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002377
Douglas Gregor92b059e2009-04-28 20:33:11 +00002378 // Look for any identifiers that were named while processing the
2379 // headers, but are otherwise not needed. We add these to the hash
2380 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002381 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002382 // file.
2383 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2384 IDEnd = PP.getIdentifierTable().end();
2385 ID != IDEnd; ++ID)
2386 getIdentifierRef(ID->second);
2387
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002388 // Create the on-disk hash table representation. We only store offsets
2389 // for identifiers that appear here for the first time.
2390 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002391 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002392 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2393 ID != IDEnd; ++ID) {
2394 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002395 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002396 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002397 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002398
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002399 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002400 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002401 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002402 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002403 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002404 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002405 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002406 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002407 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002408 }
2409
2410 // Create a blob abbreviation
2411 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002412 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002415 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002416
2417 // Write the identifier table
2418 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002419 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002420 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002421 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002422 }
2423
2424 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002425 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002426 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2429 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2430
2431 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002432 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002433 Record.push_back(IdentifierOffsets.size());
2434 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002435 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002436}
2437
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002438//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002439// DeclContext's Name Lookup Table Serialization
2440//===----------------------------------------------------------------------===//
2441
2442namespace {
2443// Trait used for the on-disk hash table used in the method pool.
2444class ASTDeclContextNameLookupTrait {
2445 ASTWriter &Writer;
2446
2447public:
2448 typedef DeclarationName key_type;
2449 typedef key_type key_type_ref;
2450
2451 typedef DeclContext::lookup_result data_type;
2452 typedef const data_type& data_type_ref;
2453
2454 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2455
2456 unsigned ComputeHash(DeclarationName Name) {
2457 llvm::FoldingSetNodeID ID;
2458 ID.AddInteger(Name.getNameKind());
2459
2460 switch (Name.getNameKind()) {
2461 case DeclarationName::Identifier:
2462 ID.AddString(Name.getAsIdentifierInfo()->getName());
2463 break;
2464 case DeclarationName::ObjCZeroArgSelector:
2465 case DeclarationName::ObjCOneArgSelector:
2466 case DeclarationName::ObjCMultiArgSelector:
2467 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2468 break;
2469 case DeclarationName::CXXConstructorName:
2470 case DeclarationName::CXXDestructorName:
2471 case DeclarationName::CXXConversionFunctionName:
2472 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2473 break;
2474 case DeclarationName::CXXOperatorName:
2475 ID.AddInteger(Name.getCXXOverloadedOperator());
2476 break;
2477 case DeclarationName::CXXLiteralOperatorName:
2478 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2479 case DeclarationName::CXXUsingDirective:
2480 break;
2481 }
2482
2483 return ID.ComputeHash();
2484 }
2485
2486 std::pair<unsigned,unsigned>
2487 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2488 data_type_ref Lookup) {
2489 unsigned KeyLen = 1;
2490 switch (Name.getNameKind()) {
2491 case DeclarationName::Identifier:
2492 case DeclarationName::ObjCZeroArgSelector:
2493 case DeclarationName::ObjCOneArgSelector:
2494 case DeclarationName::ObjCMultiArgSelector:
2495 case DeclarationName::CXXConstructorName:
2496 case DeclarationName::CXXDestructorName:
2497 case DeclarationName::CXXConversionFunctionName:
2498 case DeclarationName::CXXLiteralOperatorName:
2499 KeyLen += 4;
2500 break;
2501 case DeclarationName::CXXOperatorName:
2502 KeyLen += 1;
2503 break;
2504 case DeclarationName::CXXUsingDirective:
2505 break;
2506 }
2507 clang::io::Emit16(Out, KeyLen);
2508
2509 // 2 bytes for num of decls and 4 for each DeclID.
2510 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2511 clang::io::Emit16(Out, DataLen);
2512
2513 return std::make_pair(KeyLen, DataLen);
2514 }
2515
2516 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2517 using namespace clang::io;
2518
2519 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2520 Emit8(Out, Name.getNameKind());
2521 switch (Name.getNameKind()) {
2522 case DeclarationName::Identifier:
2523 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2524 break;
2525 case DeclarationName::ObjCZeroArgSelector:
2526 case DeclarationName::ObjCOneArgSelector:
2527 case DeclarationName::ObjCMultiArgSelector:
2528 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2529 break;
2530 case DeclarationName::CXXConstructorName:
2531 case DeclarationName::CXXDestructorName:
2532 case DeclarationName::CXXConversionFunctionName:
2533 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2534 break;
2535 case DeclarationName::CXXOperatorName:
2536 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2537 Emit8(Out, Name.getCXXOverloadedOperator());
2538 break;
2539 case DeclarationName::CXXLiteralOperatorName:
2540 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2541 break;
2542 case DeclarationName::CXXUsingDirective:
2543 break;
2544 }
2545 }
2546
2547 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2548 data_type Lookup, unsigned DataLen) {
2549 uint64_t Start = Out.tell(); (void)Start;
2550 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2551 for (; Lookup.first != Lookup.second; ++Lookup.first)
2552 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2553
2554 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2555 }
2556};
2557} // end anonymous namespace
2558
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002559/// \brief Write the block containing all of the declaration IDs
2560/// visible from the given DeclContext.
2561///
2562/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002563/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002564uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2565 DeclContext *DC) {
2566 if (DC->getPrimaryContext() != DC)
2567 return 0;
2568
2569 // Since there is no name lookup into functions or methods, don't bother to
2570 // build a visible-declarations table for these entities.
2571 if (DC->isFunctionOrMethod())
2572 return 0;
2573
2574 // If not in C++, we perform name lookup for the translation unit via the
2575 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2576 // FIXME: In C++ we need the visible declarations in order to "see" the
2577 // friend declarations, is there a way to do this without writing the table ?
2578 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2579 return 0;
2580
2581 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002582 if (DC->hasExternalVisibleStorage())
2583 DC->MaterializeVisibleDeclsFromExternalStorage();
2584 else
2585 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002586
2587 // Serialize the contents of the mapping used for lookup. Note that,
2588 // although we have two very different code paths, the serialized
2589 // representation is the same for both cases: a declaration name,
2590 // followed by a size, followed by references to the visible
2591 // declarations that have that name.
2592 uint64_t Offset = Stream.GetCurrentBitNo();
2593 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2594 if (!Map || Map->empty())
2595 return 0;
2596
2597 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2598 ASTDeclContextNameLookupTrait Trait(*this);
2599
2600 // Create the on-disk hash table representation.
2601 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2602 D != DEnd; ++D) {
2603 DeclarationName Name = D->first;
2604 DeclContext::lookup_result Result = D->second.getLookupResult();
2605 Generator.insert(Name, Result, Trait);
2606 }
2607
2608 // Create the on-disk hash table in a buffer.
2609 llvm::SmallString<4096> LookupTable;
2610 uint32_t BucketOffset;
2611 {
2612 llvm::raw_svector_ostream Out(LookupTable);
2613 // Make sure that no bucket is at offset 0
2614 clang::io::Emit32(Out, 0);
2615 BucketOffset = Generator.Emit(Out, Trait);
2616 }
2617
2618 // Write the lookup table
2619 RecordData Record;
2620 Record.push_back(DECL_CONTEXT_VISIBLE);
2621 Record.push_back(BucketOffset);
2622 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2623 LookupTable.str());
2624
2625 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2626 ++NumVisibleDeclContexts;
2627 return Offset;
2628}
2629
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002630/// \brief Write an UPDATE_VISIBLE block for the given context.
2631///
2632/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2633/// DeclContext in a dependent AST file. As such, they only exist for the TU
2634/// (in C++) and for namespaces.
2635void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002636 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2637 if (!Map || Map->empty())
2638 return;
2639
2640 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2641 ASTDeclContextNameLookupTrait Trait(*this);
2642
2643 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002644 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2645 D != DEnd; ++D) {
2646 DeclarationName Name = D->first;
2647 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002648 // For any name that appears in this table, the results are complete, i.e.
2649 // they overwrite results from previous PCHs. Merging is always a mess.
2650 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002651 }
2652
2653 // Create the on-disk hash table in a buffer.
2654 llvm::SmallString<4096> LookupTable;
2655 uint32_t BucketOffset;
2656 {
2657 llvm::raw_svector_ostream Out(LookupTable);
2658 // Make sure that no bucket is at offset 0
2659 clang::io::Emit32(Out, 0);
2660 BucketOffset = Generator.Emit(Out, Trait);
2661 }
2662
2663 // Write the lookup table
2664 RecordData Record;
2665 Record.push_back(UPDATE_VISIBLE);
2666 Record.push_back(getDeclID(cast<Decl>(DC)));
2667 Record.push_back(BucketOffset);
2668 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2669}
2670
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002671/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2672void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2673 RecordData Record;
2674 Record.push_back(Opts.fp_contract);
2675 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2676}
2677
2678/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2679void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2680 if (!SemaRef.Context.getLangOptions().OpenCL)
2681 return;
2682
2683 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2684 RecordData Record;
2685#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2686#include "clang/Basic/OpenCLExtensions.def"
2687 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2688}
2689
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002690//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002691// General Serialization Routines
2692//===----------------------------------------------------------------------===//
2693
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002694/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002695void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002696 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002697 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2698 const Attr * A = *i;
2699 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2700 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002701
Sean Huntcf807c42010-08-18 23:23:40 +00002702#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002703
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002704 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002705}
2706
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002707void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002708 Record.push_back(Str.size());
2709 Record.insert(Record.end(), Str.begin(), Str.end());
2710}
2711
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002712void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2713 RecordDataImpl &Record) {
2714 Record.push_back(Version.getMajor());
2715 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2716 Record.push_back(*Minor + 1);
2717 else
2718 Record.push_back(0);
2719 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2720 Record.push_back(*Subminor + 1);
2721 else
2722 Record.push_back(0);
2723}
2724
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002725/// \brief Note that the identifier II occurs at the given offset
2726/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002727void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002728 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002729 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002730 // up earlier in the chain and thus don't need an offset.
2731 if (ID >= FirstIdentID)
2732 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002733}
2734
Douglas Gregor83941df2009-04-25 17:48:32 +00002735/// \brief Note that the selector Sel occurs at the given offset
2736/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002737void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002738 unsigned ID = SelectorIDs[Sel];
2739 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002740 // Don't record offsets for selectors that are also available in a different
2741 // file.
2742 if (ID < FirstSelectorID)
2743 return;
2744 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002745}
2746
Sebastian Redla4232eb2010-08-18 23:56:21 +00002747ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002748 : Stream(Stream), Chain(0), SerializationListener(0),
2749 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002750 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002751 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002752 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2753 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002754 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002755 NumVisibleDeclContexts(0),
2756 FirstCXXBaseSpecifiersID(1), NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002757 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002758 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2759 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2760 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002761 DeclTypedefAbbrev(0),
2762 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2763 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00002764{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002765}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002766
Sebastian Redla4232eb2010-08-18 23:56:21 +00002767void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002768 const std::string &OutputFile,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002769 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002770 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002771 Stream.Emit((unsigned)'C', 8);
2772 Stream.Emit((unsigned)'P', 8);
2773 Stream.Emit((unsigned)'C', 8);
2774 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002775
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002776 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002777
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002778 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002779 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002780 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002781 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002782}
2783
Sebastian Redla4232eb2010-08-18 23:56:21 +00002784void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002785 const char *isysroot,
2786 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002787 using namespace llvm;
2788
2789 ASTContext &Context = SemaRef.Context;
2790 Preprocessor &PP = SemaRef.PP;
2791
Douglas Gregor2cf26342009-04-09 22:27:44 +00002792 // The translation unit is the first declaration we'll emit.
2793 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002794 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002795 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002796
Douglas Gregor2deaea32009-04-22 18:49:13 +00002797 // Make sure that we emit IdentifierInfos (and any attached
2798 // declarations) for builtins.
2799 {
2800 IdentifierTable &Table = PP.getIdentifierTable();
2801 llvm::SmallVector<const char *, 32> BuiltinNames;
2802 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2803 Context.getLangOptions().NoBuiltin);
2804 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2805 getIdentifierRef(&Table.get(BuiltinNames[I]));
2806 }
2807
Chris Lattner63d65f82009-09-08 18:19:27 +00002808 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002809 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002810 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002811 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002812 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2813 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002814 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002815
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002816 // Build a record containing all of the file scoped decls in this file.
2817 RecordData UnusedFileScopedDecls;
2818 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2819 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002820
Sean Huntebcbe1d2011-05-04 23:29:54 +00002821 RecordData DelegatingCtorDecls;
2822 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i)
2823 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
2824
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002825 RecordData WeakUndeclaredIdentifiers;
2826 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2827 WeakUndeclaredIdentifiers.push_back(
2828 SemaRef.WeakUndeclaredIdentifiers.size());
2829 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2830 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2831 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2832 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2833 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2834 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2835 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2836 }
2837 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002838
Douglas Gregor14c22f22009-04-22 22:18:58 +00002839 // Build a record containing all of the locally-scoped external
2840 // declarations in this header file. Generally, this record will be
2841 // empty.
2842 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002843 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002844 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002845 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002846 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2847 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2848 TD != TDEnd; ++TD)
2849 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2850
Douglas Gregorb81c1702009-04-27 20:06:05 +00002851 // Build a record containing all of the ext_vector declarations.
2852 RecordData ExtVectorDecls;
2853 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2854 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2855
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002856 // Build a record containing all of the VTable uses information.
2857 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002858 if (!SemaRef.VTableUses.empty()) {
2859 VTableUses.push_back(SemaRef.VTableUses.size());
2860 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2861 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2862 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2863 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2864 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002865 }
2866
2867 // Build a record containing all of dynamic classes declarations.
2868 RecordData DynamicClasses;
2869 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2870 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2871
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002872 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002873 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002874 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002875 I = SemaRef.PendingInstantiations.begin(),
2876 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2877 AddDeclRef(I->first, PendingInstantiations);
2878 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002879 }
2880 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2881 "There are local ones at end of translation unit!");
2882
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002883 // Build a record containing some declaration references.
2884 RecordData SemaDeclRefs;
2885 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2886 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2887 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2888 }
2889
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002890 RecordData CUDASpecialDeclRefs;
2891 if (Context.getcudaConfigureCallDecl()) {
2892 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2893 }
2894
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002895 // Build a record containing all of the known namespaces.
2896 RecordData KnownNamespaces;
2897 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2898 I = SemaRef.KnownNamespaces.begin(),
2899 IEnd = SemaRef.KnownNamespaces.end();
2900 I != IEnd; ++I) {
2901 if (!I->second)
2902 AddDeclRef(I->first, KnownNamespaces);
2903 }
2904
Sebastian Redl3397c552010-08-18 23:56:27 +00002905 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002906 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002907 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002908 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002909 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002910 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002911 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002912 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002913 // Write the record of special types.
2914 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002915
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002916 AddTypeRef(Context.getBuiltinVaListType(), Record);
2917 AddTypeRef(Context.getObjCIdType(), Record);
2918 AddTypeRef(Context.getObjCSelType(), Record);
2919 AddTypeRef(Context.getObjCProtoType(), Record);
2920 AddTypeRef(Context.getObjCClassType(), Record);
2921 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2922 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2923 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002924 AddTypeRef(Context.getjmp_bufType(), Record);
2925 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002926 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2927 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002928 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002929 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002930 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2931 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002932 Record.push_back(Context.isInt128Installed());
Richard Smithad762fc2011-04-14 22:09:26 +00002933 AddTypeRef(Context.AutoDeductTy, Record);
2934 AddTypeRef(Context.AutoRRefDeductTy, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002935 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Douglas Gregor366809a2009-04-26 03:49:13 +00002937 // Keep writing types and declarations until all types and
2938 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00002939 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002940 WriteDeclsBlockAbbrevs();
2941 while (!DeclTypesToEmit.empty()) {
2942 DeclOrType DOT = DeclTypesToEmit.front();
2943 DeclTypesToEmit.pop();
2944 if (DOT.isType())
2945 WriteType(DOT.getType());
2946 else
2947 WriteDecl(Context, DOT.getDecl());
2948 }
2949 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002950
Douglas Gregor813a97b2009-10-17 17:25:45 +00002951 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002952 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002953 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002954 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002955 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002956 WriteFPPragmaOptions(SemaRef.getFPOptions());
2957 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002958
Sebastian Redl1476ed42010-07-16 16:36:56 +00002959 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002960 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002961
Anders Carlssonc8505782011-03-06 18:41:18 +00002962 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00002963
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002964 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002965 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002966 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002967
2968 // Write the record containing tentative definitions.
2969 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002970 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002971
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002972 // Write the record containing unused file scoped decls.
2973 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002974 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002975
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002976 // Write the record containing weak undeclared identifiers.
2977 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002978 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002979 WeakUndeclaredIdentifiers);
2980
Douglas Gregor14c22f22009-04-22 22:18:58 +00002981 // Write the record containing locally-scoped external definitions.
2982 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002983 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002984 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002985
2986 // Write the record containing ext_vector type names.
2987 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002988 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002989
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002990 // Write the record containing VTable uses information.
2991 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002992 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002993
2994 // Write the record containing dynamic classes declarations.
2995 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002996 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002997
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002998 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002999 if (!PendingInstantiations.empty())
3000 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003001
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003002 // Write the record containing declaration references of Sema.
3003 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003004 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003005
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003006 // Write the record containing CUDA-specific declaration references.
3007 if (!CUDASpecialDeclRefs.empty())
3008 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003009
3010 // Write the delegating constructors.
3011 if (!DelegatingCtorDecls.empty())
3012 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003013
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003014 // Write the known namespaces.
3015 if (!KnownNamespaces.empty())
3016 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3017
Douglas Gregor3e1af842009-04-17 22:13:46 +00003018 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003019 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003020 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003021 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003022 Record.push_back(NumLexicalDeclContexts);
3023 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003024 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003025 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003026}
3027
Sebastian Redla4232eb2010-08-18 23:56:21 +00003028void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00003029 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003030 using namespace llvm;
3031
3032 ASTContext &Context = SemaRef.Context;
3033 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00003034
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003035 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003036 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003037 WriteMetadata(Context, isysroot, "");
Sebastian Redl1476ed42010-07-16 16:36:56 +00003038 if (StatCalls && !isysroot)
3039 WriteStatCache(*StatCalls);
3040 // FIXME: Source manager block should only write new stuff, which could be
3041 // done by tracking the largest ID in the chain
3042 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003043
3044 // The special types are in the chained PCH.
3045
3046 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003047 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003048 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003049 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00003050 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3051 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003052 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00003053 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003054 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003055 else if ((*I)->isChangedSinceDeserialization())
3056 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003057 }
Sebastian Redl681d7232010-07-27 00:17:23 +00003058 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00003059 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003060 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00003061 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3062 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3063 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003064 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00003065 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003066 data(NewGlobalDecls));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003067 // And a visible updates block for the DeclContexts.
3068 Abv = new llvm::BitCodeAbbrev();
3069 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3070 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3071 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3072 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3073 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3074 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003075
Sebastian Redl083abdf2010-07-27 23:01:28 +00003076 // Build a record containing all of the new tentative definitions in this
3077 // file, in TentativeDefinitions order.
3078 RecordData TentativeDefinitions;
3079 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
3080 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
3081 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
3082 }
3083
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003084 // Build a record containing all of the file scoped decls in this file.
3085 RecordData UnusedFileScopedDecls;
3086 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
3087 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
3088 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003089 }
3090
Sean Huntebcbe1d2011-05-04 23:29:54 +00003091 // Build a record containing all of the delegating constructor decls in this
3092 // file.
3093 RecordData DelegatingCtorDecls;
3094 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i) {
3095 if (SemaRef.DelegatingCtorDecls[i]->getPCHLevel() == 0)
3096 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
3097 }
3098
Sebastian Redl40566802010-08-05 18:21:25 +00003099 // We write the entire table, overwriting the tables from the chain.
3100 RecordData WeakUndeclaredIdentifiers;
3101 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
3102 WeakUndeclaredIdentifiers.push_back(
3103 SemaRef.WeakUndeclaredIdentifiers.size());
3104 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
3105 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3106 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3107 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3108 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3109 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3110 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3111 }
3112 }
3113
Sebastian Redl083abdf2010-07-27 23:01:28 +00003114 // Build a record containing all of the locally-scoped external
3115 // declarations in this header file. Generally, this record will be
3116 // empty.
3117 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003118 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00003119 // nondeterminstic!
3120 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3121 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3122 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3123 TD != TDEnd; ++TD) {
3124 if (TD->second->getPCHLevel() == 0)
3125 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3126 }
3127
3128 // Build a record containing all of the ext_vector declarations.
3129 RecordData ExtVectorDecls;
3130 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3131 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3132 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3133 }
3134
Sebastian Redl40566802010-08-05 18:21:25 +00003135 // Build a record containing all of the VTable uses information.
3136 // We write everything here, because it's too hard to determine whether
3137 // a use is new to this part.
3138 RecordData VTableUses;
3139 if (!SemaRef.VTableUses.empty()) {
3140 VTableUses.push_back(SemaRef.VTableUses.size());
3141 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3142 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3143 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3144 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3145 }
3146 }
3147
3148 // Build a record containing all of dynamic classes declarations.
3149 RecordData DynamicClasses;
3150 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3151 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3152 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3153
3154 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003155 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00003156 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003157 I = SemaRef.PendingInstantiations.begin(),
3158 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00003159 AddDeclRef(I->first, PendingInstantiations);
3160 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003161 }
3162 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3163 "There are local ones at end of translation unit!");
3164
3165 // Build a record containing some declaration references.
3166 // It's not worth the effort to avoid duplication here.
3167 RecordData SemaDeclRefs;
3168 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3169 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3170 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3171 }
3172
Douglas Gregora72d8c42011-06-03 02:27:19 +00003173 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003174 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003175 for (DeclsToRewriteTy::iterator
3176 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3177 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003178 while (!DeclTypesToEmit.empty()) {
3179 DeclOrType DOT = DeclTypesToEmit.front();
3180 DeclTypesToEmit.pop();
3181 if (DOT.isType())
3182 WriteType(DOT.getType());
3183 else
3184 WriteDecl(Context, DOT.getDecl());
3185 }
3186 Stream.ExitBlock();
3187
Sebastian Redl083abdf2010-07-27 23:01:28 +00003188 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003189 WriteSelectors(SemaRef);
3190 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003191 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003192 WriteFPPragmaOptions(SemaRef.getFPOptions());
3193 WriteOpenCLExtensions(SemaRef);
3194
Sebastian Redl1476ed42010-07-16 16:36:56 +00003195 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003196 // FIXME: For chained PCH only write the new mappings (we currently
3197 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003198 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003199
Anders Carlssonc8505782011-03-06 18:41:18 +00003200 WriteCXXBaseSpecifiersOffsets();
3201
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003202 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003203 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003204 RecordData FirstLatestDeclIDs;
3205 for (FirstLatestDeclMap::iterator
3206 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3207 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3208 "Expected first & second to be in different PCHs");
3209 AddDeclRef(I->first, FirstLatestDeclIDs);
3210 AddDeclRef(I->second, FirstLatestDeclIDs);
3211 }
3212 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003213 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003214
Sebastian Redl083abdf2010-07-27 23:01:28 +00003215 // Write the record containing external, unnamed definitions.
3216 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003217 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003218
3219 // Write the record containing tentative definitions.
3220 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003221 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003222
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003223 // Write the record containing unused file scoped decls.
3224 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003225 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003226
Sebastian Redl40566802010-08-05 18:21:25 +00003227 // Write the record containing weak undeclared identifiers.
3228 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003229 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003230 WeakUndeclaredIdentifiers);
3231
Sebastian Redl083abdf2010-07-27 23:01:28 +00003232 // Write the record containing locally-scoped external definitions.
3233 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003234 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003235 LocallyScopedExternalDecls);
3236
3237 // Write the record containing ext_vector type names.
3238 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003239 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003240
Sebastian Redl40566802010-08-05 18:21:25 +00003241 // Write the record containing VTable uses information.
3242 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003243 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003244
3245 // Write the record containing dynamic classes declarations.
3246 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003247 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003248
3249 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003250 if (!PendingInstantiations.empty())
3251 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003252
3253 // Write the record containing declaration references of Sema.
3254 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003255 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003256
3257 // Write the delegating constructors.
3258 if (!DelegatingCtorDecls.empty())
3259 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003260
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003261 // Write the updates to DeclContexts.
3262 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3263 I = UpdatedDeclContexts.begin(),
3264 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003265 I != E; ++I)
3266 WriteDeclContextVisibleUpdate(*I);
3267
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003268 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003269
Sebastian Redl083abdf2010-07-27 23:01:28 +00003270 Record.clear();
3271 Record.push_back(NumStatements);
3272 Record.push_back(NumMacros);
3273 Record.push_back(NumLexicalDeclContexts);
3274 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003275 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003276 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003277 Stream.ExitBlock();
3278}
3279
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003280void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003281 if (DeclUpdates.empty())
3282 return;
3283
3284 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003285 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003286 for (DeclUpdateMap::iterator
3287 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3288 const Decl *D = I->first;
3289 UpdateRecord &URec = I->second;
3290
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003291 if (DeclsToRewrite.count(D))
3292 continue; // The decl will be written completely,no need to store updates.
3293
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003294 uint64_t Offset = Stream.GetCurrentBitNo();
3295 Stream.EmitRecord(DECL_UPDATES, URec);
3296
3297 OffsetsRecord.push_back(GetDeclRef(D));
3298 OffsetsRecord.push_back(Offset);
3299 }
3300 Stream.ExitBlock();
3301 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3302}
3303
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003304void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003305 if (ReplacedDecls.empty())
3306 return;
3307
3308 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003309 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003310 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3311 Record.push_back(I->first);
3312 Record.push_back(I->second);
3313 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003314 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003315}
3316
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003317void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003318 Record.push_back(Loc.getRawEncoding());
3319}
3320
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003321void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003322 AddSourceLocation(Range.getBegin(), Record);
3323 AddSourceLocation(Range.getEnd(), Record);
3324}
3325
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003326void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003327 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003328 const uint64_t *Words = Value.getRawData();
3329 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003330}
3331
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003332void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003333 Record.push_back(Value.isUnsigned());
3334 AddAPInt(Value, Record);
3335}
3336
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003337void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003338 AddAPInt(Value.bitcastToAPInt(), Record);
3339}
3340
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003341void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003342 Record.push_back(getIdentifierRef(II));
3343}
3344
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003345IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003346 if (II == 0)
3347 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003348
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003349 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003350 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003351 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003352 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003353}
3354
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003355MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003356 if (MD == 0)
3357 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003358
3359 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003360 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003361 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003362 return ID;
3363}
3364
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003365void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003366 Record.push_back(getSelectorRef(SelRef));
3367}
3368
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003369SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003370 if (Sel.getAsOpaquePtr() == 0) {
3371 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003372 }
3373
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003374 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003375 if (SID == 0 && Chain) {
3376 // This might trigger a ReadSelector callback, which will set the ID for
3377 // this selector.
3378 Chain->LoadSelector(Sel);
3379 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003380 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003381 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003382 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003383 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003384}
3385
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003386void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003387 AddDeclRef(Temp->getDestructor(), Record);
3388}
3389
Douglas Gregor7c789c12010-10-29 22:39:52 +00003390void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3391 CXXBaseSpecifier const *BasesEnd,
3392 RecordDataImpl &Record) {
3393 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3394 CXXBaseSpecifiersToWrite.push_back(
3395 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3396 Bases, BasesEnd));
3397 Record.push_back(NextCXXBaseSpecifiersID++);
3398}
3399
Sebastian Redla4232eb2010-08-18 23:56:21 +00003400void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003401 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003402 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003403 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003404 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003405 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003406 break;
3407 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003408 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003409 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003410 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003411 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003412 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003413 break;
3414 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003415 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003416 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003417 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003418 break;
John McCall833ca992009-10-29 08:12:44 +00003419 case TemplateArgument::Null:
3420 case TemplateArgument::Integral:
3421 case TemplateArgument::Declaration:
3422 case TemplateArgument::Pack:
3423 break;
3424 }
3425}
3426
Sebastian Redla4232eb2010-08-18 23:56:21 +00003427void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003428 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003429 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003430
3431 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3432 bool InfoHasSameExpr
3433 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3434 Record.push_back(InfoHasSameExpr);
3435 if (InfoHasSameExpr)
3436 return; // Avoid storing the same expr twice.
3437 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003438 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3439 Record);
3440}
3441
Douglas Gregordc355712011-02-25 00:36:19 +00003442void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3443 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003444 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003445 AddTypeRef(QualType(), Record);
3446 return;
3447 }
3448
Douglas Gregordc355712011-02-25 00:36:19 +00003449 AddTypeLoc(TInfo->getTypeLoc(), Record);
3450}
3451
3452void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3453 AddTypeRef(TL.getType(), Record);
3454
John McCalla1ee0c52009-10-16 21:56:05 +00003455 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003456 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003457 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003458}
3459
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003460void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003461 Record.push_back(GetOrCreateTypeID(T));
3462}
3463
3464TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003465 return MakeTypeID(T,
3466 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3467}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003468
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003469TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003470 return MakeTypeID(T,
3471 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003472}
3473
3474TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3475 if (T.isNull())
3476 return TypeIdx();
3477 assert(!T.getLocalFastQualifiers());
3478
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003479 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003480 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003481 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003482 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003483 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003484 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003485 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003486 return Idx;
3487}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003488
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003489TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003490 if (T.isNull())
3491 return TypeIdx();
3492 assert(!T.getLocalFastQualifiers());
3493
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003494 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3495 assert(I != TypeIdxs.end() && "Type not emitted!");
3496 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003497}
3498
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003499void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003500 Record.push_back(GetDeclRef(D));
3501}
3502
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003503DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003504 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003505 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003506 }
Douglas Gregor97475832010-10-05 18:37:06 +00003507 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003508 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003509 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003510 // We haven't seen this declaration before. Give it a new ID and
3511 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003512 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003513 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003514 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3515 // We don't add it to the replacement collection here, because we don't
3516 // have the offset yet.
3517 DeclTypesToEmit.push(const_cast<Decl *>(D));
3518 // Reset the flag, so that we don't add this decl multiple times.
3519 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003520 }
3521
Sebastian Redl681d7232010-07-27 00:17:23 +00003522 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003523}
3524
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003525DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003526 if (D == 0)
3527 return 0;
3528
3529 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3530 return DeclIDs[D];
3531}
3532
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003533void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003534 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003535 Record.push_back(Name.getNameKind());
3536 switch (Name.getNameKind()) {
3537 case DeclarationName::Identifier:
3538 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3539 break;
3540
3541 case DeclarationName::ObjCZeroArgSelector:
3542 case DeclarationName::ObjCOneArgSelector:
3543 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003544 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003545 break;
3546
3547 case DeclarationName::CXXConstructorName:
3548 case DeclarationName::CXXDestructorName:
3549 case DeclarationName::CXXConversionFunctionName:
3550 AddTypeRef(Name.getCXXNameType(), Record);
3551 break;
3552
3553 case DeclarationName::CXXOperatorName:
3554 Record.push_back(Name.getCXXOverloadedOperator());
3555 break;
3556
Sean Hunt3e518bd2009-11-29 07:34:05 +00003557 case DeclarationName::CXXLiteralOperatorName:
3558 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3559 break;
3560
Douglas Gregor2cf26342009-04-09 22:27:44 +00003561 case DeclarationName::CXXUsingDirective:
3562 // No extra data to emit
3563 break;
3564 }
3565}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003566
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003567void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003568 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003569 switch (Name.getNameKind()) {
3570 case DeclarationName::CXXConstructorName:
3571 case DeclarationName::CXXDestructorName:
3572 case DeclarationName::CXXConversionFunctionName:
3573 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3574 break;
3575
3576 case DeclarationName::CXXOperatorName:
3577 AddSourceLocation(
3578 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3579 Record);
3580 AddSourceLocation(
3581 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3582 Record);
3583 break;
3584
3585 case DeclarationName::CXXLiteralOperatorName:
3586 AddSourceLocation(
3587 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3588 Record);
3589 break;
3590
3591 case DeclarationName::Identifier:
3592 case DeclarationName::ObjCZeroArgSelector:
3593 case DeclarationName::ObjCOneArgSelector:
3594 case DeclarationName::ObjCMultiArgSelector:
3595 case DeclarationName::CXXUsingDirective:
3596 break;
3597 }
3598}
3599
3600void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003601 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003602 AddDeclarationName(NameInfo.getName(), Record);
3603 AddSourceLocation(NameInfo.getLoc(), Record);
3604 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3605}
3606
3607void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003608 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003609 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003610 Record.push_back(Info.NumTemplParamLists);
3611 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3612 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3613}
3614
Sebastian Redla4232eb2010-08-18 23:56:21 +00003615void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003616 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003617 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003618 // typically accommodate the vast majority.
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003619 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3620
3621 // Push each of the NNS's onto a stack for serialization in reverse order.
3622 while (NNS) {
3623 NestedNames.push_back(NNS);
3624 NNS = NNS->getPrefix();
3625 }
3626
3627 Record.push_back(NestedNames.size());
3628 while(!NestedNames.empty()) {
3629 NNS = NestedNames.pop_back_val();
3630 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3631 Record.push_back(Kind);
3632 switch (Kind) {
3633 case NestedNameSpecifier::Identifier:
3634 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3635 break;
3636
3637 case NestedNameSpecifier::Namespace:
3638 AddDeclRef(NNS->getAsNamespace(), Record);
3639 break;
3640
Douglas Gregor14aba762011-02-24 02:36:08 +00003641 case NestedNameSpecifier::NamespaceAlias:
3642 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3643 break;
3644
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003645 case NestedNameSpecifier::TypeSpec:
3646 case NestedNameSpecifier::TypeSpecWithTemplate:
3647 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3648 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3649 break;
3650
3651 case NestedNameSpecifier::Global:
3652 // Don't need to write an associated value.
3653 break;
3654 }
3655 }
3656}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003657
Douglas Gregordc355712011-02-25 00:36:19 +00003658void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3659 RecordDataImpl &Record) {
3660 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003661 // typically accommodate the vast majority.
Douglas Gregordc355712011-02-25 00:36:19 +00003662 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3663
3664 // Push each of the nested-name-specifiers's onto a stack for
3665 // serialization in reverse order.
3666 while (NNS) {
3667 NestedNames.push_back(NNS);
3668 NNS = NNS.getPrefix();
3669 }
3670
3671 Record.push_back(NestedNames.size());
3672 while(!NestedNames.empty()) {
3673 NNS = NestedNames.pop_back_val();
3674 NestedNameSpecifier::SpecifierKind Kind
3675 = NNS.getNestedNameSpecifier()->getKind();
3676 Record.push_back(Kind);
3677 switch (Kind) {
3678 case NestedNameSpecifier::Identifier:
3679 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3680 AddSourceRange(NNS.getLocalSourceRange(), Record);
3681 break;
3682
3683 case NestedNameSpecifier::Namespace:
3684 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3685 AddSourceRange(NNS.getLocalSourceRange(), Record);
3686 break;
3687
3688 case NestedNameSpecifier::NamespaceAlias:
3689 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3690 AddSourceRange(NNS.getLocalSourceRange(), Record);
3691 break;
3692
3693 case NestedNameSpecifier::TypeSpec:
3694 case NestedNameSpecifier::TypeSpecWithTemplate:
3695 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3696 AddTypeLoc(NNS.getTypeLoc(), Record);
3697 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3698 break;
3699
3700 case NestedNameSpecifier::Global:
3701 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3702 break;
3703 }
3704 }
3705}
3706
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003707void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003708 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003709 Record.push_back(Kind);
3710 switch (Kind) {
3711 case TemplateName::Template:
3712 AddDeclRef(Name.getAsTemplateDecl(), Record);
3713 break;
3714
3715 case TemplateName::OverloadedTemplate: {
3716 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3717 Record.push_back(OvT->size());
3718 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3719 I != E; ++I)
3720 AddDeclRef(*I, Record);
3721 break;
3722 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003723
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003724 case TemplateName::QualifiedTemplate: {
3725 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3726 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3727 Record.push_back(QualT->hasTemplateKeyword());
3728 AddDeclRef(QualT->getTemplateDecl(), Record);
3729 break;
3730 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003731
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003732 case TemplateName::DependentTemplate: {
3733 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3734 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3735 Record.push_back(DepT->isIdentifier());
3736 if (DepT->isIdentifier())
3737 AddIdentifierRef(DepT->getIdentifier(), Record);
3738 else
3739 Record.push_back(DepT->getOperator());
3740 break;
3741 }
John McCall14606042011-06-30 08:33:18 +00003742
3743 case TemplateName::SubstTemplateTemplateParm: {
3744 SubstTemplateTemplateParmStorage *subst
3745 = Name.getAsSubstTemplateTemplateParm();
3746 AddDeclRef(subst->getParameter(), Record);
3747 AddTemplateName(subst->getReplacement(), Record);
3748 break;
3749 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003750
3751 case TemplateName::SubstTemplateTemplateParmPack: {
3752 SubstTemplateTemplateParmPackStorage *SubstPack
3753 = Name.getAsSubstTemplateTemplateParmPack();
3754 AddDeclRef(SubstPack->getParameterPack(), Record);
3755 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3756 break;
3757 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003758 }
3759}
3760
Michael J. Spencer20249a12010-10-21 03:16:25 +00003761void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003762 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003763 Record.push_back(Arg.getKind());
3764 switch (Arg.getKind()) {
3765 case TemplateArgument::Null:
3766 break;
3767 case TemplateArgument::Type:
3768 AddTypeRef(Arg.getAsType(), Record);
3769 break;
3770 case TemplateArgument::Declaration:
3771 AddDeclRef(Arg.getAsDecl(), Record);
3772 break;
3773 case TemplateArgument::Integral:
3774 AddAPSInt(*Arg.getAsIntegral(), Record);
3775 AddTypeRef(Arg.getIntegralType(), Record);
3776 break;
3777 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003778 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3779 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003780 case TemplateArgument::TemplateExpansion:
3781 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003782 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3783 Record.push_back(*NumExpansions + 1);
3784 else
3785 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003786 break;
3787 case TemplateArgument::Expression:
3788 AddStmt(Arg.getAsExpr());
3789 break;
3790 case TemplateArgument::Pack:
3791 Record.push_back(Arg.pack_size());
3792 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3793 I != E; ++I)
3794 AddTemplateArgument(*I, Record);
3795 break;
3796 }
3797}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003798
3799void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003800ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003801 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003802 assert(TemplateParams && "No TemplateParams!");
3803 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3804 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3805 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3806 Record.push_back(TemplateParams->size());
3807 for (TemplateParameterList::const_iterator
3808 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3809 P != PEnd; ++P)
3810 AddDeclRef(*P, Record);
3811}
3812
3813/// \brief Emit a template argument list.
3814void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003815ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003816 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003817 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003818 Record.push_back(TemplateArgs->size());
3819 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003820 AddTemplateArgument(TemplateArgs->get(i), Record);
3821}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003822
3823
3824void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003825ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003826 Record.push_back(Set.size());
3827 for (UnresolvedSetImpl::const_iterator
3828 I = Set.begin(), E = Set.end(); I != E; ++I) {
3829 AddDeclRef(I.getDecl(), Record);
3830 Record.push_back(I.getAccess());
3831 }
3832}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003833
Sebastian Redla4232eb2010-08-18 23:56:21 +00003834void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003835 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003836 Record.push_back(Base.isVirtual());
3837 Record.push_back(Base.isBaseOfClass());
3838 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003839 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003840 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003841 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003842 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3843 : SourceLocation(),
3844 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003845}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003846
Douglas Gregor7c789c12010-10-29 22:39:52 +00003847void ASTWriter::FlushCXXBaseSpecifiers() {
3848 RecordData Record;
3849 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3850 Record.clear();
3851
3852 // Record the offset of this base-specifier set.
3853 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3854 if (Index == CXXBaseSpecifiersOffsets.size())
3855 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3856 else {
3857 if (Index > CXXBaseSpecifiersOffsets.size())
3858 CXXBaseSpecifiersOffsets.resize(Index + 1);
3859 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3860 }
3861
3862 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3863 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3864 Record.push_back(BEnd - B);
3865 for (; B != BEnd; ++B)
3866 AddCXXBaseSpecifier(*B, Record);
3867 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003868
3869 // Flush any expressions that were written as part of the base specifiers.
3870 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003871 }
3872
3873 CXXBaseSpecifiersToWrite.clear();
3874}
3875
Sean Huntcbb67482011-01-08 20:30:50 +00003876void ASTWriter::AddCXXCtorInitializers(
3877 const CXXCtorInitializer * const *CtorInitializers,
3878 unsigned NumCtorInitializers,
3879 RecordDataImpl &Record) {
3880 Record.push_back(NumCtorInitializers);
3881 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3882 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003883
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003884 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00003885 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003886 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3887 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00003888 } else if (Init->isDelegatingInitializer()) {
3889 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3890 AddDeclRef(Init->getTargetConstructor(), Record);
3891 } else if (Init->isMemberInitializer()){
3892 Record.push_back(CTOR_INITIALIZER_MEMBER);
3893 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003894 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00003895 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3896 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003897 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003898
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003899 AddSourceLocation(Init->getMemberLocation(), Record);
3900 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003901 AddSourceLocation(Init->getLParenLoc(), Record);
3902 AddSourceLocation(Init->getRParenLoc(), Record);
3903 Record.push_back(Init->isWritten());
3904 if (Init->isWritten()) {
3905 Record.push_back(Init->getSourceOrder());
3906 } else {
3907 Record.push_back(Init->getNumArrayIndices());
3908 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3909 AddDeclRef(Init->getArrayIndex(i), Record);
3910 }
3911 }
3912}
3913
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003914void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3915 assert(D->DefinitionData);
3916 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3917 Record.push_back(Data.UserDeclaredConstructor);
3918 Record.push_back(Data.UserDeclaredCopyConstructor);
3919 Record.push_back(Data.UserDeclaredCopyAssignment);
3920 Record.push_back(Data.UserDeclaredDestructor);
3921 Record.push_back(Data.Aggregate);
3922 Record.push_back(Data.PlainOldData);
3923 Record.push_back(Data.Empty);
3924 Record.push_back(Data.Polymorphic);
3925 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00003926 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00003927 Record.push_back(Data.HasNoNonEmptyBases);
3928 Record.push_back(Data.HasPrivateFields);
3929 Record.push_back(Data.HasProtectedFields);
3930 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00003931 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00003932 Record.push_back(Data.HasTrivialDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003933 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003934 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003935 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003936 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003937 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003938 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003939 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003940 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00003941 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003942 Record.push_back(Data.DeclaredDefaultConstructor);
3943 Record.push_back(Data.DeclaredCopyConstructor);
3944 Record.push_back(Data.DeclaredCopyAssignment);
3945 Record.push_back(Data.DeclaredDestructor);
3946
3947 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003948 if (Data.NumBases > 0)
3949 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3950 Record);
3951
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003952 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3953 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003954 if (Data.NumVBases > 0)
3955 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3956 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003957
3958 AddUnresolvedSet(Data.Conversions, Record);
3959 AddUnresolvedSet(Data.VisibleConversions, Record);
3960 // Data.Definition is the owning decl, no need to write it.
3961 AddDeclRef(Data.FirstFriend, Record);
3962}
3963
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003964void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003965 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003966 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003967 assert(FirstDeclID == NextDeclID &&
3968 FirstTypeID == NextTypeID &&
3969 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003970 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003971 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003972 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003973 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003974
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003975 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003976
3977 FirstDeclID += Chain->getTotalNumDecls();
3978 FirstTypeID += Chain->getTotalNumTypes();
3979 FirstIdentID += Chain->getTotalNumIdentifiers();
3980 FirstSelectorID += Chain->getTotalNumSelectors();
3981 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003982 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003983 NextDeclID = FirstDeclID;
3984 NextTypeID = FirstTypeID;
3985 NextIdentID = FirstIdentID;
3986 NextSelectorID = FirstSelectorID;
3987 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003988 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003989}
3990
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003991void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003992 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003993 if (II->hasMacroDefinition())
3994 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003995}
3996
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003997void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003998 // Always take the highest-numbered type index. This copes with an interesting
3999 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004000 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004001 // keep the higher-numbered entry so that we can properly write it out to
4002 // the AST file.
4003 TypeIdx &StoredIdx = TypeIdxs[T];
4004 if (Idx.getIndex() >= StoredIdx.getIndex())
4005 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004006}
4007
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004008void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00004009 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004010}
Sebastian Redl5d050072010-08-04 17:20:04 +00004011
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004012void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004013 SelectorIDs[S] = ID;
4014}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004015
Michael J. Spencer20249a12010-10-21 03:16:25 +00004016void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004017 MacroDefinition *MD) {
4018 MacroDefinitions[MD] = ID;
4019}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004020
4021void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
4022 assert(D->isDefinition());
4023 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4024 // We are interested when a PCH decl is modified.
4025 if (RD->getPCHLevel() > 0) {
4026 // A forward reference was mutated into a definition. Rewrite it.
4027 // FIXME: This happens during template instantiation, should we
4028 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004029 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004030 }
4031
4032 for (CXXRecordDecl::redecl_iterator
4033 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
4034 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
4035 if (Redecl == RD)
4036 continue;
4037
4038 // We are interested when a PCH decl is modified.
4039 if (Redecl->getPCHLevel() > 0) {
4040 UpdateRecord &Record = DeclUpdates[Redecl];
4041 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
4042 assert(Redecl->DefinitionData);
4043 assert(Redecl->DefinitionData->Definition == D);
4044 AddDeclRef(D, Record); // the DefinitionDecl
4045 }
4046 }
4047 }
4048}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004049void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
4050 // TU and namespaces are handled elsewhere.
4051 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4052 return;
4053
4054 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
4055 return; // Not a source decl added to a DeclContext from PCH.
4056
4057 AddUpdatedDeclContext(DC);
4058}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004059
4060void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
4061 assert(D->isImplicit());
4062 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
4063 return; // Not a source member added to a class from PCH.
4064 if (!isa<CXXMethodDecl>(D))
4065 return; // We are interested in lazily declared implicit methods.
4066
4067 // A decl coming from PCH was modified.
4068 assert(RD->isDefinition());
4069 UpdateRecord &Record = DeclUpdates[RD];
4070 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
4071 AddDeclRef(D, Record);
4072}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004073
4074void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4075 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004076 // The specializations set is kept in the canonical template.
4077 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004078 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4079 return; // Not a source specialization added to a template from PCH.
4080
4081 UpdateRecord &Record = DeclUpdates[TD];
4082 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4083 AddDeclRef(D, Record);
4084}
Douglas Gregor89d99802010-11-30 06:16:57 +00004085
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004086void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4087 const FunctionDecl *D) {
4088 // The specializations set is kept in the canonical template.
4089 TD = TD->getCanonicalDecl();
4090 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4091 return; // Not a source specialization added to a template from PCH.
4092
4093 UpdateRecord &Record = DeclUpdates[TD];
4094 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4095 AddDeclRef(D, Record);
4096}
4097
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004098void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4099 if (D->getPCHLevel() == 0)
4100 return; // Declaration not imported from PCH.
4101
4102 // Implicit decl from a PCH was defined.
4103 // FIXME: Should implicit definition be a separate FunctionDecl?
4104 RewriteDecl(D);
4105}
4106
Sebastian Redlf79a7192011-04-29 08:19:30 +00004107void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4108 if (D->getPCHLevel() == 0)
4109 return;
4110
4111 // Since the actual instantiation is delayed, this really means that we need
4112 // to update the instantiation location.
4113 UpdateRecord &Record = DeclUpdates[D];
4114 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4115 AddSourceLocation(
4116 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4117}
4118
Douglas Gregor89d99802010-11-30 06:16:57 +00004119ASTSerializationListener::~ASTSerializationListener() { }