blob: f5f96ecd24e8081fb098af855e888f5134e5c617 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000026#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000027#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000028#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000030#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000031#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000035#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000036#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000037#include "llvm/ADT/APFloat.h"
38#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000041#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000042#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000043#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000045using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046
Sebastian Redl3df5a082010-07-30 17:03:48 +000047template <typename T, typename Allocator>
48T *data(std::vector<T, Allocator> &v) {
49 return v.empty() ? 0 : &v.front();
50}
51template <typename T, typename Allocator>
52const T *data(const std::vector<T, Allocator> &v) {
53 return v.empty() ? 0 : &v.front();
54}
55
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056//===----------------------------------------------------------------------===//
57// Type serialization
58//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000059
Douglas Gregoref84c4b2009-04-09 22:27:44 +000060namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000061 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000062 ASTWriter &Writer;
63 ASTWriter::RecordData &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000064
65 public:
66 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000067 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000069 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000070 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071
72 void VisitArrayType(const ArrayType *T);
73 void VisitFunctionType(const FunctionType *T);
74 void VisitTagType(const TagType *T);
75
76#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
77#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000078#include "clang/AST/TypeNodes.def"
79 };
80}
81
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000082void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083 assert(false && "Built-in types are never serialized");
84}
85
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000086void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000087 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000088 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089}
90
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000091void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000093 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000097 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000098 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
113 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000114 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115}
116
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000118 Writer.AddTypeRef(T->getElementType(), Record);
119 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000120 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121}
122
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000124 VisitArrayType(T);
125 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000126 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000127}
128
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000131 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000136 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
137 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000138 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000139 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140}
141
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143 Writer.AddTypeRef(T->getElementType(), Record);
144 Record.push_back(T->getNumElements());
Chris Lattner37141f42010-06-23 06:00:24 +0000145 Record.push_back(T->getAltiVecSpecific());
Sebastian Redl539c5062010-08-18 23:57:32 +0000146 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147}
148
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000150 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000156 FunctionType::ExtInfo C = T->getExtInfo();
157 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000158 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000159 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000160 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161}
162
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000165 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166}
167
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169 VisitFunctionType(T);
170 Record.push_back(T->getNumArgs());
171 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
172 Writer.AddTypeRef(T->getArgType(I), Record);
173 Record.push_back(T->isVariadic());
174 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000175 Record.push_back(T->hasExceptionSpec());
176 Record.push_back(T->hasAnyExceptionSpec());
177 Record.push_back(T->getNumExceptions());
178 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
179 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181}
182
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000184 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000185 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000186}
John McCallb96ec562009-12-04 22:46:56 +0000187
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000188void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000189 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000190 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
191 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000192 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193}
194
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000195void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000196 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000197 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198}
199
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000200void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000201 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000202 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000203}
204
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000205void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000206 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000207 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000208}
209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000211 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000212 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000213 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214 "Cannot serialize in the middle of a type definition");
215}
216
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000217void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Mike Stump11289f42009-09-09 15:08:12 +0000227void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000228ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000229 const SubstTemplateTypeParmType *T) {
230 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
231 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000232 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000233}
234
235void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000236ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000238 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000239 Writer.AddTemplateName(T->getTemplateName(), Record);
240 Record.push_back(T->getNumArgs());
241 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
242 ArgI != ArgE; ++ArgI)
243 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000244 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
245 : T->getCanonicalTypeInternal(),
246 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000248}
249
250void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000251ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000252 VisitArrayType(T);
253 Writer.AddStmt(T->getSizeExpr());
254 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000255 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000256}
257
258void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000259ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000260 const DependentSizedExtVectorType *T) {
261 // FIXME: Serialize this type (C++ only)
262 assert(false && "Cannot serialize dependent sized extended vector types");
263}
264
265void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000266ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000267 Record.push_back(T->getDepth());
268 Record.push_back(T->getIndex());
269 Record.push_back(T->isParameterPack());
270 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000271 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000272}
273
274void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000275ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000276 Record.push_back(T->getKeyword());
277 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
278 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000279 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
280 : T->getCanonicalTypeInternal(),
281 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000282 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000283}
284
285void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000287 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000288 Record.push_back(T->getKeyword());
289 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
290 Writer.AddIdentifierRef(T->getIdentifier(), Record);
291 Record.push_back(T->getNumArgs());
292 for (DependentTemplateSpecializationType::iterator
293 I = T->begin(), E = T->end(); I != E; ++I)
294 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000295 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000296}
297
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000298void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000299 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000300 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
301 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000302 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000303}
304
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000305void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000306 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000307 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000308 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000309}
310
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000311void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000312 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000313 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000314}
315
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000316void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000317 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000318 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000319 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000320 E = T->qual_end(); I != E; ++I)
321 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000322 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000323}
324
Steve Narofffb4330f2009-06-17 22:40:22 +0000325void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000326ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000327 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000328 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000329}
330
John McCall8f115c62009-10-16 21:56:05 +0000331namespace {
332
333class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000334 ASTWriter &Writer;
335 ASTWriter::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +0000336
337public:
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000338 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
John McCall8f115c62009-10-16 21:56:05 +0000339 : Writer(Writer), Record(Record) { }
340
John McCall17001972009-10-18 01:05:36 +0000341#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000342#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000343 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000344#include "clang/AST/TypeLocNodes.def"
345
John McCall17001972009-10-18 01:05:36 +0000346 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
347 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000348};
349
350}
351
John McCall17001972009-10-18 01:05:36 +0000352void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
353 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000354}
John McCall17001972009-10-18 01:05:36 +0000355void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000356 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
357 if (TL.needsExtraLocalData()) {
358 Record.push_back(TL.getWrittenTypeSpec());
359 Record.push_back(TL.getWrittenSignSpec());
360 Record.push_back(TL.getWrittenWidthSpec());
361 Record.push_back(TL.hasModeAttr());
362 }
John McCall8f115c62009-10-16 21:56:05 +0000363}
John McCall17001972009-10-18 01:05:36 +0000364void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
365 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000366}
John McCall17001972009-10-18 01:05:36 +0000367void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000369}
John McCall17001972009-10-18 01:05:36 +0000370void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000372}
John McCall17001972009-10-18 01:05:36 +0000373void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000375}
John McCall17001972009-10-18 01:05:36 +0000376void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000378}
John McCall17001972009-10-18 01:05:36 +0000379void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000381}
John McCall17001972009-10-18 01:05:36 +0000382void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
384 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
385 Record.push_back(TL.getSizeExpr() ? 1 : 0);
386 if (TL.getSizeExpr())
387 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000388}
John McCall17001972009-10-18 01:05:36 +0000389void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
390 VisitArrayTypeLoc(TL);
391}
392void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
393 VisitArrayTypeLoc(TL);
394}
395void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
396 VisitArrayTypeLoc(TL);
397}
398void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
399 DependentSizedArrayTypeLoc TL) {
400 VisitArrayTypeLoc(TL);
401}
402void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
403 DependentSizedExtVectorTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
406void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
409void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
414 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
415 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
416 Writer.AddDeclRef(TL.getArg(i), Record);
417}
418void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
419 VisitFunctionTypeLoc(TL);
420}
421void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
422 VisitFunctionTypeLoc(TL);
423}
John McCallb96ec562009-12-04 22:46:56 +0000424void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
425 Writer.AddSourceLocation(TL.getNameLoc(), Record);
426}
John McCall17001972009-10-18 01:05:36 +0000427void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
428 Writer.AddSourceLocation(TL.getNameLoc(), Record);
429}
430void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000431 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
432 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
433 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000434}
435void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000436 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
437 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
438 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
439 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000440}
441void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
443}
444void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
446}
447void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getNameLoc(), Record);
449}
John McCall17001972009-10-18 01:05:36 +0000450void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
John McCallcebee162009-10-18 09:09:24 +0000453void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
454 SubstTemplateTypeParmTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
456}
John McCall17001972009-10-18 01:05:36 +0000457void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
458 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000459 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
460 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
461 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
462 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000463 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
464 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000465}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000466void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000467 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
468 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000469}
John McCalle78aac42010-03-10 03:28:59 +0000470void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000473void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000474 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
475 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
477}
John McCallc392f372010-06-11 00:33:02 +0000478void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
479 DependentTemplateSpecializationTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
481 Writer.AddSourceRange(TL.getQualifierRange(), Record);
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
484 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
485 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000486 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
487 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000488}
John McCall17001972009-10-18 01:05:36 +0000489void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000491}
492void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
493 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000494 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
495 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
496 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
497 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000498}
John McCallfc93cf92009-10-22 22:37:11 +0000499void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000501}
John McCall8f115c62009-10-16 21:56:05 +0000502
Chris Lattner19cea4e2009-04-22 05:57:30 +0000503//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000504// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000505//===----------------------------------------------------------------------===//
506
Chris Lattner28fa4e62009-04-26 22:26:21 +0000507static void EmitBlockID(unsigned ID, const char *Name,
508 llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000509 ASTWriter::RecordData &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000510 Record.clear();
511 Record.push_back(ID);
512 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
513
514 // Emit the block name if present.
515 if (Name == 0 || Name[0] == 0) return;
516 Record.clear();
517 while (*Name)
518 Record.push_back(*Name++);
519 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
520}
521
522static void EmitRecordID(unsigned ID, const char *Name,
523 llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000524 ASTWriter::RecordData &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000525 Record.clear();
526 Record.push_back(ID);
527 while (*Name)
528 Record.push_back(*Name++);
529 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000530}
531
532static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000533 ASTWriter::RecordData &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000534#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000535 RECORD(STMT_STOP);
536 RECORD(STMT_NULL_PTR);
537 RECORD(STMT_NULL);
538 RECORD(STMT_COMPOUND);
539 RECORD(STMT_CASE);
540 RECORD(STMT_DEFAULT);
541 RECORD(STMT_LABEL);
542 RECORD(STMT_IF);
543 RECORD(STMT_SWITCH);
544 RECORD(STMT_WHILE);
545 RECORD(STMT_DO);
546 RECORD(STMT_FOR);
547 RECORD(STMT_GOTO);
548 RECORD(STMT_INDIRECT_GOTO);
549 RECORD(STMT_CONTINUE);
550 RECORD(STMT_BREAK);
551 RECORD(STMT_RETURN);
552 RECORD(STMT_DECL);
553 RECORD(STMT_ASM);
554 RECORD(EXPR_PREDEFINED);
555 RECORD(EXPR_DECL_REF);
556 RECORD(EXPR_INTEGER_LITERAL);
557 RECORD(EXPR_FLOATING_LITERAL);
558 RECORD(EXPR_IMAGINARY_LITERAL);
559 RECORD(EXPR_STRING_LITERAL);
560 RECORD(EXPR_CHARACTER_LITERAL);
561 RECORD(EXPR_PAREN);
562 RECORD(EXPR_UNARY_OPERATOR);
563 RECORD(EXPR_SIZEOF_ALIGN_OF);
564 RECORD(EXPR_ARRAY_SUBSCRIPT);
565 RECORD(EXPR_CALL);
566 RECORD(EXPR_MEMBER);
567 RECORD(EXPR_BINARY_OPERATOR);
568 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
569 RECORD(EXPR_CONDITIONAL_OPERATOR);
570 RECORD(EXPR_IMPLICIT_CAST);
571 RECORD(EXPR_CSTYLE_CAST);
572 RECORD(EXPR_COMPOUND_LITERAL);
573 RECORD(EXPR_EXT_VECTOR_ELEMENT);
574 RECORD(EXPR_INIT_LIST);
575 RECORD(EXPR_DESIGNATED_INIT);
576 RECORD(EXPR_IMPLICIT_VALUE_INIT);
577 RECORD(EXPR_VA_ARG);
578 RECORD(EXPR_ADDR_LABEL);
579 RECORD(EXPR_STMT);
580 RECORD(EXPR_TYPES_COMPATIBLE);
581 RECORD(EXPR_CHOOSE);
582 RECORD(EXPR_GNU_NULL);
583 RECORD(EXPR_SHUFFLE_VECTOR);
584 RECORD(EXPR_BLOCK);
585 RECORD(EXPR_BLOCK_DECL_REF);
586 RECORD(EXPR_OBJC_STRING_LITERAL);
587 RECORD(EXPR_OBJC_ENCODE);
588 RECORD(EXPR_OBJC_SELECTOR_EXPR);
589 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
590 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
591 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
592 RECORD(EXPR_OBJC_KVC_REF_EXPR);
593 RECORD(EXPR_OBJC_MESSAGE_EXPR);
594 RECORD(EXPR_OBJC_SUPER_EXPR);
595 RECORD(STMT_OBJC_FOR_COLLECTION);
596 RECORD(STMT_OBJC_CATCH);
597 RECORD(STMT_OBJC_FINALLY);
598 RECORD(STMT_OBJC_AT_TRY);
599 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
600 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000601 RECORD(EXPR_CXX_OPERATOR_CALL);
602 RECORD(EXPR_CXX_CONSTRUCT);
603 RECORD(EXPR_CXX_STATIC_CAST);
604 RECORD(EXPR_CXX_DYNAMIC_CAST);
605 RECORD(EXPR_CXX_REINTERPRET_CAST);
606 RECORD(EXPR_CXX_CONST_CAST);
607 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
608 RECORD(EXPR_CXX_BOOL_LITERAL);
609 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000610#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000611}
Mike Stump11289f42009-09-09 15:08:12 +0000612
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000613void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000614 RecordData Record;
615 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000616
Sebastian Redl539c5062010-08-18 23:57:32 +0000617#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
618#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000619
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000620 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000621 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000622 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000623 RECORD(TYPE_OFFSET);
624 RECORD(DECL_OFFSET);
625 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000626 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000627 RECORD(IDENTIFIER_OFFSET);
628 RECORD(IDENTIFIER_TABLE);
629 RECORD(EXTERNAL_DEFINITIONS);
630 RECORD(SPECIAL_TYPES);
631 RECORD(STATISTICS);
632 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000633 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000634 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
635 RECORD(SELECTOR_OFFSETS);
636 RECORD(METHOD_POOL);
637 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000638 RECORD(SOURCE_LOCATION_OFFSETS);
639 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000640 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000641 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000642 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000643 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000644 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000645 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregoraae92242010-03-19 21:51:54 +0000646
Chris Lattner28fa4e62009-04-26 22:26:21 +0000647 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000648 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000649 RECORD(SM_SLOC_FILE_ENTRY);
650 RECORD(SM_SLOC_BUFFER_ENTRY);
651 RECORD(SM_SLOC_BUFFER_BLOB);
652 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
653 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000654
Chris Lattner28fa4e62009-04-26 22:26:21 +0000655 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000656 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000657 RECORD(PP_MACRO_OBJECT_LIKE);
658 RECORD(PP_MACRO_FUNCTION_LIKE);
659 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000660 RECORD(PP_MACRO_INSTANTIATION);
661 RECORD(PP_MACRO_DEFINITION);
662
Douglas Gregor12bfa382009-10-17 00:13:19 +0000663 // Decls and Types block.
664 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000665 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000666 RECORD(TYPE_COMPLEX);
667 RECORD(TYPE_POINTER);
668 RECORD(TYPE_BLOCK_POINTER);
669 RECORD(TYPE_LVALUE_REFERENCE);
670 RECORD(TYPE_RVALUE_REFERENCE);
671 RECORD(TYPE_MEMBER_POINTER);
672 RECORD(TYPE_CONSTANT_ARRAY);
673 RECORD(TYPE_INCOMPLETE_ARRAY);
674 RECORD(TYPE_VARIABLE_ARRAY);
675 RECORD(TYPE_VECTOR);
676 RECORD(TYPE_EXT_VECTOR);
677 RECORD(TYPE_FUNCTION_PROTO);
678 RECORD(TYPE_FUNCTION_NO_PROTO);
679 RECORD(TYPE_TYPEDEF);
680 RECORD(TYPE_TYPEOF_EXPR);
681 RECORD(TYPE_TYPEOF);
682 RECORD(TYPE_RECORD);
683 RECORD(TYPE_ENUM);
684 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000685 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000686 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000687 RECORD(DECL_ATTR);
688 RECORD(DECL_TRANSLATION_UNIT);
689 RECORD(DECL_TYPEDEF);
690 RECORD(DECL_ENUM);
691 RECORD(DECL_RECORD);
692 RECORD(DECL_ENUM_CONSTANT);
693 RECORD(DECL_FUNCTION);
694 RECORD(DECL_OBJC_METHOD);
695 RECORD(DECL_OBJC_INTERFACE);
696 RECORD(DECL_OBJC_PROTOCOL);
697 RECORD(DECL_OBJC_IVAR);
698 RECORD(DECL_OBJC_AT_DEFS_FIELD);
699 RECORD(DECL_OBJC_CLASS);
700 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
701 RECORD(DECL_OBJC_CATEGORY);
702 RECORD(DECL_OBJC_CATEGORY_IMPL);
703 RECORD(DECL_OBJC_IMPLEMENTATION);
704 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
705 RECORD(DECL_OBJC_PROPERTY);
706 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000707 RECORD(DECL_FIELD);
708 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000709 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000710 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000711 RECORD(DECL_FILE_SCOPE_ASM);
712 RECORD(DECL_BLOCK);
713 RECORD(DECL_CONTEXT_LEXICAL);
714 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000715 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000716 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000717#undef RECORD
718#undef BLOCK
719 Stream.ExitBlock();
720}
721
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000722/// \brief Adjusts the given filename to only write out the portion of the
723/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000724///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000725/// \param Filename the file name to adjust.
726///
727/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
728/// the returned filename will be adjusted by this system root.
729///
730/// \returns either the original filename (if it needs no adjustment) or the
731/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000732static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000733adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
734 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000735
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000736 if (!isysroot)
737 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000738
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000739 // Verify that the filename and the system root have the same prefix.
740 unsigned Pos = 0;
741 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
742 if (Filename[Pos] != isysroot[Pos])
743 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000744
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000745 // We hit the end of the filename before we hit the end of the system root.
746 if (!Filename[Pos])
747 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000748
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000749 // If the file name has a '/' at the current position, skip over the '/'.
750 // We distinguish sysroot-based includes from absolute includes by the
751 // absence of '/' at the beginning of sysroot-based includes.
752 if (Filename[Pos] == '/')
753 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000754
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000755 return Filename + Pos;
756}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000757
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000758/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000759void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000760 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000761
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000762 // Metadata
763 const TargetInfo &Target = Context.Target;
764 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000765 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000766 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
771 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000772 // Target triple or chained PCH name
773 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000774 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000775
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000776 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000777 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
778 Record.push_back(VERSION_MAJOR);
779 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000780 Record.push_back(CLANG_VERSION_MAJOR);
781 Record.push_back(CLANG_VERSION_MINOR);
782 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000783 // FIXME: This writes the absolute path for chained headers.
784 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
785 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000786
Douglas Gregor45fe0362009-05-12 01:31:05 +0000787 // Original file name
788 SourceManager &SM = Context.getSourceManager();
789 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
790 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000791 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000792 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
793 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
794
795 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000796
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000797 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000798
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000799 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000800 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000801 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000802 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000803 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000804 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000805 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000806
Ted Kremenek18e066f2010-01-22 22:12:47 +0000807 // Repository branch/version information.
808 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000809 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000810 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
811 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000812 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000813 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000814 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
815 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000816}
817
818/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000819void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000820 RecordData Record;
821 Record.push_back(LangOpts.Trigraphs);
822 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
823 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
824 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
825 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000826 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000827 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
828 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
829 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
830 Record.push_back(LangOpts.C99); // C99 Support
831 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
832 Record.push_back(LangOpts.CPlusPlus); // C++ Support
833 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000834 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000835
Douglas Gregor55abb232009-04-10 20:39:37 +0000836 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
837 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000838 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000839 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000840 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000841 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000842 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000843
Douglas Gregor55abb232009-04-10 20:39:37 +0000844 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000845 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
846 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000847 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000848 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000849 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000850
851 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
852 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
853 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
854
Chris Lattner258172e2009-04-27 07:35:58 +0000855 // Whether static initializers are protected by locks.
856 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000857 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000858 Record.push_back(LangOpts.Blocks); // block extension to C
859 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
860 // they are unused.
861 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
862 // (modulo the platform support).
863
Chris Lattner51924e512010-06-26 21:25:03 +0000864 Record.push_back(LangOpts.getSignedOverflowBehavior());
865 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000866
867 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000868 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000869 // defined.
870 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
871 // opposed to __DYNAMIC__).
872 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
873
874 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
875 // used (instead of C99 semantics).
876 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000877 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
878 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000879 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
880 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000881 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000882 Record.push_back(LangOpts.getGCMode());
883 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000884 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000885 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000886 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000887 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000888 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000889 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000890 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000891}
892
Douglas Gregora7f71a92009-04-10 03:52:48 +0000893//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000894// stat cache Serialization
895//===----------------------------------------------------------------------===//
896
897namespace {
898// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000899class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000900public:
901 typedef const char * key_type;
902 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregorc5046832009-04-27 18:38:38 +0000904 typedef std::pair<int, struct stat> data_type;
905 typedef const data_type& data_type_ref;
906
907 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000908 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000909 }
Mike Stump11289f42009-09-09 15:08:12 +0000910
911 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000912 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
913 data_type_ref Data) {
914 unsigned StrLen = strlen(path);
915 clang::io::Emit16(Out, StrLen);
916 unsigned DataLen = 1; // result value
917 if (Data.first == 0)
918 DataLen += 4 + 4 + 2 + 8 + 8;
919 clang::io::Emit8(Out, DataLen);
920 return std::make_pair(StrLen + 1, DataLen);
921 }
Mike Stump11289f42009-09-09 15:08:12 +0000922
Douglas Gregorc5046832009-04-27 18:38:38 +0000923 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
924 Out.write(path, KeyLen);
925 }
Mike Stump11289f42009-09-09 15:08:12 +0000926
Douglas Gregorc5046832009-04-27 18:38:38 +0000927 void EmitData(llvm::raw_ostream& Out, key_type_ref,
928 data_type_ref Data, unsigned DataLen) {
929 using namespace clang::io;
930 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000931
Douglas Gregorc5046832009-04-27 18:38:38 +0000932 // Result of stat()
933 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000934
Douglas Gregorc5046832009-04-27 18:38:38 +0000935 if (Data.first == 0) {
936 Emit32(Out, (uint32_t) Data.second.st_ino);
937 Emit32(Out, (uint32_t) Data.second.st_dev);
938 Emit16(Out, (uint16_t) Data.second.st_mode);
939 Emit64(Out, (uint64_t) Data.second.st_mtime);
940 Emit64(Out, (uint64_t) Data.second.st_size);
941 }
942
943 assert(Out.tell() - Start == DataLen && "Wrong data length");
944 }
945};
946} // end anonymous namespace
947
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000948/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000949void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000950 // Build the on-disk hash table containing information about every
951 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000952 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000953 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000954 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000955 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000956 Stat != StatEnd; ++Stat, ++NumStatEntries) {
957 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958 Generator.insert(Filename, Stat->second);
959 }
Mike Stump11289f42009-09-09 15:08:12 +0000960
Douglas Gregorc5046832009-04-27 18:38:38 +0000961 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000962 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000963 uint32_t BucketOffset;
964 {
965 llvm::raw_svector_ostream Out(StatCacheData);
966 // Make sure that no bucket is at offset 0
967 clang::io::Emit32(Out, 0);
968 BucketOffset = Generator.Emit(Out);
969 }
970
971 // Create a blob abbreviation
972 using namespace llvm;
973 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000974 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
978 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
979
980 // Write the stat cache
981 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000982 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +0000983 Record.push_back(BucketOffset);
984 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000985 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000986}
987
988//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000989// Source Manager Serialization
990//===----------------------------------------------------------------------===//
991
992/// \brief Create an abbreviation for the SLocEntry that refers to a
993/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000994static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000995 using namespace llvm;
996 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000997 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +0000998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001002 // FileEntry fields.
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001005 // HeaderFileInfo fields.
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001011 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001012}
1013
1014/// \brief Create an abbreviation for the SLocEntry that refers to a
1015/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001016static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001017 using namespace llvm;
1018 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001019 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001025 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001026}
1027
1028/// \brief Create an abbreviation for the SLocEntry that refers to a
1029/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001030static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001031 using namespace llvm;
1032 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001033 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001035 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001036}
1037
1038/// \brief Create an abbreviation for the SLocEntry that refers to an
1039/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001040static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001041 using namespace llvm;
1042 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001043 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001049 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001050}
1051
1052/// \brief Writes the block containing the serialized form of the
1053/// source manager.
1054///
1055/// TODO: We should probably use an on-disk hash table (stored in a
1056/// blob), indexed based on the file name, so that we only create
1057/// entries for files that we actually need. In the common case (no
1058/// errors), we probably won't have to create file entries for any of
1059/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001060void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001061 const Preprocessor &PP,
1062 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001063 RecordData Record;
1064
Chris Lattner0910e3b2009-04-10 17:16:57 +00001065 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001066 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001067
1068 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001069 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1070 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1071 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1072 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001073
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001074 // Write the line table.
1075 if (SourceMgr.hasLineTable()) {
1076 LineTableInfo &LineTable = SourceMgr.getLineTable();
1077
1078 // Emit the file names
1079 Record.push_back(LineTable.getNumFilenames());
1080 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1081 // Emit the file name
1082 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001083 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001084 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1085 Record.push_back(FilenameLen);
1086 if (FilenameLen)
1087 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1088 }
Mike Stump11289f42009-09-09 15:08:12 +00001089
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001090 // Emit the line entries
1091 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1092 L != LEnd; ++L) {
1093 // Emit the file ID
1094 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001095
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001096 // Emit the line entries
1097 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001098 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001099 LEEnd = L->second.end();
1100 LE != LEEnd; ++LE) {
1101 Record.push_back(LE->FileOffset);
1102 Record.push_back(LE->LineNo);
1103 Record.push_back(LE->FilenameID);
1104 Record.push_back((unsigned)LE->FileKind);
1105 Record.push_back(LE->IncludeOffset);
1106 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001107 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001108 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001109 }
1110
Douglas Gregor258ae542009-04-27 06:38:32 +00001111 // Write out the source location entry table. We skip the first
1112 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001113 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001114 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001115 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1116 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1117 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1118 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001119 // Get this source location entry.
1120 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001121
Douglas Gregor258ae542009-04-27 06:38:32 +00001122 // Record the offset of this source-location entry.
1123 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1124
1125 // Figure out which record code to use.
1126 unsigned Code;
1127 if (SLoc->isFile()) {
1128 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001129 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001130 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001131 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001132 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001133 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001134 Record.clear();
1135 Record.push_back(Code);
1136
1137 Record.push_back(SLoc->getOffset());
1138 if (SLoc->isFile()) {
1139 const SrcMgr::FileInfo &File = SLoc->getFile();
1140 Record.push_back(File.getIncludeLoc().getRawEncoding());
1141 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1142 Record.push_back(File.hasLineDirectives());
1143
1144 const SrcMgr::ContentCache *Content = File.getContentCache();
1145 if (Content->Entry) {
1146 // The source location entry is a file. The blob associated
1147 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001148
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001149 // Emit size/modification time for this file.
1150 Record.push_back(Content->Entry->getSize());
1151 Record.push_back(Content->Entry->getModificationTime());
1152
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001153 // Emit header-search information associated with this file.
1154 HeaderFileInfo HFI;
1155 HeaderSearch &HS = PP.getHeaderSearchInfo();
1156 if (Content->Entry->getUID() < HS.header_file_size())
1157 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1158 Record.push_back(HFI.isImport);
1159 Record.push_back(HFI.DirInfo);
1160 Record.push_back(HFI.NumIncludes);
1161 AddIdentifierRef(HFI.ControllingMacro, Record);
1162
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001163 // Turn the file name into an absolute path, if it isn't already.
1164 const char *Filename = Content->Entry->getName();
1165 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001166 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001167 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001168
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001169 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001170 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001171
1172 // FIXME: For now, preload all file source locations, so that
1173 // we get the appropriate File entries in the reader. This is
1174 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001175 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001176 } else {
1177 // The source location entry is a buffer. The blob associated
1178 // with this entry contains the contents of the buffer.
1179
1180 // We add one to the size so that we capture the trailing NULL
1181 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1182 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001183 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001184 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001185 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001186 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1187 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001188 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001189 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001190 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001191 llvm::StringRef(Buffer->getBufferStart(),
1192 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001193
1194 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001195 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001196 }
1197 } else {
1198 // The source location entry is an instantiation.
1199 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1200 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1201 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1202 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1203
1204 // Compute the token length for this macro expansion.
1205 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001206 if (I + 1 != N)
1207 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001208 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1209 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1210 }
1211 }
1212
Douglas Gregor8f45df52009-04-16 22:23:12 +00001213 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001214
1215 if (SLocEntryOffsets.empty())
1216 return;
1217
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001218 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001219 // table is used for lazily loading source-location information.
1220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1226 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001227
Douglas Gregor258ae542009-04-27 06:38:32 +00001228 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001229 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001230 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001231 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1232 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001233 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001234 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001235 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001236
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001237 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001238 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001239 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001240}
1241
Douglas Gregorc5046832009-04-27 18:38:38 +00001242//===----------------------------------------------------------------------===//
1243// Preprocessor Serialization
1244//===----------------------------------------------------------------------===//
1245
Chris Lattnereeffaef2009-04-10 17:15:23 +00001246/// \brief Writes the block containing the serialized form of the
1247/// preprocessor.
1248///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001249void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001250 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001251
Chris Lattner0af3ba12009-04-13 01:29:17 +00001252 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1253 if (PP.getCounterValue() != 0) {
1254 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001255 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001256 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001257 }
1258
1259 // Enter the preprocessor block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001260 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001261
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001262 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001263 // FIXME: use diagnostics subsystem for localization etc.
1264 if (PP.SawDateOrTime())
1265 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001267 // Loop over all the macro definitions that are live at the end of the file,
1268 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001269 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001270 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1271 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001272 // FIXME: This emits macros in hash table order, we should do it in a stable
1273 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001274 MacroInfo *MI = I->second;
1275
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001276 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001277 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001278 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001279
1280 // FIXME: There is a (probably minor) optimization we could do here, if
1281 // the macro comes from the original PCH but the identifier comes from a
1282 // chained PCH, by storing the offset into the original PCH rather than
1283 // writing the macro definition a second time.
1284 if (MI->isBuiltinMacro() ||
1285 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001286 continue;
1287
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001288 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001289 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001290 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1291 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001292
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001293 unsigned Code;
1294 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001295 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001296 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001297 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001298
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001299 Record.push_back(MI->isC99Varargs());
1300 Record.push_back(MI->isGNUVarargs());
1301 Record.push_back(MI->getNumArgs());
1302 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1303 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001304 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001305 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001306
1307 // If we have a detailed preprocessing record, record the macro definition
1308 // ID that corresponds to this macro.
1309 if (PPRec)
1310 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1311
Douglas Gregor8f45df52009-04-16 22:23:12 +00001312 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001313 Record.clear();
1314
Chris Lattner2199f5b2009-04-10 18:08:30 +00001315 // Emit the tokens array.
1316 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1317 // Note that we know that the preprocessor does not have any annotation
1318 // tokens in it because they are created by the parser, and thus can't be
1319 // in a macro definition.
1320 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001321
Chris Lattner2199f5b2009-04-10 18:08:30 +00001322 Record.push_back(Tok.getLocation().getRawEncoding());
1323 Record.push_back(Tok.getLength());
1324
Chris Lattner2199f5b2009-04-10 18:08:30 +00001325 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1326 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001327 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001328
Chris Lattner2199f5b2009-04-10 18:08:30 +00001329 // FIXME: Should translate token kind to a stable encoding.
1330 Record.push_back(Tok.getKind());
1331 // FIXME: Should translate token flags to a stable encoding.
1332 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001333
Sebastian Redl539c5062010-08-18 23:57:32 +00001334 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001335 Record.clear();
1336 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001337 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001338 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001339
1340 // If the preprocessor has a preprocessing record, emit it.
1341 unsigned NumPreprocessingRecords = 0;
1342 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001343 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001344 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1345 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001346 E != EEnd; ++E) {
1347 Record.clear();
1348
1349 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001350 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001351 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1352 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1353 AddIdentifierRef(MI->getName(), Record);
1354 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
Sebastian Redl539c5062010-08-18 23:57:32 +00001355 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001356 continue;
1357 }
1358
1359 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1360 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001361 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregoraae92242010-03-19 21:51:54 +00001362 if (ID != MacroDefinitionOffsets.size()) {
1363 if (ID > MacroDefinitionOffsets.size())
1364 MacroDefinitionOffsets.resize(ID + 1);
1365
1366 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1367 } else
1368 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1369
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001370 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001371 Record.push_back(ID);
1372 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1373 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1374 AddIdentifierRef(MD->getName(), Record);
1375 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001376 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001377 continue;
1378 }
1379 }
1380 }
1381
Douglas Gregor8f45df52009-04-16 22:23:12 +00001382 Stream.ExitBlock();
Douglas Gregoraae92242010-03-19 21:51:54 +00001383
1384 // Write the offsets table for the preprocessing record.
1385 if (NumPreprocessingRecords > 0) {
1386 // Write the offsets table for identifier IDs.
1387 using namespace llvm;
1388 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001389 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1393 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1394
1395 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001396 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001397 Record.push_back(NumPreprocessingRecords);
1398 Record.push_back(MacroDefinitionOffsets.size());
1399 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001400 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001401 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1402 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001403}
1404
Douglas Gregorc5046832009-04-27 18:38:38 +00001405//===----------------------------------------------------------------------===//
1406// Type Serialization
1407//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001408
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001409/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001410void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001411 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001412 if (Idx.getIndex() == 0) // we haven't seen this type before.
1413 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001414
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001415 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001416 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001417 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001418 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001419 else if (TypeOffsets.size() < Index) {
1420 TypeOffsets.resize(Index + 1);
1421 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001422 }
1423
1424 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001425
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001426 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001427 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001428
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001429 if (T.hasLocalNonFastQualifiers()) {
1430 Qualifiers Qs = T.getLocalQualifiers();
1431 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001432 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001433 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001434 } else {
1435 switch (T->getTypeClass()) {
1436 // For all of the concrete, non-dependent types, call the
1437 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001438#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001439 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001440#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001441#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001442 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001443 }
1444
1445 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001446 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001447
1448 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001449 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001450}
1451
Douglas Gregorc5046832009-04-27 18:38:38 +00001452//===----------------------------------------------------------------------===//
1453// Declaration Serialization
1454//===----------------------------------------------------------------------===//
1455
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001456/// \brief Write the block containing all of the declaration IDs
1457/// lexically declared within the given DeclContext.
1458///
1459/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1460/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001461uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001462 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001463 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001464 return 0;
1465
Douglas Gregor8f45df52009-04-16 22:23:12 +00001466 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001467 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001468 Record.push_back(DECL_CONTEXT_LEXICAL);
1469 llvm::SmallVector<DeclID, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001470 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1471 D != DEnd; ++D)
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001472 Decls.push_back(GetDeclRef(*D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001473
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001474 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001475 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Sebastian Redl539c5062010-08-18 23:57:32 +00001476 reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(DeclID));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001477 return Offset;
1478}
1479
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001480void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001481 using namespace llvm;
1482 RecordData Record;
1483
1484 // Write the type offsets array
1485 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001486 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001487 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1489 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1490 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001491 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001492 Record.push_back(TypeOffsets.size());
1493 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001494 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001495 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1496
1497 // Write the declaration offsets array
1498 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001499 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1502 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1503 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001504 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001505 Record.push_back(DeclOffsets.size());
1506 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001507 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001508 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1509}
1510
Douglas Gregorc5046832009-04-27 18:38:38 +00001511//===----------------------------------------------------------------------===//
1512// Global Method Pool and Selector Serialization
1513//===----------------------------------------------------------------------===//
1514
Douglas Gregore84a9da2009-04-20 20:36:09 +00001515namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001516// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001517class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001518 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001519
1520public:
1521 typedef Selector key_type;
1522 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001523
Sebastian Redl834bb972010-08-04 17:20:04 +00001524 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001525 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001526 ObjCMethodList Instance, Factory;
1527 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001528 typedef const data_type& data_type_ref;
1529
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001530 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001531
Douglas Gregorc78d3462009-04-24 21:10:55 +00001532 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001533 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001534 }
Mike Stump11289f42009-09-09 15:08:12 +00001535
1536 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001537 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1538 data_type_ref Methods) {
1539 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1540 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001541 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1542 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001543 Method = Method->Next)
1544 if (Method->Method)
1545 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001546 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001547 Method = Method->Next)
1548 if (Method->Method)
1549 DataLen += 4;
1550 clang::io::Emit16(Out, DataLen);
1551 return std::make_pair(KeyLen, DataLen);
1552 }
Mike Stump11289f42009-09-09 15:08:12 +00001553
Douglas Gregor95c13f52009-04-25 17:48:32 +00001554 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001555 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001556 assert((Start >> 32) == 0 && "Selector key offset too large");
1557 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001558 unsigned N = Sel.getNumArgs();
1559 clang::io::Emit16(Out, N);
1560 if (N == 0)
1561 N = 1;
1562 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001563 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001564 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1565 }
Mike Stump11289f42009-09-09 15:08:12 +00001566
Douglas Gregorc78d3462009-04-24 21:10:55 +00001567 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001568 data_type_ref Methods, unsigned DataLen) {
1569 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001570 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001571 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001572 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001573 Method = Method->Next)
1574 if (Method->Method)
1575 ++NumInstanceMethods;
1576
1577 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001578 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001579 Method = Method->Next)
1580 if (Method->Method)
1581 ++NumFactoryMethods;
1582
1583 clang::io::Emit16(Out, NumInstanceMethods);
1584 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001585 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001586 Method = Method->Next)
1587 if (Method->Method)
1588 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001589 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001590 Method = Method->Next)
1591 if (Method->Method)
1592 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001593
1594 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001595 }
1596};
1597} // end anonymous namespace
1598
Sebastian Redla19a67f2010-08-03 21:58:15 +00001599/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001600///
1601/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001602/// in an on-disk hash table indexed by the selector. The hash table also
1603/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001604void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001605 using namespace llvm;
1606
Sebastian Redla19a67f2010-08-03 21:58:15 +00001607 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001608 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001609 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001610 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001611 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001612 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001613 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001614 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001615
Sebastian Redla19a67f2010-08-03 21:58:15 +00001616 // Create the on-disk hash table representation. We walk through every
1617 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001618 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001619 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001620 I = SelectorIDs.begin(), E = SelectorIDs.end();
1621 I != E; ++I) {
1622 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001623 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001624 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001625 I->second,
1626 ObjCMethodList(),
1627 ObjCMethodList()
1628 };
1629 if (F != SemaRef.MethodPool.end()) {
1630 Data.Instance = F->second.first;
1631 Data.Factory = F->second.second;
1632 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001633 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001634 // changed.
1635 if (Chain && I->second < FirstSelectorID) {
1636 // Selector already exists. Did it change?
1637 bool changed = false;
1638 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1639 M = M->Next) {
1640 if (M->Method->getPCHLevel() == 0)
1641 changed = true;
1642 }
1643 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1644 M = M->Next) {
1645 if (M->Method->getPCHLevel() == 0)
1646 changed = true;
1647 }
1648 if (!changed)
1649 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001650 } else if (Data.Instance.Method || Data.Factory.Method) {
1651 // A new method pool entry.
1652 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001653 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001654 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001655 }
1656
Douglas Gregorc78d3462009-04-24 21:10:55 +00001657 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001658 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001659 uint32_t BucketOffset;
1660 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001661 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001662 llvm::raw_svector_ostream Out(MethodPool);
1663 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001664 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001665 BucketOffset = Generator.Emit(Out, Trait);
1666 }
1667
1668 // Create a blob abbreviation
1669 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001670 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001671 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001672 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001673 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1674 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1675
Douglas Gregor95c13f52009-04-25 17:48:32 +00001676 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001677 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001678 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001679 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001680 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001681 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001682
1683 // Create a blob abbreviation for the selector table offsets.
1684 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001685 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1688 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1689
1690 // Write the selector offsets table.
1691 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001692 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001693 Record.push_back(SelectorOffsets.size());
1694 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001695 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001696 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001697 }
1698}
1699
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001700/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001701void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001702 using namespace llvm;
1703 if (SemaRef.ReferencedSelectors.empty())
1704 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001705
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001706 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001707
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001708 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001709 // very tricky to fix, and given that @selector shouldn't really appear in
1710 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001711 for (DenseMap<Selector, SourceLocation>::iterator S =
1712 SemaRef.ReferencedSelectors.begin(),
1713 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1714 Selector Sel = (*S).first;
1715 SourceLocation Loc = (*S).second;
1716 AddSelectorRef(Sel, Record);
1717 AddSourceLocation(Loc, Record);
1718 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001719 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001720}
1721
Douglas Gregorc5046832009-04-27 18:38:38 +00001722//===----------------------------------------------------------------------===//
1723// Identifier Table Serialization
1724//===----------------------------------------------------------------------===//
1725
Douglas Gregorc78d3462009-04-24 21:10:55 +00001726namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001727class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001728 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001729 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001730
Douglas Gregor1d583f22009-04-28 21:18:29 +00001731 /// \brief Determines whether this is an "interesting" identifier
1732 /// that needs a full IdentifierInfo structure written into the hash
1733 /// table.
1734 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1735 return II->isPoisoned() ||
1736 II->isExtensionToken() ||
1737 II->hasMacroDefinition() ||
1738 II->getObjCOrBuiltinID() ||
1739 II->getFETokenInfo<void>();
1740 }
1741
Douglas Gregore84a9da2009-04-20 20:36:09 +00001742public:
1743 typedef const IdentifierInfo* key_type;
1744 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001745
Sebastian Redl539c5062010-08-18 23:57:32 +00001746 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001747 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001748
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001749 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001750 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001751
1752 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001753 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001754 }
Mike Stump11289f42009-09-09 15:08:12 +00001755
1756 std::pair<unsigned,unsigned>
1757 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001758 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001759 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001760 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1761 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001762 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001763 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001764 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001765 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001766 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1767 DEnd = IdentifierResolver::end();
1768 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001769 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001770 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001771 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001772 // We emit the key length after the data length so that every
1773 // string is preceded by a 16-bit length. This matches the PTH
1774 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001775 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001776 return std::make_pair(KeyLen, DataLen);
1777 }
Mike Stump11289f42009-09-09 15:08:12 +00001778
1779 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001780 unsigned KeyLen) {
1781 // Record the location of the key data. This is used when generating
1782 // the mapping from persistent IDs to strings.
1783 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001784 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001785 }
Mike Stump11289f42009-09-09 15:08:12 +00001786
1787 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001788 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001789 if (!isInterestingIdentifier(II)) {
1790 clang::io::Emit32(Out, ID << 1);
1791 return;
1792 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001793
Douglas Gregor1d583f22009-04-28 21:18:29 +00001794 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001795 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001796 bool hasMacroDefinition =
1797 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001798 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001799 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001800 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1801 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1802 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001803 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001804 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001805 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001806
Douglas Gregorc3366a52009-04-21 23:56:24 +00001807 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001808 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001809
Douglas Gregora868bbd2009-04-21 22:25:48 +00001810 // Emit the declaration IDs in reverse order, because the
1811 // IdentifierResolver provides the declarations as they would be
1812 // visible (e.g., the function "stat" would come before the struct
1813 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1814 // adds declarations to the end of the list (so we need to see the
1815 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001816 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001817 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001818 IdentifierResolver::end());
1819 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1820 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001821 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001822 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001823 }
1824};
1825} // end anonymous namespace
1826
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001827/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001828///
1829/// The identifier table consists of a blob containing string data
1830/// (the actual identifiers themselves) and a separate "offsets" index
1831/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001832void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001833 using namespace llvm;
1834
1835 // Create and write out the blob that contains the identifier
1836 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001837 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001838 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001839 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001840
Douglas Gregore6648fb2009-04-28 20:33:11 +00001841 // Look for any identifiers that were named while processing the
1842 // headers, but are otherwise not needed. We add these to the hash
1843 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001844 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001845 // file.
1846 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1847 IDEnd = PP.getIdentifierTable().end();
1848 ID != IDEnd; ++ID)
1849 getIdentifierRef(ID->second);
1850
Sebastian Redlff4a2952010-07-23 23:49:55 +00001851 // Create the on-disk hash table representation. We only store offsets
1852 // for identifiers that appear here for the first time.
1853 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001854 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001855 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1856 ID != IDEnd; ++ID) {
1857 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001858 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001859 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001860 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001861
Douglas Gregore84a9da2009-04-20 20:36:09 +00001862 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001863 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001864 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001865 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001866 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001867 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001868 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001869 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001870 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001871 }
1872
1873 // Create a blob abbreviation
1874 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001875 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001878 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001879
1880 // Write the identifier table
1881 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001882 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001883 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001884 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001885 }
1886
1887 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001888 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001889 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1892 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1893
1894 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001895 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001896 Record.push_back(IdentifierOffsets.size());
1897 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001898 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001899 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001900}
1901
Douglas Gregorc5046832009-04-27 18:38:38 +00001902//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001903// DeclContext's Name Lookup Table Serialization
1904//===----------------------------------------------------------------------===//
1905
1906namespace {
1907// Trait used for the on-disk hash table used in the method pool.
1908class ASTDeclContextNameLookupTrait {
1909 ASTWriter &Writer;
1910
1911public:
1912 typedef DeclarationName key_type;
1913 typedef key_type key_type_ref;
1914
1915 typedef DeclContext::lookup_result data_type;
1916 typedef const data_type& data_type_ref;
1917
1918 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1919
1920 unsigned ComputeHash(DeclarationName Name) {
1921 llvm::FoldingSetNodeID ID;
1922 ID.AddInteger(Name.getNameKind());
1923
1924 switch (Name.getNameKind()) {
1925 case DeclarationName::Identifier:
1926 ID.AddString(Name.getAsIdentifierInfo()->getName());
1927 break;
1928 case DeclarationName::ObjCZeroArgSelector:
1929 case DeclarationName::ObjCOneArgSelector:
1930 case DeclarationName::ObjCMultiArgSelector:
1931 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1932 break;
1933 case DeclarationName::CXXConstructorName:
1934 case DeclarationName::CXXDestructorName:
1935 case DeclarationName::CXXConversionFunctionName:
1936 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1937 break;
1938 case DeclarationName::CXXOperatorName:
1939 ID.AddInteger(Name.getCXXOverloadedOperator());
1940 break;
1941 case DeclarationName::CXXLiteralOperatorName:
1942 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1943 case DeclarationName::CXXUsingDirective:
1944 break;
1945 }
1946
1947 return ID.ComputeHash();
1948 }
1949
1950 std::pair<unsigned,unsigned>
1951 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
1952 data_type_ref Lookup) {
1953 unsigned KeyLen = 1;
1954 switch (Name.getNameKind()) {
1955 case DeclarationName::Identifier:
1956 case DeclarationName::ObjCZeroArgSelector:
1957 case DeclarationName::ObjCOneArgSelector:
1958 case DeclarationName::ObjCMultiArgSelector:
1959 case DeclarationName::CXXConstructorName:
1960 case DeclarationName::CXXDestructorName:
1961 case DeclarationName::CXXConversionFunctionName:
1962 case DeclarationName::CXXLiteralOperatorName:
1963 KeyLen += 4;
1964 break;
1965 case DeclarationName::CXXOperatorName:
1966 KeyLen += 1;
1967 break;
1968 case DeclarationName::CXXUsingDirective:
1969 break;
1970 }
1971 clang::io::Emit16(Out, KeyLen);
1972
1973 // 2 bytes for num of decls and 4 for each DeclID.
1974 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
1975 clang::io::Emit16(Out, DataLen);
1976
1977 return std::make_pair(KeyLen, DataLen);
1978 }
1979
1980 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
1981 using namespace clang::io;
1982
1983 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
1984 Emit8(Out, Name.getNameKind());
1985 switch (Name.getNameKind()) {
1986 case DeclarationName::Identifier:
1987 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
1988 break;
1989 case DeclarationName::ObjCZeroArgSelector:
1990 case DeclarationName::ObjCOneArgSelector:
1991 case DeclarationName::ObjCMultiArgSelector:
1992 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
1993 break;
1994 case DeclarationName::CXXConstructorName:
1995 case DeclarationName::CXXDestructorName:
1996 case DeclarationName::CXXConversionFunctionName:
1997 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
1998 break;
1999 case DeclarationName::CXXOperatorName:
2000 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2001 Emit8(Out, Name.getCXXOverloadedOperator());
2002 break;
2003 case DeclarationName::CXXLiteralOperatorName:
2004 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2005 break;
2006 case DeclarationName::CXXUsingDirective:
2007 break;
2008 }
2009 }
2010
2011 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2012 data_type Lookup, unsigned DataLen) {
2013 uint64_t Start = Out.tell(); (void)Start;
2014 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2015 for (; Lookup.first != Lookup.second; ++Lookup.first)
2016 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2017
2018 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2019 }
2020};
2021} // end anonymous namespace
2022
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002023/// \brief Write the block containing all of the declaration IDs
2024/// visible from the given DeclContext.
2025///
2026/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002027/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002028uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2029 DeclContext *DC) {
2030 if (DC->getPrimaryContext() != DC)
2031 return 0;
2032
2033 // Since there is no name lookup into functions or methods, don't bother to
2034 // build a visible-declarations table for these entities.
2035 if (DC->isFunctionOrMethod())
2036 return 0;
2037
2038 // If not in C++, we perform name lookup for the translation unit via the
2039 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2040 // FIXME: In C++ we need the visible declarations in order to "see" the
2041 // friend declarations, is there a way to do this without writing the table ?
2042 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2043 return 0;
2044
2045 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002046 if (DC->hasExternalVisibleStorage())
2047 DC->MaterializeVisibleDeclsFromExternalStorage();
2048 else
2049 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002050
2051 // Serialize the contents of the mapping used for lookup. Note that,
2052 // although we have two very different code paths, the serialized
2053 // representation is the same for both cases: a declaration name,
2054 // followed by a size, followed by references to the visible
2055 // declarations that have that name.
2056 uint64_t Offset = Stream.GetCurrentBitNo();
2057 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2058 if (!Map || Map->empty())
2059 return 0;
2060
2061 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2062 ASTDeclContextNameLookupTrait Trait(*this);
2063
2064 // Create the on-disk hash table representation.
2065 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2066 D != DEnd; ++D) {
2067 DeclarationName Name = D->first;
2068 DeclContext::lookup_result Result = D->second.getLookupResult();
2069 Generator.insert(Name, Result, Trait);
2070 }
2071
2072 // Create the on-disk hash table in a buffer.
2073 llvm::SmallString<4096> LookupTable;
2074 uint32_t BucketOffset;
2075 {
2076 llvm::raw_svector_ostream Out(LookupTable);
2077 // Make sure that no bucket is at offset 0
2078 clang::io::Emit32(Out, 0);
2079 BucketOffset = Generator.Emit(Out, Trait);
2080 }
2081
2082 // Write the lookup table
2083 RecordData Record;
2084 Record.push_back(DECL_CONTEXT_VISIBLE);
2085 Record.push_back(BucketOffset);
2086 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2087 LookupTable.str());
2088
2089 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2090 ++NumVisibleDeclContexts;
2091 return Offset;
2092}
2093
Sebastian Redla4071b42010-08-24 00:50:09 +00002094/// \brief Write an UPDATE_VISIBLE block for the given context.
2095///
2096/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2097/// DeclContext in a dependent AST file. As such, they only exist for the TU
2098/// (in C++) and for namespaces.
2099void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
2100 assert((DC->isTranslationUnit() || DC->isNamespace()) &&
2101 "Only TU and namespaces should have visible decl updates.");
2102
2103 // Make the context build its lookup table, but don't make it load external
2104 // decls.
2105 DC->lookup(DeclarationName());
2106
2107 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2108 if (!Map || Map->empty())
2109 return;
2110
2111 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2112 ASTDeclContextNameLookupTrait Trait(*this);
2113
2114 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002115 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2116 D != DEnd; ++D) {
2117 DeclarationName Name = D->first;
2118 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002119 // For any name that appears in this table, the results are complete, i.e.
2120 // they overwrite results from previous PCHs. Merging is always a mess.
2121 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002122 }
2123
2124 // Create the on-disk hash table in a buffer.
2125 llvm::SmallString<4096> LookupTable;
2126 uint32_t BucketOffset;
2127 {
2128 llvm::raw_svector_ostream Out(LookupTable);
2129 // Make sure that no bucket is at offset 0
2130 clang::io::Emit32(Out, 0);
2131 BucketOffset = Generator.Emit(Out, Trait);
2132 }
2133
2134 // Write the lookup table
2135 RecordData Record;
2136 Record.push_back(UPDATE_VISIBLE);
2137 Record.push_back(getDeclID(cast<Decl>(DC)));
2138 Record.push_back(BucketOffset);
2139 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2140}
2141
Sebastian Redl401b39a2010-08-24 22:50:24 +00002142/// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates
2143/// that have new specializations in the current AST file.
2144void ASTWriter::WriteAdditionalTemplateSpecializations() {
2145 RecordData Record;
2146 for (AdditionalTemplateSpecializationsMap::iterator
2147 I = AdditionalTemplateSpecializations.begin(),
2148 E = AdditionalTemplateSpecializations.end();
2149 I != E; ++I) {
2150 Record.clear();
2151 Record.push_back(I->first);
2152 Record.insert(Record.end(), I->second.begin(), I->second.end());
2153 Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record);
2154 }
2155}
2156
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002157//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002158// General Serialization Routines
2159//===----------------------------------------------------------------------===//
2160
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002161/// \brief Write a record containing the given attributes.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002162void ASTWriter::WriteAttributeRecord(const AttrVec &Attrs) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002163 RecordData Record;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002164 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2165 const Attr * A = *i;
2166 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2167 AddSourceLocation(A->getLocation(), Record);
2168 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002169
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002170#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002171
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002172 }
2173
Sebastian Redl539c5062010-08-18 23:57:32 +00002174 Stream.EmitRecord(DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002175}
2176
Benjamin Kramercd495032010-09-02 15:06:24 +00002177void ASTWriter::AddString(llvm::StringRef Str, RecordData &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002178 Record.push_back(Str.size());
2179 Record.insert(Record.end(), Str.begin(), Str.end());
2180}
2181
Douglas Gregore84a9da2009-04-20 20:36:09 +00002182/// \brief Note that the identifier II occurs at the given offset
2183/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002184void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002185 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002186 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002187 // up earlier in the chain and thus don't need an offset.
2188 if (ID >= FirstIdentID)
2189 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002190}
2191
Douglas Gregor95c13f52009-04-25 17:48:32 +00002192/// \brief Note that the selector Sel occurs at the given offset
2193/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002194void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002195 unsigned ID = SelectorIDs[Sel];
2196 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002197 // Don't record offsets for selectors that are also available in a different
2198 // file.
2199 if (ID < FirstSelectorID)
2200 return;
2201 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002202}
2203
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002204ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redld95a56e2010-08-04 18:21:41 +00002205 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002206 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002207 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
2208 NextSelectorID(FirstSelectorID), CollectedStmts(&StmtsToEmit),
2209 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2210 NumVisibleDeclContexts(0) {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002211}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002212
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002213void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002214 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002215 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002216 Stream.Emit((unsigned)'C', 8);
2217 Stream.Emit((unsigned)'P', 8);
2218 Stream.Emit((unsigned)'C', 8);
2219 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002220
Chris Lattner28fa4e62009-04-26 22:26:21 +00002221 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002222
Sebastian Redl143413f2010-07-12 22:02:52 +00002223 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002224 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002225 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002226 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002227}
2228
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002229void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002230 const char *isysroot) {
2231 using namespace llvm;
2232
2233 ASTContext &Context = SemaRef.Context;
2234 Preprocessor &PP = SemaRef.PP;
2235
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002236 // The translation unit is the first declaration we'll emit.
2237 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002238 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002239 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002240
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002241 // Make sure that we emit IdentifierInfos (and any attached
2242 // declarations) for builtins.
2243 {
2244 IdentifierTable &Table = PP.getIdentifierTable();
2245 llvm::SmallVector<const char *, 32> BuiltinNames;
2246 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2247 Context.getLangOptions().NoBuiltin);
2248 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2249 getIdentifierRef(&Table.get(BuiltinNames[I]));
2250 }
2251
Chris Lattner0c797362009-09-08 18:19:27 +00002252 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002253 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002254 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002255 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002256 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2257 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002258 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002259
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002260 // Build a record containing all of the file scoped decls in this file.
2261 RecordData UnusedFileScopedDecls;
2262 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2263 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002264
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002265 RecordData WeakUndeclaredIdentifiers;
2266 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2267 WeakUndeclaredIdentifiers.push_back(
2268 SemaRef.WeakUndeclaredIdentifiers.size());
2269 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2270 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2271 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2272 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2273 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2274 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2275 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2276 }
2277 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002278
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002279 // Build a record containing all of the locally-scoped external
2280 // declarations in this header file. Generally, this record will be
2281 // empty.
2282 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002283 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002284 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002285 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002286 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2287 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2288 TD != TDEnd; ++TD)
2289 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2290
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002291 // Build a record containing all of the ext_vector declarations.
2292 RecordData ExtVectorDecls;
2293 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2294 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2295
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002296 // Build a record containing all of the VTable uses information.
2297 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002298 if (!SemaRef.VTableUses.empty()) {
2299 VTableUses.push_back(SemaRef.VTableUses.size());
2300 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2301 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2302 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2303 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2304 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002305 }
2306
2307 // Build a record containing all of dynamic classes declarations.
2308 RecordData DynamicClasses;
2309 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2310 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2311
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002312 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002313 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002314 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002315 I = SemaRef.PendingInstantiations.begin(),
2316 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2317 AddDeclRef(I->first, PendingInstantiations);
2318 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002319 }
2320 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2321 "There are local ones at end of translation unit!");
2322
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002323 // Build a record containing some declaration references.
2324 RecordData SemaDeclRefs;
2325 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2326 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2327 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2328 }
2329
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002330 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002331 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002332 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002333 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002334 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002335 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002336 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002337 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002338 // Write the record of special types.
2339 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002340
Steve Naroffc277ad12009-07-18 15:33:26 +00002341 AddTypeRef(Context.getBuiltinVaListType(), Record);
2342 AddTypeRef(Context.getObjCIdType(), Record);
2343 AddTypeRef(Context.getObjCSelType(), Record);
2344 AddTypeRef(Context.getObjCProtoType(), Record);
2345 AddTypeRef(Context.getObjCClassType(), Record);
2346 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2347 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2348 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002349 AddTypeRef(Context.getjmp_bufType(), Record);
2350 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002351 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2352 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002353 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002354 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002355 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2356 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002357 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002358 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002359
Douglas Gregor1970d882009-04-26 03:49:13 +00002360 // Keep writing types and declarations until all types and
2361 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002362 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002363 WriteDeclsBlockAbbrevs();
2364 while (!DeclTypesToEmit.empty()) {
2365 DeclOrType DOT = DeclTypesToEmit.front();
2366 DeclTypesToEmit.pop();
2367 if (DOT.isType())
2368 WriteType(DOT.getType());
2369 else
2370 WriteDecl(Context, DOT.getDecl());
2371 }
2372 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002373
Douglas Gregor45053152009-10-17 17:25:45 +00002374 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002375 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002376 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002377 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002378
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002379 WriteTypeDeclOffsets();
Douglas Gregor652d82a2009-04-18 05:55:16 +00002380
Douglas Gregord4df8652009-04-22 22:02:47 +00002381 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002382 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002383 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002384
2385 // Write the record containing tentative definitions.
2386 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002387 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002388
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002389 // Write the record containing unused file scoped decls.
2390 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002391 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002392
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002393 // Write the record containing weak undeclared identifiers.
2394 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002395 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002396 WeakUndeclaredIdentifiers);
2397
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002398 // Write the record containing locally-scoped external definitions.
2399 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002400 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002401 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002402
2403 // Write the record containing ext_vector type names.
2404 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002405 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002406
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002407 // Write the record containing VTable uses information.
2408 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002409 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002410
2411 // Write the record containing dynamic classes declarations.
2412 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002413 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002414
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002415 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002416 if (!PendingInstantiations.empty())
2417 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002418
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002419 // Write the record containing declaration references of Sema.
2420 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002421 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002422
Douglas Gregor08f01292009-04-17 22:13:46 +00002423 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002424 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002425 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002426 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002427 Record.push_back(NumLexicalDeclContexts);
2428 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002429 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002430 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002431}
2432
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002433void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002434 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002435 using namespace llvm;
2436
Sebastian Redl07a89a82010-07-30 00:29:29 +00002437 FirstDeclID += Chain->getTotalNumDecls();
2438 FirstTypeID += Chain->getTotalNumTypes();
2439 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redld95a56e2010-08-04 18:21:41 +00002440 FirstSelectorID += Chain->getTotalNumSelectors();
Sebastian Redl07a89a82010-07-30 00:29:29 +00002441 NextDeclID = FirstDeclID;
2442 NextTypeID = FirstTypeID;
2443 NextIdentID = FirstIdentID;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002444 NextSelectorID = FirstSelectorID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00002445
Sebastian Redl143413f2010-07-12 22:02:52 +00002446 ASTContext &Context = SemaRef.Context;
2447 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002448
Sebastian Redl143413f2010-07-12 22:02:52 +00002449 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002450 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002451 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002452 if (StatCalls && !isysroot)
2453 WriteStatCache(*StatCalls);
2454 // FIXME: Source manager block should only write new stuff, which could be
2455 // done by tracking the largest ID in the chain
2456 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002457
2458 // The special types are in the chained PCH.
2459
2460 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002461 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002462 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Sebastian Redl539c5062010-08-18 23:57:32 +00002463 llvm::SmallVector<DeclID, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002464 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2465 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002466 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002467 if ((*I)->getPCHLevel() == 0)
2468 NewGlobalDecls.push_back(GetDeclRef(*I));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002469 else if ((*I)->isChangedSinceDeserialization())
2470 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002471 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002472 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002473 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002474 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002475 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2476 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2477 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002478 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002479 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2480 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Sebastian Redl539c5062010-08-18 23:57:32 +00002481 NewGlobalDecls.size() * sizeof(DeclID));
Sebastian Redla4071b42010-08-24 00:50:09 +00002482 // And in C++, a visible updates block for the TU.
2483 if (Context.getLangOptions().CPlusPlus) {
2484 Abv = new llvm::BitCodeAbbrev();
2485 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2486 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2487 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2488 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2489 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2490 WriteDeclContextVisibleUpdate(TU);
2491 }
Sebastian Redl143413f2010-07-12 22:02:52 +00002492
Sebastian Redl98912122010-07-27 23:01:28 +00002493 // Build a record containing all of the new tentative definitions in this
2494 // file, in TentativeDefinitions order.
2495 RecordData TentativeDefinitions;
2496 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2497 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2498 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2499 }
2500
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002501 // Build a record containing all of the file scoped decls in this file.
2502 RecordData UnusedFileScopedDecls;
2503 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2504 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2505 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002506 }
2507
Sebastian Redl08aca90252010-08-05 18:21:25 +00002508 // We write the entire table, overwriting the tables from the chain.
2509 RecordData WeakUndeclaredIdentifiers;
2510 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2511 WeakUndeclaredIdentifiers.push_back(
2512 SemaRef.WeakUndeclaredIdentifiers.size());
2513 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2514 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2515 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2516 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2517 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2518 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2519 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2520 }
2521 }
2522
Sebastian Redl98912122010-07-27 23:01:28 +00002523 // Build a record containing all of the locally-scoped external
2524 // declarations in this header file. Generally, this record will be
2525 // empty.
2526 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002527 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002528 // nondeterminstic!
2529 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2530 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2531 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2532 TD != TDEnd; ++TD) {
2533 if (TD->second->getPCHLevel() == 0)
2534 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2535 }
2536
2537 // Build a record containing all of the ext_vector declarations.
2538 RecordData ExtVectorDecls;
2539 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2540 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2541 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2542 }
2543
Sebastian Redl08aca90252010-08-05 18:21:25 +00002544 // Build a record containing all of the VTable uses information.
2545 // We write everything here, because it's too hard to determine whether
2546 // a use is new to this part.
2547 RecordData VTableUses;
2548 if (!SemaRef.VTableUses.empty()) {
2549 VTableUses.push_back(SemaRef.VTableUses.size());
2550 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2551 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2552 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2553 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2554 }
2555 }
2556
2557 // Build a record containing all of dynamic classes declarations.
2558 RecordData DynamicClasses;
2559 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2560 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2561 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2562
2563 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002564 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002565 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002566 I = SemaRef.PendingInstantiations.begin(),
2567 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002568 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002569 AddDeclRef(I->first, PendingInstantiations);
2570 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002571 }
2572 }
2573 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2574 "There are local ones at end of translation unit!");
2575
2576 // Build a record containing some declaration references.
2577 // It's not worth the effort to avoid duplication here.
2578 RecordData SemaDeclRefs;
2579 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2580 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2581 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2582 }
2583
Sebastian Redl539c5062010-08-18 23:57:32 +00002584 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002585 WriteDeclsBlockAbbrevs();
2586 while (!DeclTypesToEmit.empty()) {
2587 DeclOrType DOT = DeclTypesToEmit.front();
2588 DeclTypesToEmit.pop();
2589 if (DOT.isType())
2590 WriteType(DOT.getType());
2591 else
2592 WriteDecl(Context, DOT.getDecl());
2593 }
2594 Stream.ExitBlock();
2595
Sebastian Redl98912122010-07-27 23:01:28 +00002596 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002597 WriteSelectors(SemaRef);
2598 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002599 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002600 WriteTypeDeclOffsets();
Sebastian Redl98912122010-07-27 23:01:28 +00002601
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002602 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002603 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002604 RecordData FirstLatestDeclIDs;
2605 for (FirstLatestDeclMap::iterator
2606 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2607 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2608 "Expected first & second to be in different PCHs");
2609 AddDeclRef(I->first, FirstLatestDeclIDs);
2610 AddDeclRef(I->second, FirstLatestDeclIDs);
2611 }
2612 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002613 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002614
Sebastian Redl98912122010-07-27 23:01:28 +00002615 // Write the record containing external, unnamed definitions.
2616 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002617 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002618
2619 // Write the record containing tentative definitions.
2620 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002621 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002622
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002623 // Write the record containing unused file scoped decls.
2624 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002625 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002626
Sebastian Redl08aca90252010-08-05 18:21:25 +00002627 // Write the record containing weak undeclared identifiers.
2628 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002629 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002630 WeakUndeclaredIdentifiers);
2631
Sebastian Redl98912122010-07-27 23:01:28 +00002632 // Write the record containing locally-scoped external definitions.
2633 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002634 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002635 LocallyScopedExternalDecls);
2636
2637 // Write the record containing ext_vector type names.
2638 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002639 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002640
Sebastian Redl08aca90252010-08-05 18:21:25 +00002641 // Write the record containing VTable uses information.
2642 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002643 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002644
2645 // Write the record containing dynamic classes declarations.
2646 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002647 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002648
2649 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002650 if (!PendingInstantiations.empty())
2651 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002652
2653 // Write the record containing declaration references of Sema.
2654 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002655 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002656
Sebastian Redla4071b42010-08-24 00:50:09 +00002657 // Write the updates to C++ namespaces.
2658 for (llvm::SmallPtrSet<const NamespaceDecl *, 16>::iterator
2659 I = UpdatedNamespaces.begin(),
2660 E = UpdatedNamespaces.end();
2661 I != E; ++I)
2662 WriteDeclContextVisibleUpdate(*I);
2663
Sebastian Redl401b39a2010-08-24 22:50:24 +00002664 // Write the updates to C++ template specialization lists.
2665 if (!AdditionalTemplateSpecializations.empty())
2666 WriteAdditionalTemplateSpecializations();
2667
Sebastian Redl98912122010-07-27 23:01:28 +00002668 Record.clear();
2669 Record.push_back(NumStatements);
2670 Record.push_back(NumMacros);
2671 Record.push_back(NumLexicalDeclContexts);
2672 Record.push_back(NumVisibleDeclContexts);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002673 WriteDeclUpdateBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002674 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002675 Stream.ExitBlock();
2676}
2677
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002678void ASTWriter::WriteDeclUpdateBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002679 if (ReplacedDecls.empty())
2680 return;
2681
2682 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002683 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002684 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2685 Record.push_back(I->first);
2686 Record.push_back(I->second);
2687 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002688 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002689}
2690
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002691void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002692 Record.push_back(Loc.getRawEncoding());
2693}
2694
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002695void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002696 AddSourceLocation(Range.getBegin(), Record);
2697 AddSourceLocation(Range.getEnd(), Record);
2698}
2699
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002700void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002701 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002702 const uint64_t *Words = Value.getRawData();
2703 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002704}
2705
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002706void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002707 Record.push_back(Value.isUnsigned());
2708 AddAPInt(Value, Record);
2709}
2710
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002711void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002712 AddAPInt(Value.bitcastToAPInt(), Record);
2713}
2714
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002715void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002716 Record.push_back(getIdentifierRef(II));
2717}
2718
Sebastian Redl539c5062010-08-18 23:57:32 +00002719IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002720 if (II == 0)
2721 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002722
Sebastian Redl539c5062010-08-18 23:57:32 +00002723 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002724 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002725 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002726 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727}
2728
Sebastian Redl50e26582010-09-15 19:54:06 +00002729MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002730 if (MD == 0)
2731 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002732
2733 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002734 if (ID == 0)
2735 ID = MacroDefinitions.size();
2736 return ID;
2737}
2738
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002739void ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002740 Record.push_back(getSelectorRef(SelRef));
2741}
2742
Sebastian Redl539c5062010-08-18 23:57:32 +00002743SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002744 if (Sel.getAsOpaquePtr() == 0) {
2745 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002746 }
2747
Sebastian Redl539c5062010-08-18 23:57:32 +00002748 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002749 if (SID == 0 && Chain) {
2750 // This might trigger a ReadSelector callback, which will set the ID for
2751 // this selector.
2752 Chain->LoadSelector(Sel);
2753 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002754 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002755 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002756 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002757 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002758}
2759
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002760void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002761 AddDeclRef(Temp->getDestructor(), Record);
2762}
2763
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002764void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002765 const TemplateArgumentLocInfo &Arg,
2766 RecordData &Record) {
2767 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002768 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002769 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002770 break;
2771 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002772 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002773 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002774 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002775 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2776 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002777 break;
John McCall0ad16662009-10-29 08:12:44 +00002778 case TemplateArgument::Null:
2779 case TemplateArgument::Integral:
2780 case TemplateArgument::Declaration:
2781 case TemplateArgument::Pack:
2782 break;
2783 }
2784}
2785
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002786void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002787 RecordData &Record) {
2788 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002789
2790 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2791 bool InfoHasSameExpr
2792 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2793 Record.push_back(InfoHasSameExpr);
2794 if (InfoHasSameExpr)
2795 return; // Avoid storing the same expr twice.
2796 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002797 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2798 Record);
2799}
2800
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002801void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002802 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002803 AddTypeRef(QualType(), Record);
2804 return;
2805 }
2806
John McCallbcd03502009-12-07 02:54:59 +00002807 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002808 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002809 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002810 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002811}
2812
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002813void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002814 Record.push_back(GetOrCreateTypeID(T));
2815}
2816
2817TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002818 return MakeTypeID(T,
2819 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2820}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002821
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002822TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002823 return MakeTypeID(T,
2824 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002825}
2826
2827TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2828 if (T.isNull())
2829 return TypeIdx();
2830 assert(!T.getLocalFastQualifiers());
2831
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002832 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002833 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002834 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002835 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002836 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002837 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002838 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002839 return Idx;
2840}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002841
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002842TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002843 if (T.isNull())
2844 return TypeIdx();
2845 assert(!T.getLocalFastQualifiers());
2846
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002847 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2848 assert(I != TypeIdxs.end() && "Type not emitted!");
2849 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002850}
2851
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002852void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002853 Record.push_back(GetDeclRef(D));
2854}
2855
Sebastian Redl539c5062010-08-18 23:57:32 +00002856DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002857 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002858 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002859 }
2860
Sebastian Redl539c5062010-08-18 23:57:32 +00002861 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002862 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002863 // We haven't seen this declaration before. Give it a new ID and
2864 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002865 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002866 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002867 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2868 // We don't add it to the replacement collection here, because we don't
2869 // have the offset yet.
2870 DeclTypesToEmit.push(const_cast<Decl *>(D));
2871 // Reset the flag, so that we don't add this decl multiple times.
2872 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002873 }
2874
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002875 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002876}
2877
Sebastian Redl539c5062010-08-18 23:57:32 +00002878DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002879 if (D == 0)
2880 return 0;
2881
2882 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2883 return DeclIDs[D];
2884}
2885
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002886void ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002887 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002888 Record.push_back(Name.getNameKind());
2889 switch (Name.getNameKind()) {
2890 case DeclarationName::Identifier:
2891 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2892 break;
2893
2894 case DeclarationName::ObjCZeroArgSelector:
2895 case DeclarationName::ObjCOneArgSelector:
2896 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002897 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002898 break;
2899
2900 case DeclarationName::CXXConstructorName:
2901 case DeclarationName::CXXDestructorName:
2902 case DeclarationName::CXXConversionFunctionName:
2903 AddTypeRef(Name.getCXXNameType(), Record);
2904 break;
2905
2906 case DeclarationName::CXXOperatorName:
2907 Record.push_back(Name.getCXXOverloadedOperator());
2908 break;
2909
Alexis Hunt3d221f22009-11-29 07:34:05 +00002910 case DeclarationName::CXXLiteralOperatorName:
2911 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2912 break;
2913
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002914 case DeclarationName::CXXUsingDirective:
2915 // No extra data to emit
2916 break;
2917 }
2918}
Chris Lattnerca025db2010-05-07 21:43:38 +00002919
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002920void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Chris Lattnerca025db2010-05-07 21:43:38 +00002921 RecordData &Record) {
2922 // Nested name specifiers usually aren't too long. I think that 8 would
2923 // typically accomodate the vast majority.
2924 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2925
2926 // Push each of the NNS's onto a stack for serialization in reverse order.
2927 while (NNS) {
2928 NestedNames.push_back(NNS);
2929 NNS = NNS->getPrefix();
2930 }
2931
2932 Record.push_back(NestedNames.size());
2933 while(!NestedNames.empty()) {
2934 NNS = NestedNames.pop_back_val();
2935 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2936 Record.push_back(Kind);
2937 switch (Kind) {
2938 case NestedNameSpecifier::Identifier:
2939 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2940 break;
2941
2942 case NestedNameSpecifier::Namespace:
2943 AddDeclRef(NNS->getAsNamespace(), Record);
2944 break;
2945
2946 case NestedNameSpecifier::TypeSpec:
2947 case NestedNameSpecifier::TypeSpecWithTemplate:
2948 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2949 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2950 break;
2951
2952 case NestedNameSpecifier::Global:
2953 // Don't need to write an associated value.
2954 break;
2955 }
2956 }
2957}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002958
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002959void ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002960 TemplateName::NameKind Kind = Name.getKind();
2961 Record.push_back(Kind);
2962 switch (Kind) {
2963 case TemplateName::Template:
2964 AddDeclRef(Name.getAsTemplateDecl(), Record);
2965 break;
2966
2967 case TemplateName::OverloadedTemplate: {
2968 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2969 Record.push_back(OvT->size());
2970 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2971 I != E; ++I)
2972 AddDeclRef(*I, Record);
2973 break;
2974 }
2975
2976 case TemplateName::QualifiedTemplate: {
2977 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2978 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2979 Record.push_back(QualT->hasTemplateKeyword());
2980 AddDeclRef(QualT->getTemplateDecl(), Record);
2981 break;
2982 }
2983
2984 case TemplateName::DependentTemplate: {
2985 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2986 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2987 Record.push_back(DepT->isIdentifier());
2988 if (DepT->isIdentifier())
2989 AddIdentifierRef(DepT->getIdentifier(), Record);
2990 else
2991 Record.push_back(DepT->getOperator());
2992 break;
2993 }
2994 }
2995}
2996
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002997void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002998 RecordData &Record) {
2999 Record.push_back(Arg.getKind());
3000 switch (Arg.getKind()) {
3001 case TemplateArgument::Null:
3002 break;
3003 case TemplateArgument::Type:
3004 AddTypeRef(Arg.getAsType(), Record);
3005 break;
3006 case TemplateArgument::Declaration:
3007 AddDeclRef(Arg.getAsDecl(), Record);
3008 break;
3009 case TemplateArgument::Integral:
3010 AddAPSInt(*Arg.getAsIntegral(), Record);
3011 AddTypeRef(Arg.getIntegralType(), Record);
3012 break;
3013 case TemplateArgument::Template:
3014 AddTemplateName(Arg.getAsTemplate(), Record);
3015 break;
3016 case TemplateArgument::Expression:
3017 AddStmt(Arg.getAsExpr());
3018 break;
3019 case TemplateArgument::Pack:
3020 Record.push_back(Arg.pack_size());
3021 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3022 I != E; ++I)
3023 AddTemplateArgument(*I, Record);
3024 break;
3025 }
3026}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003027
3028void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003029ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003030 RecordData &Record) {
3031 assert(TemplateParams && "No TemplateParams!");
3032 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3033 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3034 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3035 Record.push_back(TemplateParams->size());
3036 for (TemplateParameterList::const_iterator
3037 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3038 P != PEnd; ++P)
3039 AddDeclRef(*P, Record);
3040}
3041
3042/// \brief Emit a template argument list.
3043void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003044ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003045 RecordData &Record) {
3046 assert(TemplateArgs && "No TemplateArgs!");
3047 Record.push_back(TemplateArgs->flat_size());
3048 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
3049 AddTemplateArgument(TemplateArgs->get(i), Record);
3050}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003051
3052
3053void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003054ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003055 Record.push_back(Set.size());
3056 for (UnresolvedSetImpl::const_iterator
3057 I = Set.begin(), E = Set.end(); I != E; ++I) {
3058 AddDeclRef(I.getDecl(), Record);
3059 Record.push_back(I.getAccess());
3060 }
3061}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003062
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003063void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003064 RecordData &Record) {
3065 Record.push_back(Base.isVirtual());
3066 Record.push_back(Base.isBaseOfClass());
3067 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003068 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003069 AddSourceRange(Base.getSourceRange(), Record);
3070}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003071
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003072void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003073 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
3074 unsigned NumBaseOrMembers, RecordData &Record) {
3075 Record.push_back(NumBaseOrMembers);
3076 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3077 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3078
3079 Record.push_back(Init->isBaseInitializer());
3080 if (Init->isBaseInitializer()) {
3081 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3082 Record.push_back(Init->isBaseVirtual());
3083 } else {
3084 AddDeclRef(Init->getMember(), Record);
3085 }
3086 AddSourceLocation(Init->getMemberLocation(), Record);
3087 AddStmt(Init->getInit());
3088 AddDeclRef(Init->getAnonUnionMember(), Record);
3089 AddSourceLocation(Init->getLParenLoc(), Record);
3090 AddSourceLocation(Init->getRParenLoc(), Record);
3091 Record.push_back(Init->isWritten());
3092 if (Init->isWritten()) {
3093 Record.push_back(Init->getSourceOrder());
3094 } else {
3095 Record.push_back(Init->getNumArrayIndices());
3096 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3097 AddDeclRef(Init->getArrayIndex(i), Record);
3098 }
3099 }
3100}
3101
Sebastian Redl2c499f62010-08-18 23:56:43 +00003102void ASTWriter::SetReader(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003103 assert(Reader && "Cannot remove chain");
3104 assert(FirstDeclID == NextDeclID &&
3105 FirstTypeID == NextTypeID &&
3106 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003107 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003108 "Setting chain after writing has started.");
3109 Chain = Reader;
3110}
3111
Sebastian Redl539c5062010-08-18 23:57:32 +00003112void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003113 IdentifierIDs[II] = ID;
3114}
3115
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003116void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003117 TypeIdxs[T] = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003118}
3119
Sebastian Redl539c5062010-08-18 23:57:32 +00003120void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003121 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003122}
Sebastian Redl834bb972010-08-04 17:20:04 +00003123
Sebastian Redl539c5062010-08-18 23:57:32 +00003124void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003125 SelectorIDs[S] = ID;
3126}