blob: 8dffe1a9501885e496f90d97dff424b67a5d1065 [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);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000415 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000416 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
417 Writer.AddDeclRef(TL.getArg(i), Record);
418}
419void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
420 VisitFunctionTypeLoc(TL);
421}
422void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
423 VisitFunctionTypeLoc(TL);
424}
John McCallb96ec562009-12-04 22:46:56 +0000425void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
427}
John McCall17001972009-10-18 01:05:36 +0000428void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getNameLoc(), Record);
430}
431void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000432 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
433 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
434 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000435}
436void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000437 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
438 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
439 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
440 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000441}
442void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
445void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getNameLoc(), Record);
447}
448void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
449 Writer.AddSourceLocation(TL.getNameLoc(), Record);
450}
John McCall17001972009-10-18 01:05:36 +0000451void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
452 Writer.AddSourceLocation(TL.getNameLoc(), Record);
453}
John McCallcebee162009-10-18 09:09:24 +0000454void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
455 SubstTemplateTypeParmTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getNameLoc(), Record);
457}
John McCall17001972009-10-18 01:05:36 +0000458void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
459 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000460 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
461 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
462 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
463 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000464 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
465 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000466}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000467void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000468 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
469 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000470}
John McCalle78aac42010-03-10 03:28:59 +0000471void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000474void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000475 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
476 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
John McCallc392f372010-06-11 00:33:02 +0000479void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
480 DependentTemplateSpecializationTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
482 Writer.AddSourceRange(TL.getQualifierRange(), Record);
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
484 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
485 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
486 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000487 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
488 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000489}
John McCall17001972009-10-18 01:05:36 +0000490void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000492}
493void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
494 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000495 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
496 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
497 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
498 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000499}
John McCallfc93cf92009-10-22 22:37:11 +0000500void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000502}
John McCall8f115c62009-10-16 21:56:05 +0000503
Chris Lattner19cea4e2009-04-22 05:57:30 +0000504//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000505// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000506//===----------------------------------------------------------------------===//
507
Chris Lattner28fa4e62009-04-26 22:26:21 +0000508static void EmitBlockID(unsigned ID, const char *Name,
509 llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000510 ASTWriter::RecordData &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000511 Record.clear();
512 Record.push_back(ID);
513 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
514
515 // Emit the block name if present.
516 if (Name == 0 || Name[0] == 0) return;
517 Record.clear();
518 while (*Name)
519 Record.push_back(*Name++);
520 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
521}
522
523static void EmitRecordID(unsigned ID, const char *Name,
524 llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000525 ASTWriter::RecordData &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000526 Record.clear();
527 Record.push_back(ID);
528 while (*Name)
529 Record.push_back(*Name++);
530 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000531}
532
533static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000534 ASTWriter::RecordData &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000535#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000536 RECORD(STMT_STOP);
537 RECORD(STMT_NULL_PTR);
538 RECORD(STMT_NULL);
539 RECORD(STMT_COMPOUND);
540 RECORD(STMT_CASE);
541 RECORD(STMT_DEFAULT);
542 RECORD(STMT_LABEL);
543 RECORD(STMT_IF);
544 RECORD(STMT_SWITCH);
545 RECORD(STMT_WHILE);
546 RECORD(STMT_DO);
547 RECORD(STMT_FOR);
548 RECORD(STMT_GOTO);
549 RECORD(STMT_INDIRECT_GOTO);
550 RECORD(STMT_CONTINUE);
551 RECORD(STMT_BREAK);
552 RECORD(STMT_RETURN);
553 RECORD(STMT_DECL);
554 RECORD(STMT_ASM);
555 RECORD(EXPR_PREDEFINED);
556 RECORD(EXPR_DECL_REF);
557 RECORD(EXPR_INTEGER_LITERAL);
558 RECORD(EXPR_FLOATING_LITERAL);
559 RECORD(EXPR_IMAGINARY_LITERAL);
560 RECORD(EXPR_STRING_LITERAL);
561 RECORD(EXPR_CHARACTER_LITERAL);
562 RECORD(EXPR_PAREN);
563 RECORD(EXPR_UNARY_OPERATOR);
564 RECORD(EXPR_SIZEOF_ALIGN_OF);
565 RECORD(EXPR_ARRAY_SUBSCRIPT);
566 RECORD(EXPR_CALL);
567 RECORD(EXPR_MEMBER);
568 RECORD(EXPR_BINARY_OPERATOR);
569 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
570 RECORD(EXPR_CONDITIONAL_OPERATOR);
571 RECORD(EXPR_IMPLICIT_CAST);
572 RECORD(EXPR_CSTYLE_CAST);
573 RECORD(EXPR_COMPOUND_LITERAL);
574 RECORD(EXPR_EXT_VECTOR_ELEMENT);
575 RECORD(EXPR_INIT_LIST);
576 RECORD(EXPR_DESIGNATED_INIT);
577 RECORD(EXPR_IMPLICIT_VALUE_INIT);
578 RECORD(EXPR_VA_ARG);
579 RECORD(EXPR_ADDR_LABEL);
580 RECORD(EXPR_STMT);
581 RECORD(EXPR_TYPES_COMPATIBLE);
582 RECORD(EXPR_CHOOSE);
583 RECORD(EXPR_GNU_NULL);
584 RECORD(EXPR_SHUFFLE_VECTOR);
585 RECORD(EXPR_BLOCK);
586 RECORD(EXPR_BLOCK_DECL_REF);
587 RECORD(EXPR_OBJC_STRING_LITERAL);
588 RECORD(EXPR_OBJC_ENCODE);
589 RECORD(EXPR_OBJC_SELECTOR_EXPR);
590 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
591 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
592 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
593 RECORD(EXPR_OBJC_KVC_REF_EXPR);
594 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000595 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 Gregor91096292010-10-02 19:29:26 +00001362
1363 // Don't write the macro definition if it is from another AST file.
1364 if (ID < FirstMacroID)
1365 continue;
1366
1367 unsigned Position = ID - FirstMacroID;
1368 if (Position != MacroDefinitionOffsets.size()) {
1369 if (Position > MacroDefinitionOffsets.size())
1370 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregoraae92242010-03-19 21:51:54 +00001371
Douglas Gregor91096292010-10-02 19:29:26 +00001372 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001373 } else
1374 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1375
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001376 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001377 Record.push_back(ID);
1378 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1379 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1380 AddIdentifierRef(MD->getName(), Record);
1381 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001382 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001383 continue;
1384 }
1385 }
1386 }
1387
Douglas Gregor8f45df52009-04-16 22:23:12 +00001388 Stream.ExitBlock();
Douglas Gregoraae92242010-03-19 21:51:54 +00001389
1390 // Write the offsets table for the preprocessing record.
1391 if (NumPreprocessingRecords > 0) {
1392 // Write the offsets table for identifier IDs.
1393 using namespace llvm;
1394 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001395 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1399 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1400
1401 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001402 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001403 Record.push_back(NumPreprocessingRecords);
1404 Record.push_back(MacroDefinitionOffsets.size());
1405 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001406 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001407 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1408 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001409}
1410
Douglas Gregorc5046832009-04-27 18:38:38 +00001411//===----------------------------------------------------------------------===//
1412// Type Serialization
1413//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001414
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001415/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001416void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001417 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001418 if (Idx.getIndex() == 0) // we haven't seen this type before.
1419 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001420
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001421 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001422
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001423 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001424 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001425 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001426 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001427 else if (TypeOffsets.size() < Index) {
1428 TypeOffsets.resize(Index + 1);
1429 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001430 }
1431
1432 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001433
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001434 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001435 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001436
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001437 if (T.hasLocalNonFastQualifiers()) {
1438 Qualifiers Qs = T.getLocalQualifiers();
1439 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001440 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001441 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001442 } else {
1443 switch (T->getTypeClass()) {
1444 // For all of the concrete, non-dependent types, call the
1445 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001446#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001447 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001448#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001449#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001450 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001451 }
1452
1453 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001454 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001455
1456 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001457 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001458}
1459
Douglas Gregorc5046832009-04-27 18:38:38 +00001460//===----------------------------------------------------------------------===//
1461// Declaration Serialization
1462//===----------------------------------------------------------------------===//
1463
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001464/// \brief Write the block containing all of the declaration IDs
1465/// lexically declared within the given DeclContext.
1466///
1467/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1468/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001469uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001470 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001471 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001472 return 0;
1473
Douglas Gregor8f45df52009-04-16 22:23:12 +00001474 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001475 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001476 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001477 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001478 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1479 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001480 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001481
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001482 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001483 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001484 reinterpret_cast<char*>(Decls.data()),
1485 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001486 return Offset;
1487}
1488
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001489void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001490 using namespace llvm;
1491 RecordData Record;
1492
1493 // Write the type offsets array
1494 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001495 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1497 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1498 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1499 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001500 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001501 Record.push_back(TypeOffsets.size());
1502 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001503 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001504 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1505
1506 // Write the declaration offsets array
1507 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001508 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1511 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1512 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001513 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001514 Record.push_back(DeclOffsets.size());
1515 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001516 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001517 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1518}
1519
Douglas Gregorc5046832009-04-27 18:38:38 +00001520//===----------------------------------------------------------------------===//
1521// Global Method Pool and Selector Serialization
1522//===----------------------------------------------------------------------===//
1523
Douglas Gregore84a9da2009-04-20 20:36:09 +00001524namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001525// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001526class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001527 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001528
1529public:
1530 typedef Selector key_type;
1531 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001532
Sebastian Redl834bb972010-08-04 17:20:04 +00001533 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001534 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001535 ObjCMethodList Instance, Factory;
1536 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001537 typedef const data_type& data_type_ref;
1538
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001539 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001540
Douglas Gregorc78d3462009-04-24 21:10:55 +00001541 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001542 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001543 }
Mike Stump11289f42009-09-09 15:08:12 +00001544
1545 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001546 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1547 data_type_ref Methods) {
1548 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1549 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001550 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1551 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001552 Method = Method->Next)
1553 if (Method->Method)
1554 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001555 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001556 Method = Method->Next)
1557 if (Method->Method)
1558 DataLen += 4;
1559 clang::io::Emit16(Out, DataLen);
1560 return std::make_pair(KeyLen, DataLen);
1561 }
Mike Stump11289f42009-09-09 15:08:12 +00001562
Douglas Gregor95c13f52009-04-25 17:48:32 +00001563 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001564 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001565 assert((Start >> 32) == 0 && "Selector key offset too large");
1566 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001567 unsigned N = Sel.getNumArgs();
1568 clang::io::Emit16(Out, N);
1569 if (N == 0)
1570 N = 1;
1571 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001572 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001573 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1574 }
Mike Stump11289f42009-09-09 15:08:12 +00001575
Douglas Gregorc78d3462009-04-24 21:10:55 +00001576 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001577 data_type_ref Methods, unsigned DataLen) {
1578 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001579 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001580 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001581 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001582 Method = Method->Next)
1583 if (Method->Method)
1584 ++NumInstanceMethods;
1585
1586 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001587 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001588 Method = Method->Next)
1589 if (Method->Method)
1590 ++NumFactoryMethods;
1591
1592 clang::io::Emit16(Out, NumInstanceMethods);
1593 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001594 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001595 Method = Method->Next)
1596 if (Method->Method)
1597 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001598 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001599 Method = Method->Next)
1600 if (Method->Method)
1601 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001602
1603 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001604 }
1605};
1606} // end anonymous namespace
1607
Sebastian Redla19a67f2010-08-03 21:58:15 +00001608/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001609///
1610/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001611/// in an on-disk hash table indexed by the selector. The hash table also
1612/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001613void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001614 using namespace llvm;
1615
Sebastian Redla19a67f2010-08-03 21:58:15 +00001616 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001617 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001618 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001619 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001620 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001621 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001622 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001623 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001624
Sebastian Redla19a67f2010-08-03 21:58:15 +00001625 // Create the on-disk hash table representation. We walk through every
1626 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001627 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001628 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001629 I = SelectorIDs.begin(), E = SelectorIDs.end();
1630 I != E; ++I) {
1631 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001632 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001633 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001634 I->second,
1635 ObjCMethodList(),
1636 ObjCMethodList()
1637 };
1638 if (F != SemaRef.MethodPool.end()) {
1639 Data.Instance = F->second.first;
1640 Data.Factory = F->second.second;
1641 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001642 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001643 // changed.
1644 if (Chain && I->second < FirstSelectorID) {
1645 // Selector already exists. Did it change?
1646 bool changed = false;
1647 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1648 M = M->Next) {
1649 if (M->Method->getPCHLevel() == 0)
1650 changed = true;
1651 }
1652 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1653 M = M->Next) {
1654 if (M->Method->getPCHLevel() == 0)
1655 changed = true;
1656 }
1657 if (!changed)
1658 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001659 } else if (Data.Instance.Method || Data.Factory.Method) {
1660 // A new method pool entry.
1661 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001662 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001663 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001664 }
1665
Douglas Gregorc78d3462009-04-24 21:10:55 +00001666 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001667 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001668 uint32_t BucketOffset;
1669 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001670 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001671 llvm::raw_svector_ostream Out(MethodPool);
1672 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001673 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001674 BucketOffset = Generator.Emit(Out, Trait);
1675 }
1676
1677 // Create a blob abbreviation
1678 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001679 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001680 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001681 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001682 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1683 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1684
Douglas Gregor95c13f52009-04-25 17:48:32 +00001685 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001686 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001687 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001688 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001689 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001690 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001691
1692 // Create a blob abbreviation for the selector table offsets.
1693 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001694 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001695 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1696 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1697 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1698
1699 // Write the selector offsets table.
1700 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001701 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001702 Record.push_back(SelectorOffsets.size());
1703 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001704 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001705 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001706 }
1707}
1708
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001709/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001710void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001711 using namespace llvm;
1712 if (SemaRef.ReferencedSelectors.empty())
1713 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001714
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001715 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001716
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001717 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001718 // very tricky to fix, and given that @selector shouldn't really appear in
1719 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001720 for (DenseMap<Selector, SourceLocation>::iterator S =
1721 SemaRef.ReferencedSelectors.begin(),
1722 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1723 Selector Sel = (*S).first;
1724 SourceLocation Loc = (*S).second;
1725 AddSelectorRef(Sel, Record);
1726 AddSourceLocation(Loc, Record);
1727 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001728 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001729}
1730
Douglas Gregorc5046832009-04-27 18:38:38 +00001731//===----------------------------------------------------------------------===//
1732// Identifier Table Serialization
1733//===----------------------------------------------------------------------===//
1734
Douglas Gregorc78d3462009-04-24 21:10:55 +00001735namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001736class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001737 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001738 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001739
Douglas Gregor1d583f22009-04-28 21:18:29 +00001740 /// \brief Determines whether this is an "interesting" identifier
1741 /// that needs a full IdentifierInfo structure written into the hash
1742 /// table.
1743 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1744 return II->isPoisoned() ||
1745 II->isExtensionToken() ||
1746 II->hasMacroDefinition() ||
1747 II->getObjCOrBuiltinID() ||
1748 II->getFETokenInfo<void>();
1749 }
1750
Douglas Gregore84a9da2009-04-20 20:36:09 +00001751public:
1752 typedef const IdentifierInfo* key_type;
1753 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001754
Sebastian Redl539c5062010-08-18 23:57:32 +00001755 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001756 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001757
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001758 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001759 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001760
1761 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001762 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001763 }
Mike Stump11289f42009-09-09 15:08:12 +00001764
1765 std::pair<unsigned,unsigned>
1766 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001767 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001768 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001769 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1770 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001771 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001772 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001773 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001774 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001775 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1776 DEnd = IdentifierResolver::end();
1777 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001778 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001779 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001780 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001781 // We emit the key length after the data length so that every
1782 // string is preceded by a 16-bit length. This matches the PTH
1783 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001784 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001785 return std::make_pair(KeyLen, DataLen);
1786 }
Mike Stump11289f42009-09-09 15:08:12 +00001787
1788 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001789 unsigned KeyLen) {
1790 // Record the location of the key data. This is used when generating
1791 // the mapping from persistent IDs to strings.
1792 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001793 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001794 }
Mike Stump11289f42009-09-09 15:08:12 +00001795
1796 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001797 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001798 if (!isInterestingIdentifier(II)) {
1799 clang::io::Emit32(Out, ID << 1);
1800 return;
1801 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001802
Douglas Gregor1d583f22009-04-28 21:18:29 +00001803 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001804 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001805 bool hasMacroDefinition =
1806 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001807 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001808 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001809 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1810 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1811 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001812 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001813 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001814 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001815
Douglas Gregorc3366a52009-04-21 23:56:24 +00001816 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001817 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001818
Douglas Gregora868bbd2009-04-21 22:25:48 +00001819 // Emit the declaration IDs in reverse order, because the
1820 // IdentifierResolver provides the declarations as they would be
1821 // visible (e.g., the function "stat" would come before the struct
1822 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1823 // adds declarations to the end of the list (so we need to see the
1824 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001825 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001826 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001827 IdentifierResolver::end());
1828 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1829 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001830 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001831 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001832 }
1833};
1834} // end anonymous namespace
1835
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001836/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001837///
1838/// The identifier table consists of a blob containing string data
1839/// (the actual identifiers themselves) and a separate "offsets" index
1840/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001841void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001842 using namespace llvm;
1843
1844 // Create and write out the blob that contains the identifier
1845 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001846 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001847 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001848 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001849
Douglas Gregore6648fb2009-04-28 20:33:11 +00001850 // Look for any identifiers that were named while processing the
1851 // headers, but are otherwise not needed. We add these to the hash
1852 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001853 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001854 // file.
1855 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1856 IDEnd = PP.getIdentifierTable().end();
1857 ID != IDEnd; ++ID)
1858 getIdentifierRef(ID->second);
1859
Sebastian Redlff4a2952010-07-23 23:49:55 +00001860 // Create the on-disk hash table representation. We only store offsets
1861 // for identifiers that appear here for the first time.
1862 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001863 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001864 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1865 ID != IDEnd; ++ID) {
1866 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001867 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001868 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001869 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001870
Douglas Gregore84a9da2009-04-20 20:36:09 +00001871 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001872 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001873 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001874 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001875 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001876 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001877 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001878 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001879 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001880 }
1881
1882 // Create a blob abbreviation
1883 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001884 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001887 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001888
1889 // Write the identifier table
1890 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001891 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001892 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001893 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001894 }
1895
1896 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001897 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001898 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1901 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1902
1903 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001904 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001905 Record.push_back(IdentifierOffsets.size());
1906 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001907 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001908 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001909}
1910
Douglas Gregorc5046832009-04-27 18:38:38 +00001911//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001912// DeclContext's Name Lookup Table Serialization
1913//===----------------------------------------------------------------------===//
1914
1915namespace {
1916// Trait used for the on-disk hash table used in the method pool.
1917class ASTDeclContextNameLookupTrait {
1918 ASTWriter &Writer;
1919
1920public:
1921 typedef DeclarationName key_type;
1922 typedef key_type key_type_ref;
1923
1924 typedef DeclContext::lookup_result data_type;
1925 typedef const data_type& data_type_ref;
1926
1927 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1928
1929 unsigned ComputeHash(DeclarationName Name) {
1930 llvm::FoldingSetNodeID ID;
1931 ID.AddInteger(Name.getNameKind());
1932
1933 switch (Name.getNameKind()) {
1934 case DeclarationName::Identifier:
1935 ID.AddString(Name.getAsIdentifierInfo()->getName());
1936 break;
1937 case DeclarationName::ObjCZeroArgSelector:
1938 case DeclarationName::ObjCOneArgSelector:
1939 case DeclarationName::ObjCMultiArgSelector:
1940 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1941 break;
1942 case DeclarationName::CXXConstructorName:
1943 case DeclarationName::CXXDestructorName:
1944 case DeclarationName::CXXConversionFunctionName:
1945 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1946 break;
1947 case DeclarationName::CXXOperatorName:
1948 ID.AddInteger(Name.getCXXOverloadedOperator());
1949 break;
1950 case DeclarationName::CXXLiteralOperatorName:
1951 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1952 case DeclarationName::CXXUsingDirective:
1953 break;
1954 }
1955
1956 return ID.ComputeHash();
1957 }
1958
1959 std::pair<unsigned,unsigned>
1960 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
1961 data_type_ref Lookup) {
1962 unsigned KeyLen = 1;
1963 switch (Name.getNameKind()) {
1964 case DeclarationName::Identifier:
1965 case DeclarationName::ObjCZeroArgSelector:
1966 case DeclarationName::ObjCOneArgSelector:
1967 case DeclarationName::ObjCMultiArgSelector:
1968 case DeclarationName::CXXConstructorName:
1969 case DeclarationName::CXXDestructorName:
1970 case DeclarationName::CXXConversionFunctionName:
1971 case DeclarationName::CXXLiteralOperatorName:
1972 KeyLen += 4;
1973 break;
1974 case DeclarationName::CXXOperatorName:
1975 KeyLen += 1;
1976 break;
1977 case DeclarationName::CXXUsingDirective:
1978 break;
1979 }
1980 clang::io::Emit16(Out, KeyLen);
1981
1982 // 2 bytes for num of decls and 4 for each DeclID.
1983 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
1984 clang::io::Emit16(Out, DataLen);
1985
1986 return std::make_pair(KeyLen, DataLen);
1987 }
1988
1989 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
1990 using namespace clang::io;
1991
1992 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
1993 Emit8(Out, Name.getNameKind());
1994 switch (Name.getNameKind()) {
1995 case DeclarationName::Identifier:
1996 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
1997 break;
1998 case DeclarationName::ObjCZeroArgSelector:
1999 case DeclarationName::ObjCOneArgSelector:
2000 case DeclarationName::ObjCMultiArgSelector:
2001 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2002 break;
2003 case DeclarationName::CXXConstructorName:
2004 case DeclarationName::CXXDestructorName:
2005 case DeclarationName::CXXConversionFunctionName:
2006 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2007 break;
2008 case DeclarationName::CXXOperatorName:
2009 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2010 Emit8(Out, Name.getCXXOverloadedOperator());
2011 break;
2012 case DeclarationName::CXXLiteralOperatorName:
2013 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2014 break;
2015 case DeclarationName::CXXUsingDirective:
2016 break;
2017 }
2018 }
2019
2020 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2021 data_type Lookup, unsigned DataLen) {
2022 uint64_t Start = Out.tell(); (void)Start;
2023 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2024 for (; Lookup.first != Lookup.second; ++Lookup.first)
2025 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2026
2027 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2028 }
2029};
2030} // end anonymous namespace
2031
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002032/// \brief Write the block containing all of the declaration IDs
2033/// visible from the given DeclContext.
2034///
2035/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002036/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002037uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2038 DeclContext *DC) {
2039 if (DC->getPrimaryContext() != DC)
2040 return 0;
2041
2042 // Since there is no name lookup into functions or methods, don't bother to
2043 // build a visible-declarations table for these entities.
2044 if (DC->isFunctionOrMethod())
2045 return 0;
2046
2047 // If not in C++, we perform name lookup for the translation unit via the
2048 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2049 // FIXME: In C++ we need the visible declarations in order to "see" the
2050 // friend declarations, is there a way to do this without writing the table ?
2051 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2052 return 0;
2053
2054 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002055 if (DC->hasExternalVisibleStorage())
2056 DC->MaterializeVisibleDeclsFromExternalStorage();
2057 else
2058 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002059
2060 // Serialize the contents of the mapping used for lookup. Note that,
2061 // although we have two very different code paths, the serialized
2062 // representation is the same for both cases: a declaration name,
2063 // followed by a size, followed by references to the visible
2064 // declarations that have that name.
2065 uint64_t Offset = Stream.GetCurrentBitNo();
2066 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2067 if (!Map || Map->empty())
2068 return 0;
2069
2070 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2071 ASTDeclContextNameLookupTrait Trait(*this);
2072
2073 // Create the on-disk hash table representation.
2074 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2075 D != DEnd; ++D) {
2076 DeclarationName Name = D->first;
2077 DeclContext::lookup_result Result = D->second.getLookupResult();
2078 Generator.insert(Name, Result, Trait);
2079 }
2080
2081 // Create the on-disk hash table in a buffer.
2082 llvm::SmallString<4096> LookupTable;
2083 uint32_t BucketOffset;
2084 {
2085 llvm::raw_svector_ostream Out(LookupTable);
2086 // Make sure that no bucket is at offset 0
2087 clang::io::Emit32(Out, 0);
2088 BucketOffset = Generator.Emit(Out, Trait);
2089 }
2090
2091 // Write the lookup table
2092 RecordData Record;
2093 Record.push_back(DECL_CONTEXT_VISIBLE);
2094 Record.push_back(BucketOffset);
2095 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2096 LookupTable.str());
2097
2098 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2099 ++NumVisibleDeclContexts;
2100 return Offset;
2101}
2102
Sebastian Redla4071b42010-08-24 00:50:09 +00002103/// \brief Write an UPDATE_VISIBLE block for the given context.
2104///
2105/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2106/// DeclContext in a dependent AST file. As such, they only exist for the TU
2107/// (in C++) and for namespaces.
2108void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
2109 assert((DC->isTranslationUnit() || DC->isNamespace()) &&
2110 "Only TU and namespaces should have visible decl updates.");
2111
2112 // Make the context build its lookup table, but don't make it load external
2113 // decls.
2114 DC->lookup(DeclarationName());
2115
2116 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2117 if (!Map || Map->empty())
2118 return;
2119
2120 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2121 ASTDeclContextNameLookupTrait Trait(*this);
2122
2123 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002124 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2125 D != DEnd; ++D) {
2126 DeclarationName Name = D->first;
2127 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002128 // For any name that appears in this table, the results are complete, i.e.
2129 // they overwrite results from previous PCHs. Merging is always a mess.
2130 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002131 }
2132
2133 // Create the on-disk hash table in a buffer.
2134 llvm::SmallString<4096> LookupTable;
2135 uint32_t BucketOffset;
2136 {
2137 llvm::raw_svector_ostream Out(LookupTable);
2138 // Make sure that no bucket is at offset 0
2139 clang::io::Emit32(Out, 0);
2140 BucketOffset = Generator.Emit(Out, Trait);
2141 }
2142
2143 // Write the lookup table
2144 RecordData Record;
2145 Record.push_back(UPDATE_VISIBLE);
2146 Record.push_back(getDeclID(cast<Decl>(DC)));
2147 Record.push_back(BucketOffset);
2148 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2149}
2150
Sebastian Redl401b39a2010-08-24 22:50:24 +00002151/// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates
2152/// that have new specializations in the current AST file.
2153void ASTWriter::WriteAdditionalTemplateSpecializations() {
2154 RecordData Record;
2155 for (AdditionalTemplateSpecializationsMap::iterator
2156 I = AdditionalTemplateSpecializations.begin(),
2157 E = AdditionalTemplateSpecializations.end();
2158 I != E; ++I) {
2159 Record.clear();
2160 Record.push_back(I->first);
2161 Record.insert(Record.end(), I->second.begin(), I->second.end());
2162 Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record);
2163 }
2164}
2165
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002166//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002167// General Serialization Routines
2168//===----------------------------------------------------------------------===//
2169
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002170/// \brief Write a record containing the given attributes.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002171void ASTWriter::WriteAttributeRecord(const AttrVec &Attrs) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002172 RecordData Record;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002173 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2174 const Attr * A = *i;
2175 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2176 AddSourceLocation(A->getLocation(), Record);
2177 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002178
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002179#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002180
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002181 }
2182
Sebastian Redl539c5062010-08-18 23:57:32 +00002183 Stream.EmitRecord(DECL_ATTR, Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002184}
2185
Benjamin Kramercd495032010-09-02 15:06:24 +00002186void ASTWriter::AddString(llvm::StringRef Str, RecordData &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002187 Record.push_back(Str.size());
2188 Record.insert(Record.end(), Str.begin(), Str.end());
2189}
2190
Douglas Gregore84a9da2009-04-20 20:36:09 +00002191/// \brief Note that the identifier II occurs at the given offset
2192/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002193void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002194 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002195 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002196 // up earlier in the chain and thus don't need an offset.
2197 if (ID >= FirstIdentID)
2198 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002199}
2200
Douglas Gregor95c13f52009-04-25 17:48:32 +00002201/// \brief Note that the selector Sel occurs at the given offset
2202/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002203void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002204 unsigned ID = SelectorIDs[Sel];
2205 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002206 // Don't record offsets for selectors that are also available in a different
2207 // file.
2208 if (ID < FirstSelectorID)
2209 return;
2210 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002211}
2212
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002213ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redld95a56e2010-08-04 18:21:41 +00002214 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002216 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002217 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2218 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002219 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2220 NumVisibleDeclContexts(0) {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002221}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002222
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002223void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002224 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002225 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002226 Stream.Emit((unsigned)'C', 8);
2227 Stream.Emit((unsigned)'P', 8);
2228 Stream.Emit((unsigned)'C', 8);
2229 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002230
Chris Lattner28fa4e62009-04-26 22:26:21 +00002231 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002232
Sebastian Redl143413f2010-07-12 22:02:52 +00002233 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002234 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002235 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002236 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002237}
2238
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002239void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002240 const char *isysroot) {
2241 using namespace llvm;
2242
2243 ASTContext &Context = SemaRef.Context;
2244 Preprocessor &PP = SemaRef.PP;
2245
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002246 // The translation unit is the first declaration we'll emit.
2247 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002248 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002249 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002250
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002251 // Make sure that we emit IdentifierInfos (and any attached
2252 // declarations) for builtins.
2253 {
2254 IdentifierTable &Table = PP.getIdentifierTable();
2255 llvm::SmallVector<const char *, 32> BuiltinNames;
2256 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2257 Context.getLangOptions().NoBuiltin);
2258 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2259 getIdentifierRef(&Table.get(BuiltinNames[I]));
2260 }
2261
Chris Lattner0c797362009-09-08 18:19:27 +00002262 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002263 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002264 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002265 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002266 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2267 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002268 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002269
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002270 // Build a record containing all of the file scoped decls in this file.
2271 RecordData UnusedFileScopedDecls;
2272 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2273 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002274
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002275 RecordData WeakUndeclaredIdentifiers;
2276 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2277 WeakUndeclaredIdentifiers.push_back(
2278 SemaRef.WeakUndeclaredIdentifiers.size());
2279 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2280 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2281 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2282 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2283 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2284 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2285 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2286 }
2287 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002288
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002289 // Build a record containing all of the locally-scoped external
2290 // declarations in this header file. Generally, this record will be
2291 // empty.
2292 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002293 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002294 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002295 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002296 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2297 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2298 TD != TDEnd; ++TD)
2299 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2300
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002301 // Build a record containing all of the ext_vector declarations.
2302 RecordData ExtVectorDecls;
2303 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2304 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2305
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002306 // Build a record containing all of the VTable uses information.
2307 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002308 if (!SemaRef.VTableUses.empty()) {
2309 VTableUses.push_back(SemaRef.VTableUses.size());
2310 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2311 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2312 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2313 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2314 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002315 }
2316
2317 // Build a record containing all of dynamic classes declarations.
2318 RecordData DynamicClasses;
2319 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2320 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2321
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002322 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002323 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002324 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002325 I = SemaRef.PendingInstantiations.begin(),
2326 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2327 AddDeclRef(I->first, PendingInstantiations);
2328 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002329 }
2330 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2331 "There are local ones at end of translation unit!");
2332
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002333 // Build a record containing some declaration references.
2334 RecordData SemaDeclRefs;
2335 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2336 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2337 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2338 }
2339
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002340 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002341 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002342 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002343 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002344 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002345 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002346 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002347 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002348 // Write the record of special types.
2349 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002350
Steve Naroffc277ad12009-07-18 15:33:26 +00002351 AddTypeRef(Context.getBuiltinVaListType(), Record);
2352 AddTypeRef(Context.getObjCIdType(), Record);
2353 AddTypeRef(Context.getObjCSelType(), Record);
2354 AddTypeRef(Context.getObjCProtoType(), Record);
2355 AddTypeRef(Context.getObjCClassType(), Record);
2356 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2357 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2358 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002359 AddTypeRef(Context.getjmp_bufType(), Record);
2360 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002361 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2362 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002363 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002364 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002365 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2366 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002367 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002368 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002369
Douglas Gregor1970d882009-04-26 03:49:13 +00002370 // Keep writing types and declarations until all types and
2371 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002372 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002373 WriteDeclsBlockAbbrevs();
2374 while (!DeclTypesToEmit.empty()) {
2375 DeclOrType DOT = DeclTypesToEmit.front();
2376 DeclTypesToEmit.pop();
2377 if (DOT.isType())
2378 WriteType(DOT.getType());
2379 else
2380 WriteDecl(Context, DOT.getDecl());
2381 }
2382 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002383
Douglas Gregor45053152009-10-17 17:25:45 +00002384 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002385 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002386 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002387 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002388
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002389 WriteTypeDeclOffsets();
Douglas Gregor652d82a2009-04-18 05:55:16 +00002390
Douglas Gregord4df8652009-04-22 22:02:47 +00002391 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002392 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002393 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002394
2395 // Write the record containing tentative definitions.
2396 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002397 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002398
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002399 // Write the record containing unused file scoped decls.
2400 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002401 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002402
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002403 // Write the record containing weak undeclared identifiers.
2404 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002405 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002406 WeakUndeclaredIdentifiers);
2407
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002408 // Write the record containing locally-scoped external definitions.
2409 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002410 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002411 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002412
2413 // Write the record containing ext_vector type names.
2414 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002415 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002416
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002417 // Write the record containing VTable uses information.
2418 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002419 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002420
2421 // Write the record containing dynamic classes declarations.
2422 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002423 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002424
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002425 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002426 if (!PendingInstantiations.empty())
2427 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002428
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002429 // Write the record containing declaration references of Sema.
2430 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002431 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002432
Douglas Gregor08f01292009-04-17 22:13:46 +00002433 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002434 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002435 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002436 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002437 Record.push_back(NumLexicalDeclContexts);
2438 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002439 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002440 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002441}
2442
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002443void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002444 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002445 using namespace llvm;
2446
Sebastian Redl07a89a82010-07-30 00:29:29 +00002447 FirstDeclID += Chain->getTotalNumDecls();
2448 FirstTypeID += Chain->getTotalNumTypes();
2449 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redld95a56e2010-08-04 18:21:41 +00002450 FirstSelectorID += Chain->getTotalNumSelectors();
Douglas Gregor91096292010-10-02 19:29:26 +00002451 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Sebastian Redl07a89a82010-07-30 00:29:29 +00002452 NextDeclID = FirstDeclID;
2453 NextTypeID = FirstTypeID;
2454 NextIdentID = FirstIdentID;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002455 NextSelectorID = FirstSelectorID;
Douglas Gregor91096292010-10-02 19:29:26 +00002456 NextMacroID = FirstMacroID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00002457
Sebastian Redl143413f2010-07-12 22:02:52 +00002458 ASTContext &Context = SemaRef.Context;
2459 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002460
Sebastian Redl143413f2010-07-12 22:02:52 +00002461 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002462 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002463 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002464 if (StatCalls && !isysroot)
2465 WriteStatCache(*StatCalls);
2466 // FIXME: Source manager block should only write new stuff, which could be
2467 // done by tracking the largest ID in the chain
2468 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002469
2470 // The special types are in the chained PCH.
2471
2472 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002473 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002474 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002475 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002476 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2477 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002478 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002479 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002480 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002481 else if ((*I)->isChangedSinceDeserialization())
2482 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002483 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002484 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002485 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002486 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002487 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2488 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2489 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002490 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002491 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2492 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002493 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Sebastian Redla4071b42010-08-24 00:50:09 +00002494 // And in C++, a visible updates block for the TU.
2495 if (Context.getLangOptions().CPlusPlus) {
2496 Abv = new llvm::BitCodeAbbrev();
2497 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2498 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2499 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2500 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2501 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2502 WriteDeclContextVisibleUpdate(TU);
2503 }
Sebastian Redl143413f2010-07-12 22:02:52 +00002504
Sebastian Redl98912122010-07-27 23:01:28 +00002505 // Build a record containing all of the new tentative definitions in this
2506 // file, in TentativeDefinitions order.
2507 RecordData TentativeDefinitions;
2508 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2509 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2510 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2511 }
2512
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002513 // Build a record containing all of the file scoped decls in this file.
2514 RecordData UnusedFileScopedDecls;
2515 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2516 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2517 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002518 }
2519
Sebastian Redl08aca90252010-08-05 18:21:25 +00002520 // We write the entire table, overwriting the tables from the chain.
2521 RecordData WeakUndeclaredIdentifiers;
2522 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2523 WeakUndeclaredIdentifiers.push_back(
2524 SemaRef.WeakUndeclaredIdentifiers.size());
2525 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2526 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2527 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2528 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2529 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2530 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2531 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2532 }
2533 }
2534
Sebastian Redl98912122010-07-27 23:01:28 +00002535 // Build a record containing all of the locally-scoped external
2536 // declarations in this header file. Generally, this record will be
2537 // empty.
2538 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002539 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002540 // nondeterminstic!
2541 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2542 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2543 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2544 TD != TDEnd; ++TD) {
2545 if (TD->second->getPCHLevel() == 0)
2546 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2547 }
2548
2549 // Build a record containing all of the ext_vector declarations.
2550 RecordData ExtVectorDecls;
2551 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2552 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2553 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2554 }
2555
Sebastian Redl08aca90252010-08-05 18:21:25 +00002556 // Build a record containing all of the VTable uses information.
2557 // We write everything here, because it's too hard to determine whether
2558 // a use is new to this part.
2559 RecordData VTableUses;
2560 if (!SemaRef.VTableUses.empty()) {
2561 VTableUses.push_back(SemaRef.VTableUses.size());
2562 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2563 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2564 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2565 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2566 }
2567 }
2568
2569 // Build a record containing all of dynamic classes declarations.
2570 RecordData DynamicClasses;
2571 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2572 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2573 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2574
2575 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002576 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002577 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002578 I = SemaRef.PendingInstantiations.begin(),
2579 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002580 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002581 AddDeclRef(I->first, PendingInstantiations);
2582 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002583 }
2584 }
2585 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2586 "There are local ones at end of translation unit!");
2587
2588 // Build a record containing some declaration references.
2589 // It's not worth the effort to avoid duplication here.
2590 RecordData SemaDeclRefs;
2591 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2592 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2593 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2594 }
2595
Sebastian Redl539c5062010-08-18 23:57:32 +00002596 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002597 WriteDeclsBlockAbbrevs();
2598 while (!DeclTypesToEmit.empty()) {
2599 DeclOrType DOT = DeclTypesToEmit.front();
2600 DeclTypesToEmit.pop();
2601 if (DOT.isType())
2602 WriteType(DOT.getType());
2603 else
2604 WriteDecl(Context, DOT.getDecl());
2605 }
2606 Stream.ExitBlock();
2607
Sebastian Redl98912122010-07-27 23:01:28 +00002608 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002609 WriteSelectors(SemaRef);
2610 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002611 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002612 WriteTypeDeclOffsets();
Sebastian Redl98912122010-07-27 23:01:28 +00002613
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002614 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002615 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002616 RecordData FirstLatestDeclIDs;
2617 for (FirstLatestDeclMap::iterator
2618 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2619 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2620 "Expected first & second to be in different PCHs");
2621 AddDeclRef(I->first, FirstLatestDeclIDs);
2622 AddDeclRef(I->second, FirstLatestDeclIDs);
2623 }
2624 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002625 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002626
Sebastian Redl98912122010-07-27 23:01:28 +00002627 // Write the record containing external, unnamed definitions.
2628 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002629 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002630
2631 // Write the record containing tentative definitions.
2632 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002633 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002634
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002635 // Write the record containing unused file scoped decls.
2636 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002637 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002638
Sebastian Redl08aca90252010-08-05 18:21:25 +00002639 // Write the record containing weak undeclared identifiers.
2640 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002641 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002642 WeakUndeclaredIdentifiers);
2643
Sebastian Redl98912122010-07-27 23:01:28 +00002644 // Write the record containing locally-scoped external definitions.
2645 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002646 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002647 LocallyScopedExternalDecls);
2648
2649 // Write the record containing ext_vector type names.
2650 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002651 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002652
Sebastian Redl08aca90252010-08-05 18:21:25 +00002653 // Write the record containing VTable uses information.
2654 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002655 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002656
2657 // Write the record containing dynamic classes declarations.
2658 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002659 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002660
2661 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002662 if (!PendingInstantiations.empty())
2663 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002664
2665 // Write the record containing declaration references of Sema.
2666 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002667 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002668
Sebastian Redla4071b42010-08-24 00:50:09 +00002669 // Write the updates to C++ namespaces.
2670 for (llvm::SmallPtrSet<const NamespaceDecl *, 16>::iterator
2671 I = UpdatedNamespaces.begin(),
2672 E = UpdatedNamespaces.end();
2673 I != E; ++I)
2674 WriteDeclContextVisibleUpdate(*I);
2675
Sebastian Redl401b39a2010-08-24 22:50:24 +00002676 // Write the updates to C++ template specialization lists.
2677 if (!AdditionalTemplateSpecializations.empty())
2678 WriteAdditionalTemplateSpecializations();
2679
Sebastian Redl98912122010-07-27 23:01:28 +00002680 Record.clear();
2681 Record.push_back(NumStatements);
2682 Record.push_back(NumMacros);
2683 Record.push_back(NumLexicalDeclContexts);
2684 Record.push_back(NumVisibleDeclContexts);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002685 WriteDeclUpdateBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002686 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002687 Stream.ExitBlock();
2688}
2689
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002690void ASTWriter::WriteDeclUpdateBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002691 if (ReplacedDecls.empty())
2692 return;
2693
2694 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002695 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002696 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2697 Record.push_back(I->first);
2698 Record.push_back(I->second);
2699 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002700 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002701}
2702
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002703void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002704 Record.push_back(Loc.getRawEncoding());
2705}
2706
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002707void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002708 AddSourceLocation(Range.getBegin(), Record);
2709 AddSourceLocation(Range.getEnd(), Record);
2710}
2711
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002712void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002713 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002714 const uint64_t *Words = Value.getRawData();
2715 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002716}
2717
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002718void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002719 Record.push_back(Value.isUnsigned());
2720 AddAPInt(Value, Record);
2721}
2722
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002723void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002724 AddAPInt(Value.bitcastToAPInt(), Record);
2725}
2726
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002727void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002728 Record.push_back(getIdentifierRef(II));
2729}
2730
Sebastian Redl539c5062010-08-18 23:57:32 +00002731IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002732 if (II == 0)
2733 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002734
Sebastian Redl539c5062010-08-18 23:57:32 +00002735 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002736 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002737 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002738 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002739}
2740
Sebastian Redl50e26582010-09-15 19:54:06 +00002741MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002742 if (MD == 0)
2743 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002744
2745 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002746 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002747 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002748 return ID;
2749}
2750
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002751void ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002752 Record.push_back(getSelectorRef(SelRef));
2753}
2754
Sebastian Redl539c5062010-08-18 23:57:32 +00002755SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002756 if (Sel.getAsOpaquePtr() == 0) {
2757 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002758 }
2759
Sebastian Redl539c5062010-08-18 23:57:32 +00002760 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002761 if (SID == 0 && Chain) {
2762 // This might trigger a ReadSelector callback, which will set the ID for
2763 // this selector.
2764 Chain->LoadSelector(Sel);
2765 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002766 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002767 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002768 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002769 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002770}
2771
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002772void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002773 AddDeclRef(Temp->getDestructor(), Record);
2774}
2775
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002776void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002777 const TemplateArgumentLocInfo &Arg,
2778 RecordData &Record) {
2779 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002780 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002781 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002782 break;
2783 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002784 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002785 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002786 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002787 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2788 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002789 break;
John McCall0ad16662009-10-29 08:12:44 +00002790 case TemplateArgument::Null:
2791 case TemplateArgument::Integral:
2792 case TemplateArgument::Declaration:
2793 case TemplateArgument::Pack:
2794 break;
2795 }
2796}
2797
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002798void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002799 RecordData &Record) {
2800 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002801
2802 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2803 bool InfoHasSameExpr
2804 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2805 Record.push_back(InfoHasSameExpr);
2806 if (InfoHasSameExpr)
2807 return; // Avoid storing the same expr twice.
2808 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002809 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2810 Record);
2811}
2812
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002813void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002814 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002815 AddTypeRef(QualType(), Record);
2816 return;
2817 }
2818
John McCallbcd03502009-12-07 02:54:59 +00002819 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002820 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002821 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002822 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002823}
2824
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002825void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002826 Record.push_back(GetOrCreateTypeID(T));
2827}
2828
2829TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002830 return MakeTypeID(T,
2831 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2832}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002833
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002834TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002835 return MakeTypeID(T,
2836 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002837}
2838
2839TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2840 if (T.isNull())
2841 return TypeIdx();
2842 assert(!T.getLocalFastQualifiers());
2843
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002844 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002845 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002846 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002847 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002848 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002849 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002850 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002851 return Idx;
2852}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002853
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002854TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002855 if (T.isNull())
2856 return TypeIdx();
2857 assert(!T.getLocalFastQualifiers());
2858
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002859 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2860 assert(I != TypeIdxs.end() && "Type not emitted!");
2861 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002862}
2863
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002864void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002865 Record.push_back(GetDeclRef(D));
2866}
2867
Sebastian Redl539c5062010-08-18 23:57:32 +00002868DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002869 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002870 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002871 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002872 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00002873 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002874 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002875 // We haven't seen this declaration before. Give it a new ID and
2876 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002877 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002878 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002879 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2880 // We don't add it to the replacement collection here, because we don't
2881 // have the offset yet.
2882 DeclTypesToEmit.push(const_cast<Decl *>(D));
2883 // Reset the flag, so that we don't add this decl multiple times.
2884 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002885 }
2886
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002887 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002888}
2889
Sebastian Redl539c5062010-08-18 23:57:32 +00002890DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002891 if (D == 0)
2892 return 0;
2893
2894 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2895 return DeclIDs[D];
2896}
2897
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002898void ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002899 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002900 Record.push_back(Name.getNameKind());
2901 switch (Name.getNameKind()) {
2902 case DeclarationName::Identifier:
2903 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2904 break;
2905
2906 case DeclarationName::ObjCZeroArgSelector:
2907 case DeclarationName::ObjCOneArgSelector:
2908 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002909 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002910 break;
2911
2912 case DeclarationName::CXXConstructorName:
2913 case DeclarationName::CXXDestructorName:
2914 case DeclarationName::CXXConversionFunctionName:
2915 AddTypeRef(Name.getCXXNameType(), Record);
2916 break;
2917
2918 case DeclarationName::CXXOperatorName:
2919 Record.push_back(Name.getCXXOverloadedOperator());
2920 break;
2921
Alexis Hunt3d221f22009-11-29 07:34:05 +00002922 case DeclarationName::CXXLiteralOperatorName:
2923 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2924 break;
2925
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002926 case DeclarationName::CXXUsingDirective:
2927 // No extra data to emit
2928 break;
2929 }
2930}
Chris Lattnerca025db2010-05-07 21:43:38 +00002931
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002932void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Chris Lattnerca025db2010-05-07 21:43:38 +00002933 RecordData &Record) {
2934 // Nested name specifiers usually aren't too long. I think that 8 would
2935 // typically accomodate the vast majority.
2936 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2937
2938 // Push each of the NNS's onto a stack for serialization in reverse order.
2939 while (NNS) {
2940 NestedNames.push_back(NNS);
2941 NNS = NNS->getPrefix();
2942 }
2943
2944 Record.push_back(NestedNames.size());
2945 while(!NestedNames.empty()) {
2946 NNS = NestedNames.pop_back_val();
2947 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2948 Record.push_back(Kind);
2949 switch (Kind) {
2950 case NestedNameSpecifier::Identifier:
2951 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2952 break;
2953
2954 case NestedNameSpecifier::Namespace:
2955 AddDeclRef(NNS->getAsNamespace(), Record);
2956 break;
2957
2958 case NestedNameSpecifier::TypeSpec:
2959 case NestedNameSpecifier::TypeSpecWithTemplate:
2960 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2961 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2962 break;
2963
2964 case NestedNameSpecifier::Global:
2965 // Don't need to write an associated value.
2966 break;
2967 }
2968 }
2969}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002970
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002971void ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00002972 TemplateName::NameKind Kind = Name.getKind();
2973 Record.push_back(Kind);
2974 switch (Kind) {
2975 case TemplateName::Template:
2976 AddDeclRef(Name.getAsTemplateDecl(), Record);
2977 break;
2978
2979 case TemplateName::OverloadedTemplate: {
2980 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2981 Record.push_back(OvT->size());
2982 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2983 I != E; ++I)
2984 AddDeclRef(*I, Record);
2985 break;
2986 }
2987
2988 case TemplateName::QualifiedTemplate: {
2989 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2990 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2991 Record.push_back(QualT->hasTemplateKeyword());
2992 AddDeclRef(QualT->getTemplateDecl(), Record);
2993 break;
2994 }
2995
2996 case TemplateName::DependentTemplate: {
2997 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2998 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2999 Record.push_back(DepT->isIdentifier());
3000 if (DepT->isIdentifier())
3001 AddIdentifierRef(DepT->getIdentifier(), Record);
3002 else
3003 Record.push_back(DepT->getOperator());
3004 break;
3005 }
3006 }
3007}
3008
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003009void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003010 RecordData &Record) {
3011 Record.push_back(Arg.getKind());
3012 switch (Arg.getKind()) {
3013 case TemplateArgument::Null:
3014 break;
3015 case TemplateArgument::Type:
3016 AddTypeRef(Arg.getAsType(), Record);
3017 break;
3018 case TemplateArgument::Declaration:
3019 AddDeclRef(Arg.getAsDecl(), Record);
3020 break;
3021 case TemplateArgument::Integral:
3022 AddAPSInt(*Arg.getAsIntegral(), Record);
3023 AddTypeRef(Arg.getIntegralType(), Record);
3024 break;
3025 case TemplateArgument::Template:
3026 AddTemplateName(Arg.getAsTemplate(), Record);
3027 break;
3028 case TemplateArgument::Expression:
3029 AddStmt(Arg.getAsExpr());
3030 break;
3031 case TemplateArgument::Pack:
3032 Record.push_back(Arg.pack_size());
3033 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3034 I != E; ++I)
3035 AddTemplateArgument(*I, Record);
3036 break;
3037 }
3038}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003039
3040void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003041ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003042 RecordData &Record) {
3043 assert(TemplateParams && "No TemplateParams!");
3044 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3045 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3046 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3047 Record.push_back(TemplateParams->size());
3048 for (TemplateParameterList::const_iterator
3049 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3050 P != PEnd; ++P)
3051 AddDeclRef(*P, Record);
3052}
3053
3054/// \brief Emit a template argument list.
3055void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003056ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003057 RecordData &Record) {
3058 assert(TemplateArgs && "No TemplateArgs!");
3059 Record.push_back(TemplateArgs->flat_size());
3060 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
3061 AddTemplateArgument(TemplateArgs->get(i), Record);
3062}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003063
3064
3065void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003066ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003067 Record.push_back(Set.size());
3068 for (UnresolvedSetImpl::const_iterator
3069 I = Set.begin(), E = Set.end(); I != E; ++I) {
3070 AddDeclRef(I.getDecl(), Record);
3071 Record.push_back(I.getAccess());
3072 }
3073}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003074
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003075void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003076 RecordData &Record) {
3077 Record.push_back(Base.isVirtual());
3078 Record.push_back(Base.isBaseOfClass());
3079 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003080 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003081 AddSourceRange(Base.getSourceRange(), Record);
3082}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003083
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003084void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003085 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
3086 unsigned NumBaseOrMembers, RecordData &Record) {
3087 Record.push_back(NumBaseOrMembers);
3088 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3089 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3090
3091 Record.push_back(Init->isBaseInitializer());
3092 if (Init->isBaseInitializer()) {
3093 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3094 Record.push_back(Init->isBaseVirtual());
3095 } else {
3096 AddDeclRef(Init->getMember(), Record);
3097 }
3098 AddSourceLocation(Init->getMemberLocation(), Record);
3099 AddStmt(Init->getInit());
3100 AddDeclRef(Init->getAnonUnionMember(), Record);
3101 AddSourceLocation(Init->getLParenLoc(), Record);
3102 AddSourceLocation(Init->getRParenLoc(), Record);
3103 Record.push_back(Init->isWritten());
3104 if (Init->isWritten()) {
3105 Record.push_back(Init->getSourceOrder());
3106 } else {
3107 Record.push_back(Init->getNumArrayIndices());
3108 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3109 AddDeclRef(Init->getArrayIndex(i), Record);
3110 }
3111 }
3112}
3113
Sebastian Redl2c499f62010-08-18 23:56:43 +00003114void ASTWriter::SetReader(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003115 assert(Reader && "Cannot remove chain");
3116 assert(FirstDeclID == NextDeclID &&
3117 FirstTypeID == NextTypeID &&
3118 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003119 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003120 FirstMacroID == NextMacroID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003121 "Setting chain after writing has started.");
3122 Chain = Reader;
3123}
3124
Sebastian Redl539c5062010-08-18 23:57:32 +00003125void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003126 IdentifierIDs[II] = ID;
3127}
3128
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003129void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003130 // Always take the highest-numbered type index. This copes with an interesting
3131 // case for chained AST writing where we schedule writing the type and then,
3132 // later, deserialize the type from another AST. In this case, we want to
3133 // keep the higher-numbered entry so that we can properly write it out to
3134 // the AST file.
3135 TypeIdx &StoredIdx = TypeIdxs[T];
3136 if (Idx.getIndex() >= StoredIdx.getIndex())
3137 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003138}
3139
Sebastian Redl539c5062010-08-18 23:57:32 +00003140void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003141 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003142}
Sebastian Redl834bb972010-08-04 17:20:04 +00003143
Sebastian Redl539c5062010-08-18 23:57:32 +00003144void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003145 SelectorIDs[S] = ID;
3146}
Douglas Gregor91096292010-10-02 19:29:26 +00003147
3148void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
3149 MacroDefinition *MD) {
3150 MacroDefinitions[MD] = ID;
3151}