blob: 2cbe08190b7348746ce00d64077665eb39fea8b8 [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_TRANSLATION_UNIT);
688 RECORD(DECL_TYPEDEF);
689 RECORD(DECL_ENUM);
690 RECORD(DECL_RECORD);
691 RECORD(DECL_ENUM_CONSTANT);
692 RECORD(DECL_FUNCTION);
693 RECORD(DECL_OBJC_METHOD);
694 RECORD(DECL_OBJC_INTERFACE);
695 RECORD(DECL_OBJC_PROTOCOL);
696 RECORD(DECL_OBJC_IVAR);
697 RECORD(DECL_OBJC_AT_DEFS_FIELD);
698 RECORD(DECL_OBJC_CLASS);
699 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
700 RECORD(DECL_OBJC_CATEGORY);
701 RECORD(DECL_OBJC_CATEGORY_IMPL);
702 RECORD(DECL_OBJC_IMPLEMENTATION);
703 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
704 RECORD(DECL_OBJC_PROPERTY);
705 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000706 RECORD(DECL_FIELD);
707 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000708 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000709 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000710 RECORD(DECL_FILE_SCOPE_ASM);
711 RECORD(DECL_BLOCK);
712 RECORD(DECL_CONTEXT_LEXICAL);
713 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000714 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000715 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000716#undef RECORD
717#undef BLOCK
718 Stream.ExitBlock();
719}
720
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000721/// \brief Adjusts the given filename to only write out the portion of the
722/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000723///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000724/// \param Filename the file name to adjust.
725///
726/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
727/// the returned filename will be adjusted by this system root.
728///
729/// \returns either the original filename (if it needs no adjustment) or the
730/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000731static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000732adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
733 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000734
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000735 if (!isysroot)
736 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000737
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000738 // Verify that the filename and the system root have the same prefix.
739 unsigned Pos = 0;
740 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
741 if (Filename[Pos] != isysroot[Pos])
742 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000743
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000744 // We hit the end of the filename before we hit the end of the system root.
745 if (!Filename[Pos])
746 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000747
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000748 // If the file name has a '/' at the current position, skip over the '/'.
749 // We distinguish sysroot-based includes from absolute includes by the
750 // absence of '/' at the beginning of sysroot-based includes.
751 if (Filename[Pos] == '/')
752 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000753
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000754 return Filename + Pos;
755}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000756
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000757/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000758void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000759 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000760
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000761 // Metadata
762 const TargetInfo &Target = Context.Target;
763 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000764 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000765 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000766 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000771 // Target triple or chained PCH name
772 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000773 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000774
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000775 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000776 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
777 Record.push_back(VERSION_MAJOR);
778 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000779 Record.push_back(CLANG_VERSION_MAJOR);
780 Record.push_back(CLANG_VERSION_MINOR);
781 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000782 // FIXME: This writes the absolute path for chained headers.
783 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
784 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000785
Douglas Gregor45fe0362009-05-12 01:31:05 +0000786 // Original file name
787 SourceManager &SM = Context.getSourceManager();
788 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
789 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000790 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000791 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
792 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
793
794 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000795
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000796 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000797
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000798 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000799 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000800 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000801 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000802 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000803 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000804 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000805
Ted Kremenek18e066f2010-01-22 22:12:47 +0000806 // Repository branch/version information.
807 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000808 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000809 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
810 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000811 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000812 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000813 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
814 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000815}
816
817/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000818void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000819 RecordData Record;
820 Record.push_back(LangOpts.Trigraphs);
821 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
822 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
823 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
824 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000825 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000826 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
827 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
828 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
829 Record.push_back(LangOpts.C99); // C99 Support
830 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
831 Record.push_back(LangOpts.CPlusPlus); // C++ Support
832 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000833 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000834
Douglas Gregor55abb232009-04-10 20:39:37 +0000835 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
836 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000837 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000838 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000839 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000840 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000841 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000842
Douglas Gregor55abb232009-04-10 20:39:37 +0000843 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000844 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
845 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000846 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000847 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000848 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000849
850 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
851 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
852 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
853
Chris Lattner258172e2009-04-27 07:35:58 +0000854 // Whether static initializers are protected by locks.
855 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000856 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000857 Record.push_back(LangOpts.Blocks); // block extension to C
858 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
859 // they are unused.
860 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
861 // (modulo the platform support).
862
Chris Lattner51924e512010-06-26 21:25:03 +0000863 Record.push_back(LangOpts.getSignedOverflowBehavior());
864 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000865
866 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000867 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000868 // defined.
869 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
870 // opposed to __DYNAMIC__).
871 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
872
873 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
874 // used (instead of C99 semantics).
875 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000876 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
877 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000878 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
879 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000880 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000881 Record.push_back(LangOpts.getGCMode());
882 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000883 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000884 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000885 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000886 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000887 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000888 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000889 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000890}
891
Douglas Gregora7f71a92009-04-10 03:52:48 +0000892//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000893// stat cache Serialization
894//===----------------------------------------------------------------------===//
895
896namespace {
897// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000898class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000899public:
900 typedef const char * key_type;
901 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000902
Douglas Gregorc5046832009-04-27 18:38:38 +0000903 typedef std::pair<int, struct stat> data_type;
904 typedef const data_type& data_type_ref;
905
906 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000907 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000908 }
Mike Stump11289f42009-09-09 15:08:12 +0000909
910 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000911 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
912 data_type_ref Data) {
913 unsigned StrLen = strlen(path);
914 clang::io::Emit16(Out, StrLen);
915 unsigned DataLen = 1; // result value
916 if (Data.first == 0)
917 DataLen += 4 + 4 + 2 + 8 + 8;
918 clang::io::Emit8(Out, DataLen);
919 return std::make_pair(StrLen + 1, DataLen);
920 }
Mike Stump11289f42009-09-09 15:08:12 +0000921
Douglas Gregorc5046832009-04-27 18:38:38 +0000922 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
923 Out.write(path, KeyLen);
924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Douglas Gregorc5046832009-04-27 18:38:38 +0000926 void EmitData(llvm::raw_ostream& Out, key_type_ref,
927 data_type_ref Data, unsigned DataLen) {
928 using namespace clang::io;
929 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000930
Douglas Gregorc5046832009-04-27 18:38:38 +0000931 // Result of stat()
932 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000933
Douglas Gregorc5046832009-04-27 18:38:38 +0000934 if (Data.first == 0) {
935 Emit32(Out, (uint32_t) Data.second.st_ino);
936 Emit32(Out, (uint32_t) Data.second.st_dev);
937 Emit16(Out, (uint16_t) Data.second.st_mode);
938 Emit64(Out, (uint64_t) Data.second.st_mtime);
939 Emit64(Out, (uint64_t) Data.second.st_size);
940 }
941
942 assert(Out.tell() - Start == DataLen && "Wrong data length");
943 }
944};
945} // end anonymous namespace
946
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000947/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000948void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000949 // Build the on-disk hash table containing information about every
950 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000951 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000952 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000953 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000954 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000955 Stat != StatEnd; ++Stat, ++NumStatEntries) {
956 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000957 Generator.insert(Filename, Stat->second);
958 }
Mike Stump11289f42009-09-09 15:08:12 +0000959
Douglas Gregorc5046832009-04-27 18:38:38 +0000960 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000961 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000962 uint32_t BucketOffset;
963 {
964 llvm::raw_svector_ostream Out(StatCacheData);
965 // Make sure that no bucket is at offset 0
966 clang::io::Emit32(Out, 0);
967 BucketOffset = Generator.Emit(Out);
968 }
969
970 // Create a blob abbreviation
971 using namespace llvm;
972 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000973 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
977 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
978
979 // Write the stat cache
980 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000981 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +0000982 Record.push_back(BucketOffset);
983 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000984 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000985}
986
987//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000988// Source Manager Serialization
989//===----------------------------------------------------------------------===//
990
991/// \brief Create an abbreviation for the SLocEntry that refers to a
992/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000993static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000994 using namespace llvm;
995 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000996 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +0000997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001001 // FileEntry fields.
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001004 // HeaderFileInfo fields.
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001010 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001011}
1012
1013/// \brief Create an abbreviation for the SLocEntry that refers to a
1014/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001015static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001016 using namespace llvm;
1017 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001018 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001024 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001025}
1026
1027/// \brief Create an abbreviation for the SLocEntry that refers to a
1028/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001029static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001030 using namespace llvm;
1031 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001032 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001034 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001035}
1036
1037/// \brief Create an abbreviation for the SLocEntry that refers to an
1038/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001039static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001040 using namespace llvm;
1041 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001042 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001048 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001049}
1050
1051/// \brief Writes the block containing the serialized form of the
1052/// source manager.
1053///
1054/// TODO: We should probably use an on-disk hash table (stored in a
1055/// blob), indexed based on the file name, so that we only create
1056/// entries for files that we actually need. In the common case (no
1057/// errors), we probably won't have to create file entries for any of
1058/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001059void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001060 const Preprocessor &PP,
1061 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001062 RecordData Record;
1063
Chris Lattner0910e3b2009-04-10 17:16:57 +00001064 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001065 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001066
1067 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001068 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1069 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1070 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1071 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001072
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001073 // Write the line table.
1074 if (SourceMgr.hasLineTable()) {
1075 LineTableInfo &LineTable = SourceMgr.getLineTable();
1076
1077 // Emit the file names
1078 Record.push_back(LineTable.getNumFilenames());
1079 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1080 // Emit the file name
1081 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001082 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001083 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1084 Record.push_back(FilenameLen);
1085 if (FilenameLen)
1086 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1087 }
Mike Stump11289f42009-09-09 15:08:12 +00001088
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001089 // Emit the line entries
1090 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1091 L != LEnd; ++L) {
1092 // Emit the file ID
1093 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001094
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001095 // Emit the line entries
1096 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001097 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001098 LEEnd = L->second.end();
1099 LE != LEEnd; ++LE) {
1100 Record.push_back(LE->FileOffset);
1101 Record.push_back(LE->LineNo);
1102 Record.push_back(LE->FilenameID);
1103 Record.push_back((unsigned)LE->FileKind);
1104 Record.push_back(LE->IncludeOffset);
1105 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001106 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001107 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001108 }
1109
Douglas Gregor258ae542009-04-27 06:38:32 +00001110 // Write out the source location entry table. We skip the first
1111 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001112 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001113 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001114 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1115 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1116 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1117 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001118 // Get this source location entry.
1119 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001120
Douglas Gregor258ae542009-04-27 06:38:32 +00001121 // Record the offset of this source-location entry.
1122 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1123
1124 // Figure out which record code to use.
1125 unsigned Code;
1126 if (SLoc->isFile()) {
1127 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001128 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001129 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001130 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001131 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001132 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001133 Record.clear();
1134 Record.push_back(Code);
1135
1136 Record.push_back(SLoc->getOffset());
1137 if (SLoc->isFile()) {
1138 const SrcMgr::FileInfo &File = SLoc->getFile();
1139 Record.push_back(File.getIncludeLoc().getRawEncoding());
1140 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1141 Record.push_back(File.hasLineDirectives());
1142
1143 const SrcMgr::ContentCache *Content = File.getContentCache();
1144 if (Content->Entry) {
1145 // The source location entry is a file. The blob associated
1146 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001147
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001148 // Emit size/modification time for this file.
1149 Record.push_back(Content->Entry->getSize());
1150 Record.push_back(Content->Entry->getModificationTime());
1151
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001152 // Emit header-search information associated with this file.
1153 HeaderFileInfo HFI;
1154 HeaderSearch &HS = PP.getHeaderSearchInfo();
1155 if (Content->Entry->getUID() < HS.header_file_size())
1156 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1157 Record.push_back(HFI.isImport);
1158 Record.push_back(HFI.DirInfo);
1159 Record.push_back(HFI.NumIncludes);
1160 AddIdentifierRef(HFI.ControllingMacro, Record);
1161
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001162 // Turn the file name into an absolute path, if it isn't already.
1163 const char *Filename = Content->Entry->getName();
1164 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001165 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001166 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001167
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001168 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001169 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001170
1171 // FIXME: For now, preload all file source locations, so that
1172 // we get the appropriate File entries in the reader. This is
1173 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001174 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001175 } else {
1176 // The source location entry is a buffer. The blob associated
1177 // with this entry contains the contents of the buffer.
1178
1179 // We add one to the size so that we capture the trailing NULL
1180 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1181 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001182 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001183 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001184 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001185 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1186 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001187 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001188 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001189 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001190 llvm::StringRef(Buffer->getBufferStart(),
1191 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001192
1193 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001194 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001195 }
1196 } else {
1197 // The source location entry is an instantiation.
1198 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1199 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1200 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1201 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1202
1203 // Compute the token length for this macro expansion.
1204 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001205 if (I + 1 != N)
1206 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001207 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1208 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1209 }
1210 }
1211
Douglas Gregor8f45df52009-04-16 22:23:12 +00001212 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001213
1214 if (SLocEntryOffsets.empty())
1215 return;
1216
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001217 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001218 // table is used for lazily loading source-location information.
1219 using namespace llvm;
1220 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001221 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1225 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001226
Douglas Gregor258ae542009-04-27 06:38:32 +00001227 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001228 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001229 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001230 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1231 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001232 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001233 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001234 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001235
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001236 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001237 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001238 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001239}
1240
Douglas Gregorc5046832009-04-27 18:38:38 +00001241//===----------------------------------------------------------------------===//
1242// Preprocessor Serialization
1243//===----------------------------------------------------------------------===//
1244
Chris Lattnereeffaef2009-04-10 17:15:23 +00001245/// \brief Writes the block containing the serialized form of the
1246/// preprocessor.
1247///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001248void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001249 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001250
Chris Lattner0af3ba12009-04-13 01:29:17 +00001251 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1252 if (PP.getCounterValue() != 0) {
1253 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001254 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001255 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001256 }
1257
1258 // Enter the preprocessor block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001259 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 2);
Mike Stump11289f42009-09-09 15:08:12 +00001260
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001261 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001262 // FIXME: use diagnostics subsystem for localization etc.
1263 if (PP.SawDateOrTime())
1264 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001265
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001266 // Loop over all the macro definitions that are live at the end of the file,
1267 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001268 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001269 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1270 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001271 // FIXME: This emits macros in hash table order, we should do it in a stable
1272 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001273 MacroInfo *MI = I->second;
1274
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001275 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001276 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001277 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001278
1279 // FIXME: There is a (probably minor) optimization we could do here, if
1280 // the macro comes from the original PCH but the identifier comes from a
1281 // chained PCH, by storing the offset into the original PCH rather than
1282 // writing the macro definition a second time.
1283 if (MI->isBuiltinMacro() ||
1284 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001285 continue;
1286
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001287 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001288 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001289 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1290 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001291
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001292 unsigned Code;
1293 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001294 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001295 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001296 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001297
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001298 Record.push_back(MI->isC99Varargs());
1299 Record.push_back(MI->isGNUVarargs());
1300 Record.push_back(MI->getNumArgs());
1301 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1302 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001303 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001304 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001305
1306 // If we have a detailed preprocessing record, record the macro definition
1307 // ID that corresponds to this macro.
1308 if (PPRec)
1309 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1310
Douglas Gregor8f45df52009-04-16 22:23:12 +00001311 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001312 Record.clear();
1313
Chris Lattner2199f5b2009-04-10 18:08:30 +00001314 // Emit the tokens array.
1315 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1316 // Note that we know that the preprocessor does not have any annotation
1317 // tokens in it because they are created by the parser, and thus can't be
1318 // in a macro definition.
1319 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001320
Chris Lattner2199f5b2009-04-10 18:08:30 +00001321 Record.push_back(Tok.getLocation().getRawEncoding());
1322 Record.push_back(Tok.getLength());
1323
Chris Lattner2199f5b2009-04-10 18:08:30 +00001324 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1325 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001326 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001327
Chris Lattner2199f5b2009-04-10 18:08:30 +00001328 // FIXME: Should translate token kind to a stable encoding.
1329 Record.push_back(Tok.getKind());
1330 // FIXME: Should translate token flags to a stable encoding.
1331 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001332
Sebastian Redl539c5062010-08-18 23:57:32 +00001333 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001334 Record.clear();
1335 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001336 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001337 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001338
1339 // If the preprocessor has a preprocessing record, emit it.
1340 unsigned NumPreprocessingRecords = 0;
1341 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001342 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001343 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1344 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001345 E != EEnd; ++E) {
1346 Record.clear();
1347
1348 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001349 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001350 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1351 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1352 AddIdentifierRef(MI->getName(), Record);
1353 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
Sebastian Redl539c5062010-08-18 23:57:32 +00001354 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001355 continue;
1356 }
1357
1358 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1359 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001360 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregor91096292010-10-02 19:29:26 +00001361
1362 // Don't write the macro definition if it is from another AST file.
1363 if (ID < FirstMacroID)
1364 continue;
1365
1366 unsigned Position = ID - FirstMacroID;
1367 if (Position != MacroDefinitionOffsets.size()) {
1368 if (Position > MacroDefinitionOffsets.size())
1369 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregoraae92242010-03-19 21:51:54 +00001370
Douglas Gregor91096292010-10-02 19:29:26 +00001371 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001372 } else
1373 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1374
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001375 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001376 Record.push_back(ID);
1377 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1378 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1379 AddIdentifierRef(MD->getName(), Record);
1380 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001381 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001382 continue;
1383 }
1384 }
1385 }
1386
Douglas Gregor8f45df52009-04-16 22:23:12 +00001387 Stream.ExitBlock();
Douglas Gregoraae92242010-03-19 21:51:54 +00001388
1389 // Write the offsets table for the preprocessing record.
1390 if (NumPreprocessingRecords > 0) {
1391 // Write the offsets table for identifier IDs.
1392 using namespace llvm;
1393 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001394 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1398 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1399
1400 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001401 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001402 Record.push_back(NumPreprocessingRecords);
1403 Record.push_back(MacroDefinitionOffsets.size());
1404 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001405 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001406 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1407 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001408}
1409
Douglas Gregorc5046832009-04-27 18:38:38 +00001410//===----------------------------------------------------------------------===//
1411// Type Serialization
1412//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001413
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001414/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001415void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001416 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001417 if (Idx.getIndex() == 0) // we haven't seen this type before.
1418 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001419
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001420 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001421
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001422 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001423 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001424 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001425 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001426 else if (TypeOffsets.size() < Index) {
1427 TypeOffsets.resize(Index + 1);
1428 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001429 }
1430
1431 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001432
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001433 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001434 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001435
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001436 if (T.hasLocalNonFastQualifiers()) {
1437 Qualifiers Qs = T.getLocalQualifiers();
1438 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001439 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001440 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001441 } else {
1442 switch (T->getTypeClass()) {
1443 // For all of the concrete, non-dependent types, call the
1444 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001445#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001446 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001447#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001448#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001449 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001450 }
1451
1452 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001453 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001454
1455 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001456 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001457}
1458
Douglas Gregorc5046832009-04-27 18:38:38 +00001459//===----------------------------------------------------------------------===//
1460// Declaration Serialization
1461//===----------------------------------------------------------------------===//
1462
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001463/// \brief Write the block containing all of the declaration IDs
1464/// lexically declared within the given DeclContext.
1465///
1466/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1467/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001468uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001469 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001470 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001471 return 0;
1472
Douglas Gregor8f45df52009-04-16 22:23:12 +00001473 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001474 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001475 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001476 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001477 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1478 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001479 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001480
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001481 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001482 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001483 reinterpret_cast<char*>(Decls.data()),
1484 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001485 return Offset;
1486}
1487
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001488void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001489 using namespace llvm;
1490 RecordData Record;
1491
1492 // Write the type offsets array
1493 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001494 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1496 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1497 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1498 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001499 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001500 Record.push_back(TypeOffsets.size());
1501 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001502 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001503 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1504
1505 // Write the declaration offsets array
1506 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001507 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001508 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1510 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1511 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001512 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001513 Record.push_back(DeclOffsets.size());
1514 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001515 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001516 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1517}
1518
Douglas Gregorc5046832009-04-27 18:38:38 +00001519//===----------------------------------------------------------------------===//
1520// Global Method Pool and Selector Serialization
1521//===----------------------------------------------------------------------===//
1522
Douglas Gregore84a9da2009-04-20 20:36:09 +00001523namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001524// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001525class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001526 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001527
1528public:
1529 typedef Selector key_type;
1530 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001531
Sebastian Redl834bb972010-08-04 17:20:04 +00001532 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001533 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001534 ObjCMethodList Instance, Factory;
1535 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001536 typedef const data_type& data_type_ref;
1537
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001538 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001539
Douglas Gregorc78d3462009-04-24 21:10:55 +00001540 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001541 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001542 }
Mike Stump11289f42009-09-09 15:08:12 +00001543
1544 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001545 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1546 data_type_ref Methods) {
1547 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1548 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001549 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1550 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001551 Method = Method->Next)
1552 if (Method->Method)
1553 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001554 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001555 Method = Method->Next)
1556 if (Method->Method)
1557 DataLen += 4;
1558 clang::io::Emit16(Out, DataLen);
1559 return std::make_pair(KeyLen, DataLen);
1560 }
Mike Stump11289f42009-09-09 15:08:12 +00001561
Douglas Gregor95c13f52009-04-25 17:48:32 +00001562 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001563 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001564 assert((Start >> 32) == 0 && "Selector key offset too large");
1565 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001566 unsigned N = Sel.getNumArgs();
1567 clang::io::Emit16(Out, N);
1568 if (N == 0)
1569 N = 1;
1570 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001571 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001572 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1573 }
Mike Stump11289f42009-09-09 15:08:12 +00001574
Douglas Gregorc78d3462009-04-24 21:10:55 +00001575 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001576 data_type_ref Methods, unsigned DataLen) {
1577 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001578 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001579 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001580 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001581 Method = Method->Next)
1582 if (Method->Method)
1583 ++NumInstanceMethods;
1584
1585 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001586 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001587 Method = Method->Next)
1588 if (Method->Method)
1589 ++NumFactoryMethods;
1590
1591 clang::io::Emit16(Out, NumInstanceMethods);
1592 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001593 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001594 Method = Method->Next)
1595 if (Method->Method)
1596 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001597 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001598 Method = Method->Next)
1599 if (Method->Method)
1600 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001601
1602 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001603 }
1604};
1605} // end anonymous namespace
1606
Sebastian Redla19a67f2010-08-03 21:58:15 +00001607/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001608///
1609/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001610/// in an on-disk hash table indexed by the selector. The hash table also
1611/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001612void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001613 using namespace llvm;
1614
Sebastian Redla19a67f2010-08-03 21:58:15 +00001615 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001616 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001617 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001618 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001619 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001620 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001621 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001622 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001623
Sebastian Redla19a67f2010-08-03 21:58:15 +00001624 // Create the on-disk hash table representation. We walk through every
1625 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001626 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001627 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001628 I = SelectorIDs.begin(), E = SelectorIDs.end();
1629 I != E; ++I) {
1630 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001631 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001632 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001633 I->second,
1634 ObjCMethodList(),
1635 ObjCMethodList()
1636 };
1637 if (F != SemaRef.MethodPool.end()) {
1638 Data.Instance = F->second.first;
1639 Data.Factory = F->second.second;
1640 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001641 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001642 // changed.
1643 if (Chain && I->second < FirstSelectorID) {
1644 // Selector already exists. Did it change?
1645 bool changed = false;
1646 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1647 M = M->Next) {
1648 if (M->Method->getPCHLevel() == 0)
1649 changed = true;
1650 }
1651 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1652 M = M->Next) {
1653 if (M->Method->getPCHLevel() == 0)
1654 changed = true;
1655 }
1656 if (!changed)
1657 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001658 } else if (Data.Instance.Method || Data.Factory.Method) {
1659 // A new method pool entry.
1660 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001661 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001662 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001663 }
1664
Douglas Gregorc78d3462009-04-24 21:10:55 +00001665 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001666 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001667 uint32_t BucketOffset;
1668 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001669 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001670 llvm::raw_svector_ostream Out(MethodPool);
1671 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001672 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001673 BucketOffset = Generator.Emit(Out, Trait);
1674 }
1675
1676 // Create a blob abbreviation
1677 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001678 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001679 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001680 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001681 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1682 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1683
Douglas Gregor95c13f52009-04-25 17:48:32 +00001684 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001685 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001686 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001687 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001688 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001689 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001690
1691 // Create a blob abbreviation for the selector table offsets.
1692 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001693 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001694 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1695 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1696 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1697
1698 // Write the selector offsets table.
1699 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001700 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001701 Record.push_back(SelectorOffsets.size());
1702 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001703 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001704 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001705 }
1706}
1707
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001708/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001709void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001710 using namespace llvm;
1711 if (SemaRef.ReferencedSelectors.empty())
1712 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001713
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001714 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001715
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001716 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001717 // very tricky to fix, and given that @selector shouldn't really appear in
1718 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001719 for (DenseMap<Selector, SourceLocation>::iterator S =
1720 SemaRef.ReferencedSelectors.begin(),
1721 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1722 Selector Sel = (*S).first;
1723 SourceLocation Loc = (*S).second;
1724 AddSelectorRef(Sel, Record);
1725 AddSourceLocation(Loc, Record);
1726 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001727 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001728}
1729
Douglas Gregorc5046832009-04-27 18:38:38 +00001730//===----------------------------------------------------------------------===//
1731// Identifier Table Serialization
1732//===----------------------------------------------------------------------===//
1733
Douglas Gregorc78d3462009-04-24 21:10:55 +00001734namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001735class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001736 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001737 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001738
Douglas Gregor1d583f22009-04-28 21:18:29 +00001739 /// \brief Determines whether this is an "interesting" identifier
1740 /// that needs a full IdentifierInfo structure written into the hash
1741 /// table.
1742 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1743 return II->isPoisoned() ||
1744 II->isExtensionToken() ||
1745 II->hasMacroDefinition() ||
1746 II->getObjCOrBuiltinID() ||
1747 II->getFETokenInfo<void>();
1748 }
1749
Douglas Gregore84a9da2009-04-20 20:36:09 +00001750public:
1751 typedef const IdentifierInfo* key_type;
1752 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001753
Sebastian Redl539c5062010-08-18 23:57:32 +00001754 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001755 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001756
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001757 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001758 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001759
1760 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001761 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001762 }
Mike Stump11289f42009-09-09 15:08:12 +00001763
1764 std::pair<unsigned,unsigned>
1765 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001766 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001767 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001768 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1769 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001770 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001771 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001772 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001773 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001774 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1775 DEnd = IdentifierResolver::end();
1776 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001777 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001778 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001779 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001780 // We emit the key length after the data length so that every
1781 // string is preceded by a 16-bit length. This matches the PTH
1782 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001783 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001784 return std::make_pair(KeyLen, DataLen);
1785 }
Mike Stump11289f42009-09-09 15:08:12 +00001786
1787 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001788 unsigned KeyLen) {
1789 // Record the location of the key data. This is used when generating
1790 // the mapping from persistent IDs to strings.
1791 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001792 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001793 }
Mike Stump11289f42009-09-09 15:08:12 +00001794
1795 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001796 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001797 if (!isInterestingIdentifier(II)) {
1798 clang::io::Emit32(Out, ID << 1);
1799 return;
1800 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001801
Douglas Gregor1d583f22009-04-28 21:18:29 +00001802 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001803 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001804 bool hasMacroDefinition =
1805 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001806 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001807 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001808 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1809 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1810 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001811 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001812 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001813 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001814
Douglas Gregorc3366a52009-04-21 23:56:24 +00001815 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001816 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001817
Douglas Gregora868bbd2009-04-21 22:25:48 +00001818 // Emit the declaration IDs in reverse order, because the
1819 // IdentifierResolver provides the declarations as they would be
1820 // visible (e.g., the function "stat" would come before the struct
1821 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1822 // adds declarations to the end of the list (so we need to see the
1823 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001824 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001825 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001826 IdentifierResolver::end());
1827 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1828 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001829 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001830 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001831 }
1832};
1833} // end anonymous namespace
1834
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001835/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001836///
1837/// The identifier table consists of a blob containing string data
1838/// (the actual identifiers themselves) and a separate "offsets" index
1839/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001840void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001841 using namespace llvm;
1842
1843 // Create and write out the blob that contains the identifier
1844 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001845 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001846 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001847 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001848
Douglas Gregore6648fb2009-04-28 20:33:11 +00001849 // Look for any identifiers that were named while processing the
1850 // headers, but are otherwise not needed. We add these to the hash
1851 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001852 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001853 // file.
1854 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1855 IDEnd = PP.getIdentifierTable().end();
1856 ID != IDEnd; ++ID)
1857 getIdentifierRef(ID->second);
1858
Sebastian Redlff4a2952010-07-23 23:49:55 +00001859 // Create the on-disk hash table representation. We only store offsets
1860 // for identifiers that appear here for the first time.
1861 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001862 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001863 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1864 ID != IDEnd; ++ID) {
1865 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001866 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001867 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001868 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001869
Douglas Gregore84a9da2009-04-20 20:36:09 +00001870 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001871 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001872 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001873 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001874 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001875 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001876 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001877 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001878 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001879 }
1880
1881 // Create a blob abbreviation
1882 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001883 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001886 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001887
1888 // Write the identifier table
1889 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001890 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001891 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001892 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001893 }
1894
1895 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001896 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001897 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1900 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1901
1902 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001903 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001904 Record.push_back(IdentifierOffsets.size());
1905 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001906 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001907 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001908}
1909
Douglas Gregorc5046832009-04-27 18:38:38 +00001910//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001911// DeclContext's Name Lookup Table Serialization
1912//===----------------------------------------------------------------------===//
1913
1914namespace {
1915// Trait used for the on-disk hash table used in the method pool.
1916class ASTDeclContextNameLookupTrait {
1917 ASTWriter &Writer;
1918
1919public:
1920 typedef DeclarationName key_type;
1921 typedef key_type key_type_ref;
1922
1923 typedef DeclContext::lookup_result data_type;
1924 typedef const data_type& data_type_ref;
1925
1926 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1927
1928 unsigned ComputeHash(DeclarationName Name) {
1929 llvm::FoldingSetNodeID ID;
1930 ID.AddInteger(Name.getNameKind());
1931
1932 switch (Name.getNameKind()) {
1933 case DeclarationName::Identifier:
1934 ID.AddString(Name.getAsIdentifierInfo()->getName());
1935 break;
1936 case DeclarationName::ObjCZeroArgSelector:
1937 case DeclarationName::ObjCOneArgSelector:
1938 case DeclarationName::ObjCMultiArgSelector:
1939 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1940 break;
1941 case DeclarationName::CXXConstructorName:
1942 case DeclarationName::CXXDestructorName:
1943 case DeclarationName::CXXConversionFunctionName:
1944 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1945 break;
1946 case DeclarationName::CXXOperatorName:
1947 ID.AddInteger(Name.getCXXOverloadedOperator());
1948 break;
1949 case DeclarationName::CXXLiteralOperatorName:
1950 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1951 case DeclarationName::CXXUsingDirective:
1952 break;
1953 }
1954
1955 return ID.ComputeHash();
1956 }
1957
1958 std::pair<unsigned,unsigned>
1959 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
1960 data_type_ref Lookup) {
1961 unsigned KeyLen = 1;
1962 switch (Name.getNameKind()) {
1963 case DeclarationName::Identifier:
1964 case DeclarationName::ObjCZeroArgSelector:
1965 case DeclarationName::ObjCOneArgSelector:
1966 case DeclarationName::ObjCMultiArgSelector:
1967 case DeclarationName::CXXConstructorName:
1968 case DeclarationName::CXXDestructorName:
1969 case DeclarationName::CXXConversionFunctionName:
1970 case DeclarationName::CXXLiteralOperatorName:
1971 KeyLen += 4;
1972 break;
1973 case DeclarationName::CXXOperatorName:
1974 KeyLen += 1;
1975 break;
1976 case DeclarationName::CXXUsingDirective:
1977 break;
1978 }
1979 clang::io::Emit16(Out, KeyLen);
1980
1981 // 2 bytes for num of decls and 4 for each DeclID.
1982 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
1983 clang::io::Emit16(Out, DataLen);
1984
1985 return std::make_pair(KeyLen, DataLen);
1986 }
1987
1988 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
1989 using namespace clang::io;
1990
1991 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
1992 Emit8(Out, Name.getNameKind());
1993 switch (Name.getNameKind()) {
1994 case DeclarationName::Identifier:
1995 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
1996 break;
1997 case DeclarationName::ObjCZeroArgSelector:
1998 case DeclarationName::ObjCOneArgSelector:
1999 case DeclarationName::ObjCMultiArgSelector:
2000 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2001 break;
2002 case DeclarationName::CXXConstructorName:
2003 case DeclarationName::CXXDestructorName:
2004 case DeclarationName::CXXConversionFunctionName:
2005 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2006 break;
2007 case DeclarationName::CXXOperatorName:
2008 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2009 Emit8(Out, Name.getCXXOverloadedOperator());
2010 break;
2011 case DeclarationName::CXXLiteralOperatorName:
2012 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2013 break;
2014 case DeclarationName::CXXUsingDirective:
2015 break;
2016 }
2017 }
2018
2019 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2020 data_type Lookup, unsigned DataLen) {
2021 uint64_t Start = Out.tell(); (void)Start;
2022 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2023 for (; Lookup.first != Lookup.second; ++Lookup.first)
2024 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2025
2026 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2027 }
2028};
2029} // end anonymous namespace
2030
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002031/// \brief Write the block containing all of the declaration IDs
2032/// visible from the given DeclContext.
2033///
2034/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002035/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002036uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2037 DeclContext *DC) {
2038 if (DC->getPrimaryContext() != DC)
2039 return 0;
2040
2041 // Since there is no name lookup into functions or methods, don't bother to
2042 // build a visible-declarations table for these entities.
2043 if (DC->isFunctionOrMethod())
2044 return 0;
2045
2046 // If not in C++, we perform name lookup for the translation unit via the
2047 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2048 // FIXME: In C++ we need the visible declarations in order to "see" the
2049 // friend declarations, is there a way to do this without writing the table ?
2050 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2051 return 0;
2052
2053 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002054 if (DC->hasExternalVisibleStorage())
2055 DC->MaterializeVisibleDeclsFromExternalStorage();
2056 else
2057 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002058
2059 // Serialize the contents of the mapping used for lookup. Note that,
2060 // although we have two very different code paths, the serialized
2061 // representation is the same for both cases: a declaration name,
2062 // followed by a size, followed by references to the visible
2063 // declarations that have that name.
2064 uint64_t Offset = Stream.GetCurrentBitNo();
2065 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2066 if (!Map || Map->empty())
2067 return 0;
2068
2069 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2070 ASTDeclContextNameLookupTrait Trait(*this);
2071
2072 // Create the on-disk hash table representation.
2073 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2074 D != DEnd; ++D) {
2075 DeclarationName Name = D->first;
2076 DeclContext::lookup_result Result = D->second.getLookupResult();
2077 Generator.insert(Name, Result, Trait);
2078 }
2079
2080 // Create the on-disk hash table in a buffer.
2081 llvm::SmallString<4096> LookupTable;
2082 uint32_t BucketOffset;
2083 {
2084 llvm::raw_svector_ostream Out(LookupTable);
2085 // Make sure that no bucket is at offset 0
2086 clang::io::Emit32(Out, 0);
2087 BucketOffset = Generator.Emit(Out, Trait);
2088 }
2089
2090 // Write the lookup table
2091 RecordData Record;
2092 Record.push_back(DECL_CONTEXT_VISIBLE);
2093 Record.push_back(BucketOffset);
2094 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2095 LookupTable.str());
2096
2097 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2098 ++NumVisibleDeclContexts;
2099 return Offset;
2100}
2101
Sebastian Redla4071b42010-08-24 00:50:09 +00002102/// \brief Write an UPDATE_VISIBLE block for the given context.
2103///
2104/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2105/// DeclContext in a dependent AST file. As such, they only exist for the TU
2106/// (in C++) and for namespaces.
2107void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
2108 assert((DC->isTranslationUnit() || DC->isNamespace()) &&
2109 "Only TU and namespaces should have visible decl updates.");
2110
2111 // Make the context build its lookup table, but don't make it load external
2112 // decls.
2113 DC->lookup(DeclarationName());
2114
2115 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2116 if (!Map || Map->empty())
2117 return;
2118
2119 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2120 ASTDeclContextNameLookupTrait Trait(*this);
2121
2122 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002123 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2124 D != DEnd; ++D) {
2125 DeclarationName Name = D->first;
2126 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002127 // For any name that appears in this table, the results are complete, i.e.
2128 // they overwrite results from previous PCHs. Merging is always a mess.
2129 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002130 }
2131
2132 // Create the on-disk hash table in a buffer.
2133 llvm::SmallString<4096> LookupTable;
2134 uint32_t BucketOffset;
2135 {
2136 llvm::raw_svector_ostream Out(LookupTable);
2137 // Make sure that no bucket is at offset 0
2138 clang::io::Emit32(Out, 0);
2139 BucketOffset = Generator.Emit(Out, Trait);
2140 }
2141
2142 // Write the lookup table
2143 RecordData Record;
2144 Record.push_back(UPDATE_VISIBLE);
2145 Record.push_back(getDeclID(cast<Decl>(DC)));
2146 Record.push_back(BucketOffset);
2147 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2148}
2149
Sebastian Redl401b39a2010-08-24 22:50:24 +00002150/// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates
2151/// that have new specializations in the current AST file.
2152void ASTWriter::WriteAdditionalTemplateSpecializations() {
2153 RecordData Record;
2154 for (AdditionalTemplateSpecializationsMap::iterator
2155 I = AdditionalTemplateSpecializations.begin(),
2156 E = AdditionalTemplateSpecializations.end();
2157 I != E; ++I) {
2158 Record.clear();
2159 Record.push_back(I->first);
2160 Record.insert(Record.end(), I->second.begin(), I->second.end());
2161 Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record);
2162 }
2163}
2164
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002165//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002166// General Serialization Routines
2167//===----------------------------------------------------------------------===//
2168
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002169/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002170void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordData &Record) {
2171 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002172 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2173 const Attr * A = *i;
2174 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2175 AddSourceLocation(A->getLocation(), Record);
2176 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002177
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002178#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002179
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002180 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002181}
2182
Benjamin Kramercd495032010-09-02 15:06:24 +00002183void ASTWriter::AddString(llvm::StringRef Str, RecordData &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002184 Record.push_back(Str.size());
2185 Record.insert(Record.end(), Str.begin(), Str.end());
2186}
2187
Douglas Gregore84a9da2009-04-20 20:36:09 +00002188/// \brief Note that the identifier II occurs at the given offset
2189/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002190void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002191 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002192 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002193 // up earlier in the chain and thus don't need an offset.
2194 if (ID >= FirstIdentID)
2195 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002196}
2197
Douglas Gregor95c13f52009-04-25 17:48:32 +00002198/// \brief Note that the selector Sel occurs at the given offset
2199/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002200void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002201 unsigned ID = SelectorIDs[Sel];
2202 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002203 // Don't record offsets for selectors that are also available in a different
2204 // file.
2205 if (ID < FirstSelectorID)
2206 return;
2207 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002208}
2209
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002210ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redld95a56e2010-08-04 18:21:41 +00002211 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002212 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002213 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002214 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2215 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002216 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2217 NumVisibleDeclContexts(0) {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002218}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002219
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002220void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002221 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002222 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002223 Stream.Emit((unsigned)'C', 8);
2224 Stream.Emit((unsigned)'P', 8);
2225 Stream.Emit((unsigned)'C', 8);
2226 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002227
Chris Lattner28fa4e62009-04-26 22:26:21 +00002228 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002229
Sebastian Redl143413f2010-07-12 22:02:52 +00002230 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002231 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002232 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002233 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002234}
2235
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002236void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002237 const char *isysroot) {
2238 using namespace llvm;
2239
2240 ASTContext &Context = SemaRef.Context;
2241 Preprocessor &PP = SemaRef.PP;
2242
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002243 // The translation unit is the first declaration we'll emit.
2244 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002245 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002246 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002247
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002248 // Make sure that we emit IdentifierInfos (and any attached
2249 // declarations) for builtins.
2250 {
2251 IdentifierTable &Table = PP.getIdentifierTable();
2252 llvm::SmallVector<const char *, 32> BuiltinNames;
2253 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2254 Context.getLangOptions().NoBuiltin);
2255 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2256 getIdentifierRef(&Table.get(BuiltinNames[I]));
2257 }
2258
Chris Lattner0c797362009-09-08 18:19:27 +00002259 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002260 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002261 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002262 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002263 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2264 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002265 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002266
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002267 // Build a record containing all of the file scoped decls in this file.
2268 RecordData UnusedFileScopedDecls;
2269 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2270 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002271
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002272 RecordData WeakUndeclaredIdentifiers;
2273 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2274 WeakUndeclaredIdentifiers.push_back(
2275 SemaRef.WeakUndeclaredIdentifiers.size());
2276 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2277 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2278 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2279 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2280 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2281 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2282 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2283 }
2284 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002285
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002286 // Build a record containing all of the locally-scoped external
2287 // declarations in this header file. Generally, this record will be
2288 // empty.
2289 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002290 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002291 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002292 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002293 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2294 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2295 TD != TDEnd; ++TD)
2296 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2297
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002298 // Build a record containing all of the ext_vector declarations.
2299 RecordData ExtVectorDecls;
2300 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2301 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2302
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002303 // Build a record containing all of the VTable uses information.
2304 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002305 if (!SemaRef.VTableUses.empty()) {
2306 VTableUses.push_back(SemaRef.VTableUses.size());
2307 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2308 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2309 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2310 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2311 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002312 }
2313
2314 // Build a record containing all of dynamic classes declarations.
2315 RecordData DynamicClasses;
2316 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2317 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2318
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002319 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002320 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002321 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002322 I = SemaRef.PendingInstantiations.begin(),
2323 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2324 AddDeclRef(I->first, PendingInstantiations);
2325 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002326 }
2327 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2328 "There are local ones at end of translation unit!");
2329
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002330 // Build a record containing some declaration references.
2331 RecordData SemaDeclRefs;
2332 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2333 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2334 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2335 }
2336
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002337 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002338 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002339 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002340 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002341 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002342 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002343 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002344 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002345 // Write the record of special types.
2346 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002347
Steve Naroffc277ad12009-07-18 15:33:26 +00002348 AddTypeRef(Context.getBuiltinVaListType(), Record);
2349 AddTypeRef(Context.getObjCIdType(), Record);
2350 AddTypeRef(Context.getObjCSelType(), Record);
2351 AddTypeRef(Context.getObjCProtoType(), Record);
2352 AddTypeRef(Context.getObjCClassType(), Record);
2353 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2354 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2355 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002356 AddTypeRef(Context.getjmp_bufType(), Record);
2357 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002358 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2359 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002360 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002361 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002362 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2363 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002364 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002365 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002366
Douglas Gregor1970d882009-04-26 03:49:13 +00002367 // Keep writing types and declarations until all types and
2368 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002369 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002370 WriteDeclsBlockAbbrevs();
2371 while (!DeclTypesToEmit.empty()) {
2372 DeclOrType DOT = DeclTypesToEmit.front();
2373 DeclTypesToEmit.pop();
2374 if (DOT.isType())
2375 WriteType(DOT.getType());
2376 else
2377 WriteDecl(Context, DOT.getDecl());
2378 }
2379 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002380
Douglas Gregor45053152009-10-17 17:25:45 +00002381 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002382 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002383 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002384 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002385
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002386 WriteTypeDeclOffsets();
Douglas Gregor652d82a2009-04-18 05:55:16 +00002387
Douglas Gregord4df8652009-04-22 22:02:47 +00002388 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002389 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002390 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002391
2392 // Write the record containing tentative definitions.
2393 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002394 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002395
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002396 // Write the record containing unused file scoped decls.
2397 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002398 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002399
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002400 // Write the record containing weak undeclared identifiers.
2401 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002402 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002403 WeakUndeclaredIdentifiers);
2404
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002405 // Write the record containing locally-scoped external definitions.
2406 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002407 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002408 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002409
2410 // Write the record containing ext_vector type names.
2411 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002412 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002413
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002414 // Write the record containing VTable uses information.
2415 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002416 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002417
2418 // Write the record containing dynamic classes declarations.
2419 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002420 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002421
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002422 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002423 if (!PendingInstantiations.empty())
2424 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002425
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002426 // Write the record containing declaration references of Sema.
2427 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002428 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002429
Douglas Gregor08f01292009-04-17 22:13:46 +00002430 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002431 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002432 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002433 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002434 Record.push_back(NumLexicalDeclContexts);
2435 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002436 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002437 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002438}
2439
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002440void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002441 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002442 using namespace llvm;
2443
Sebastian Redl07a89a82010-07-30 00:29:29 +00002444 FirstDeclID += Chain->getTotalNumDecls();
2445 FirstTypeID += Chain->getTotalNumTypes();
2446 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redld95a56e2010-08-04 18:21:41 +00002447 FirstSelectorID += Chain->getTotalNumSelectors();
Douglas Gregor91096292010-10-02 19:29:26 +00002448 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Sebastian Redl07a89a82010-07-30 00:29:29 +00002449 NextDeclID = FirstDeclID;
2450 NextTypeID = FirstTypeID;
2451 NextIdentID = FirstIdentID;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002452 NextSelectorID = FirstSelectorID;
Douglas Gregor91096292010-10-02 19:29:26 +00002453 NextMacroID = FirstMacroID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00002454
Sebastian Redl143413f2010-07-12 22:02:52 +00002455 ASTContext &Context = SemaRef.Context;
2456 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002457
Sebastian Redl143413f2010-07-12 22:02:52 +00002458 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002459 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002460 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002461 if (StatCalls && !isysroot)
2462 WriteStatCache(*StatCalls);
2463 // FIXME: Source manager block should only write new stuff, which could be
2464 // done by tracking the largest ID in the chain
2465 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002466
2467 // The special types are in the chained PCH.
2468
2469 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002470 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002471 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002472 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002473 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2474 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002475 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002476 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002477 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002478 else if ((*I)->isChangedSinceDeserialization())
2479 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002480 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002481 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002482 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002483 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002484 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2485 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2486 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002487 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002488 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2489 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002490 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Sebastian Redla4071b42010-08-24 00:50:09 +00002491 // And in C++, a visible updates block for the TU.
2492 if (Context.getLangOptions().CPlusPlus) {
2493 Abv = new llvm::BitCodeAbbrev();
2494 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2495 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2496 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2497 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2498 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2499 WriteDeclContextVisibleUpdate(TU);
2500 }
Sebastian Redl143413f2010-07-12 22:02:52 +00002501
Sebastian Redl98912122010-07-27 23:01:28 +00002502 // Build a record containing all of the new tentative definitions in this
2503 // file, in TentativeDefinitions order.
2504 RecordData TentativeDefinitions;
2505 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2506 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2507 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2508 }
2509
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002510 // Build a record containing all of the file scoped decls in this file.
2511 RecordData UnusedFileScopedDecls;
2512 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2513 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2514 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002515 }
2516
Sebastian Redl08aca90252010-08-05 18:21:25 +00002517 // We write the entire table, overwriting the tables from the chain.
2518 RecordData WeakUndeclaredIdentifiers;
2519 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2520 WeakUndeclaredIdentifiers.push_back(
2521 SemaRef.WeakUndeclaredIdentifiers.size());
2522 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2523 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2524 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2525 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2526 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2527 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2528 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2529 }
2530 }
2531
Sebastian Redl98912122010-07-27 23:01:28 +00002532 // Build a record containing all of the locally-scoped external
2533 // declarations in this header file. Generally, this record will be
2534 // empty.
2535 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002536 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002537 // nondeterminstic!
2538 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2539 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2540 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2541 TD != TDEnd; ++TD) {
2542 if (TD->second->getPCHLevel() == 0)
2543 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2544 }
2545
2546 // Build a record containing all of the ext_vector declarations.
2547 RecordData ExtVectorDecls;
2548 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2549 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2550 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2551 }
2552
Sebastian Redl08aca90252010-08-05 18:21:25 +00002553 // Build a record containing all of the VTable uses information.
2554 // We write everything here, because it's too hard to determine whether
2555 // a use is new to this part.
2556 RecordData VTableUses;
2557 if (!SemaRef.VTableUses.empty()) {
2558 VTableUses.push_back(SemaRef.VTableUses.size());
2559 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2560 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2561 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2562 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2563 }
2564 }
2565
2566 // Build a record containing all of dynamic classes declarations.
2567 RecordData DynamicClasses;
2568 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2569 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2570 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2571
2572 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002573 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002574 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002575 I = SemaRef.PendingInstantiations.begin(),
2576 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002577 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002578 AddDeclRef(I->first, PendingInstantiations);
2579 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002580 }
2581 }
2582 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2583 "There are local ones at end of translation unit!");
2584
2585 // Build a record containing some declaration references.
2586 // It's not worth the effort to avoid duplication here.
2587 RecordData SemaDeclRefs;
2588 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2589 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2590 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2591 }
2592
Sebastian Redl539c5062010-08-18 23:57:32 +00002593 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002594 WriteDeclsBlockAbbrevs();
2595 while (!DeclTypesToEmit.empty()) {
2596 DeclOrType DOT = DeclTypesToEmit.front();
2597 DeclTypesToEmit.pop();
2598 if (DOT.isType())
2599 WriteType(DOT.getType());
2600 else
2601 WriteDecl(Context, DOT.getDecl());
2602 }
2603 Stream.ExitBlock();
2604
Sebastian Redl98912122010-07-27 23:01:28 +00002605 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002606 WriteSelectors(SemaRef);
2607 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002608 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002609 WriteTypeDeclOffsets();
Sebastian Redl98912122010-07-27 23:01:28 +00002610
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002611 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002612 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002613 RecordData FirstLatestDeclIDs;
2614 for (FirstLatestDeclMap::iterator
2615 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2616 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2617 "Expected first & second to be in different PCHs");
2618 AddDeclRef(I->first, FirstLatestDeclIDs);
2619 AddDeclRef(I->second, FirstLatestDeclIDs);
2620 }
2621 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002622 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002623
Sebastian Redl98912122010-07-27 23:01:28 +00002624 // Write the record containing external, unnamed definitions.
2625 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002626 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002627
2628 // Write the record containing tentative definitions.
2629 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002630 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002631
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002632 // Write the record containing unused file scoped decls.
2633 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002634 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002635
Sebastian Redl08aca90252010-08-05 18:21:25 +00002636 // Write the record containing weak undeclared identifiers.
2637 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002638 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002639 WeakUndeclaredIdentifiers);
2640
Sebastian Redl98912122010-07-27 23:01:28 +00002641 // Write the record containing locally-scoped external definitions.
2642 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002643 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002644 LocallyScopedExternalDecls);
2645
2646 // Write the record containing ext_vector type names.
2647 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002648 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002649
Sebastian Redl08aca90252010-08-05 18:21:25 +00002650 // Write the record containing VTable uses information.
2651 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002652 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002653
2654 // Write the record containing dynamic classes declarations.
2655 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002656 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002657
2658 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002659 if (!PendingInstantiations.empty())
2660 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002661
2662 // Write the record containing declaration references of Sema.
2663 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002664 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002665
Sebastian Redla4071b42010-08-24 00:50:09 +00002666 // Write the updates to C++ namespaces.
2667 for (llvm::SmallPtrSet<const NamespaceDecl *, 16>::iterator
2668 I = UpdatedNamespaces.begin(),
2669 E = UpdatedNamespaces.end();
2670 I != E; ++I)
2671 WriteDeclContextVisibleUpdate(*I);
2672
Sebastian Redl401b39a2010-08-24 22:50:24 +00002673 // Write the updates to C++ template specialization lists.
2674 if (!AdditionalTemplateSpecializations.empty())
2675 WriteAdditionalTemplateSpecializations();
2676
Sebastian Redl98912122010-07-27 23:01:28 +00002677 Record.clear();
2678 Record.push_back(NumStatements);
2679 Record.push_back(NumMacros);
2680 Record.push_back(NumLexicalDeclContexts);
2681 Record.push_back(NumVisibleDeclContexts);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002682 WriteDeclUpdateBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002683 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002684 Stream.ExitBlock();
2685}
2686
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002687void ASTWriter::WriteDeclUpdateBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002688 if (ReplacedDecls.empty())
2689 return;
2690
2691 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002692 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002693 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2694 Record.push_back(I->first);
2695 Record.push_back(I->second);
2696 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002697 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002698}
2699
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002700void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002701 Record.push_back(Loc.getRawEncoding());
2702}
2703
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002704void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002705 AddSourceLocation(Range.getBegin(), Record);
2706 AddSourceLocation(Range.getEnd(), Record);
2707}
2708
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002709void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002710 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002711 const uint64_t *Words = Value.getRawData();
2712 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002713}
2714
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002715void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002716 Record.push_back(Value.isUnsigned());
2717 AddAPInt(Value, Record);
2718}
2719
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002720void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002721 AddAPInt(Value.bitcastToAPInt(), Record);
2722}
2723
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002724void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002725 Record.push_back(getIdentifierRef(II));
2726}
2727
Sebastian Redl539c5062010-08-18 23:57:32 +00002728IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002729 if (II == 0)
2730 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002731
Sebastian Redl539c5062010-08-18 23:57:32 +00002732 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002733 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002734 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002735 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002736}
2737
Sebastian Redl50e26582010-09-15 19:54:06 +00002738MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002739 if (MD == 0)
2740 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002741
2742 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002743 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002744 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002745 return ID;
2746}
2747
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002748void ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002749 Record.push_back(getSelectorRef(SelRef));
2750}
2751
Sebastian Redl539c5062010-08-18 23:57:32 +00002752SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002753 if (Sel.getAsOpaquePtr() == 0) {
2754 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002755 }
2756
Sebastian Redl539c5062010-08-18 23:57:32 +00002757 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002758 if (SID == 0 && Chain) {
2759 // This might trigger a ReadSelector callback, which will set the ID for
2760 // this selector.
2761 Chain->LoadSelector(Sel);
2762 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002763 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002764 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002765 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002766 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002767}
2768
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002769void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002770 AddDeclRef(Temp->getDestructor(), Record);
2771}
2772
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002773void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002774 const TemplateArgumentLocInfo &Arg,
2775 RecordData &Record) {
2776 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002777 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002778 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002779 break;
2780 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002781 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002782 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002783 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002784 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2785 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002786 break;
John McCall0ad16662009-10-29 08:12:44 +00002787 case TemplateArgument::Null:
2788 case TemplateArgument::Integral:
2789 case TemplateArgument::Declaration:
2790 case TemplateArgument::Pack:
2791 break;
2792 }
2793}
2794
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002795void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002796 RecordData &Record) {
2797 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002798
2799 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2800 bool InfoHasSameExpr
2801 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2802 Record.push_back(InfoHasSameExpr);
2803 if (InfoHasSameExpr)
2804 return; // Avoid storing the same expr twice.
2805 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002806 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2807 Record);
2808}
2809
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002810void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002811 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002812 AddTypeRef(QualType(), Record);
2813 return;
2814 }
2815
John McCallbcd03502009-12-07 02:54:59 +00002816 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002817 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002818 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002819 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002820}
2821
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002822void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002823 Record.push_back(GetOrCreateTypeID(T));
2824}
2825
2826TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002827 return MakeTypeID(T,
2828 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2829}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002830
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002831TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002832 return MakeTypeID(T,
2833 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002834}
2835
2836TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2837 if (T.isNull())
2838 return TypeIdx();
2839 assert(!T.getLocalFastQualifiers());
2840
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002841 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002842 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002843 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002844 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002845 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002846 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002847 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002848 return Idx;
2849}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002850
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002851TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002852 if (T.isNull())
2853 return TypeIdx();
2854 assert(!T.getLocalFastQualifiers());
2855
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002856 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2857 assert(I != TypeIdxs.end() && "Type not emitted!");
2858 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002859}
2860
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002861void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002862 Record.push_back(GetDeclRef(D));
2863}
2864
Sebastian Redl539c5062010-08-18 23:57:32 +00002865DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002866 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002867 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002868 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002869 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00002870 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002871 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002872 // We haven't seen this declaration before. Give it a new ID and
2873 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002874 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002875 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002876 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2877 // We don't add it to the replacement collection here, because we don't
2878 // have the offset yet.
2879 DeclTypesToEmit.push(const_cast<Decl *>(D));
2880 // Reset the flag, so that we don't add this decl multiple times.
2881 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002882 }
2883
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002884 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002885}
2886
Sebastian Redl539c5062010-08-18 23:57:32 +00002887DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002888 if (D == 0)
2889 return 0;
2890
2891 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2892 return DeclIDs[D];
2893}
2894
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002895void ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002896 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002897 Record.push_back(Name.getNameKind());
2898 switch (Name.getNameKind()) {
2899 case DeclarationName::Identifier:
2900 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2901 break;
2902
2903 case DeclarationName::ObjCZeroArgSelector:
2904 case DeclarationName::ObjCOneArgSelector:
2905 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002906 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002907 break;
2908
2909 case DeclarationName::CXXConstructorName:
2910 case DeclarationName::CXXDestructorName:
2911 case DeclarationName::CXXConversionFunctionName:
2912 AddTypeRef(Name.getCXXNameType(), Record);
2913 break;
2914
2915 case DeclarationName::CXXOperatorName:
2916 Record.push_back(Name.getCXXOverloadedOperator());
2917 break;
2918
Alexis Hunt3d221f22009-11-29 07:34:05 +00002919 case DeclarationName::CXXLiteralOperatorName:
2920 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2921 break;
2922
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002923 case DeclarationName::CXXUsingDirective:
2924 // No extra data to emit
2925 break;
2926 }
2927}
Chris Lattnerca025db2010-05-07 21:43:38 +00002928
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00002929void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
2930 DeclarationName Name, RecordData &Record) {
2931 switch (Name.getNameKind()) {
2932 case DeclarationName::CXXConstructorName:
2933 case DeclarationName::CXXDestructorName:
2934 case DeclarationName::CXXConversionFunctionName:
2935 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
2936 break;
2937
2938 case DeclarationName::CXXOperatorName:
2939 AddSourceLocation(
2940 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
2941 Record);
2942 AddSourceLocation(
2943 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
2944 Record);
2945 break;
2946
2947 case DeclarationName::CXXLiteralOperatorName:
2948 AddSourceLocation(
2949 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
2950 Record);
2951 break;
2952
2953 case DeclarationName::Identifier:
2954 case DeclarationName::ObjCZeroArgSelector:
2955 case DeclarationName::ObjCOneArgSelector:
2956 case DeclarationName::ObjCMultiArgSelector:
2957 case DeclarationName::CXXUsingDirective:
2958 break;
2959 }
2960}
2961
2962void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2963 RecordData &Record) {
2964 AddDeclarationName(NameInfo.getName(), Record);
2965 AddSourceLocation(NameInfo.getLoc(), Record);
2966 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
2967}
2968
2969void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
2970 RecordData &Record) {
2971 AddNestedNameSpecifier(Info.NNS, Record);
2972 AddSourceRange(Info.NNSRange, Record);
2973 Record.push_back(Info.NumTemplParamLists);
2974 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
2975 AddTemplateParameterList(Info.TemplParamLists[i], Record);
2976}
2977
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002978void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Chris Lattnerca025db2010-05-07 21:43:38 +00002979 RecordData &Record) {
2980 // Nested name specifiers usually aren't too long. I think that 8 would
2981 // typically accomodate the vast majority.
2982 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2983
2984 // Push each of the NNS's onto a stack for serialization in reverse order.
2985 while (NNS) {
2986 NestedNames.push_back(NNS);
2987 NNS = NNS->getPrefix();
2988 }
2989
2990 Record.push_back(NestedNames.size());
2991 while(!NestedNames.empty()) {
2992 NNS = NestedNames.pop_back_val();
2993 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2994 Record.push_back(Kind);
2995 switch (Kind) {
2996 case NestedNameSpecifier::Identifier:
2997 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2998 break;
2999
3000 case NestedNameSpecifier::Namespace:
3001 AddDeclRef(NNS->getAsNamespace(), Record);
3002 break;
3003
3004 case NestedNameSpecifier::TypeSpec:
3005 case NestedNameSpecifier::TypeSpecWithTemplate:
3006 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3007 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3008 break;
3009
3010 case NestedNameSpecifier::Global:
3011 // Don't need to write an associated value.
3012 break;
3013 }
3014 }
3015}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003016
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003017void ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003018 TemplateName::NameKind Kind = Name.getKind();
3019 Record.push_back(Kind);
3020 switch (Kind) {
3021 case TemplateName::Template:
3022 AddDeclRef(Name.getAsTemplateDecl(), Record);
3023 break;
3024
3025 case TemplateName::OverloadedTemplate: {
3026 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3027 Record.push_back(OvT->size());
3028 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3029 I != E; ++I)
3030 AddDeclRef(*I, Record);
3031 break;
3032 }
3033
3034 case TemplateName::QualifiedTemplate: {
3035 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3036 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3037 Record.push_back(QualT->hasTemplateKeyword());
3038 AddDeclRef(QualT->getTemplateDecl(), Record);
3039 break;
3040 }
3041
3042 case TemplateName::DependentTemplate: {
3043 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3044 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3045 Record.push_back(DepT->isIdentifier());
3046 if (DepT->isIdentifier())
3047 AddIdentifierRef(DepT->getIdentifier(), Record);
3048 else
3049 Record.push_back(DepT->getOperator());
3050 break;
3051 }
3052 }
3053}
3054
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003055void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003056 RecordData &Record) {
3057 Record.push_back(Arg.getKind());
3058 switch (Arg.getKind()) {
3059 case TemplateArgument::Null:
3060 break;
3061 case TemplateArgument::Type:
3062 AddTypeRef(Arg.getAsType(), Record);
3063 break;
3064 case TemplateArgument::Declaration:
3065 AddDeclRef(Arg.getAsDecl(), Record);
3066 break;
3067 case TemplateArgument::Integral:
3068 AddAPSInt(*Arg.getAsIntegral(), Record);
3069 AddTypeRef(Arg.getIntegralType(), Record);
3070 break;
3071 case TemplateArgument::Template:
3072 AddTemplateName(Arg.getAsTemplate(), Record);
3073 break;
3074 case TemplateArgument::Expression:
3075 AddStmt(Arg.getAsExpr());
3076 break;
3077 case TemplateArgument::Pack:
3078 Record.push_back(Arg.pack_size());
3079 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3080 I != E; ++I)
3081 AddTemplateArgument(*I, Record);
3082 break;
3083 }
3084}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003085
3086void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003087ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003088 RecordData &Record) {
3089 assert(TemplateParams && "No TemplateParams!");
3090 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3091 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3092 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3093 Record.push_back(TemplateParams->size());
3094 for (TemplateParameterList::const_iterator
3095 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3096 P != PEnd; ++P)
3097 AddDeclRef(*P, Record);
3098}
3099
3100/// \brief Emit a template argument list.
3101void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003102ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003103 RecordData &Record) {
3104 assert(TemplateArgs && "No TemplateArgs!");
3105 Record.push_back(TemplateArgs->flat_size());
3106 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
3107 AddTemplateArgument(TemplateArgs->get(i), Record);
3108}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003109
3110
3111void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003112ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003113 Record.push_back(Set.size());
3114 for (UnresolvedSetImpl::const_iterator
3115 I = Set.begin(), E = Set.end(); I != E; ++I) {
3116 AddDeclRef(I.getDecl(), Record);
3117 Record.push_back(I.getAccess());
3118 }
3119}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003120
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003121void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003122 RecordData &Record) {
3123 Record.push_back(Base.isVirtual());
3124 Record.push_back(Base.isBaseOfClass());
3125 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003126 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003127 AddSourceRange(Base.getSourceRange(), Record);
3128}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003129
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003130void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003131 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
3132 unsigned NumBaseOrMembers, RecordData &Record) {
3133 Record.push_back(NumBaseOrMembers);
3134 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3135 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3136
3137 Record.push_back(Init->isBaseInitializer());
3138 if (Init->isBaseInitializer()) {
3139 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3140 Record.push_back(Init->isBaseVirtual());
3141 } else {
3142 AddDeclRef(Init->getMember(), Record);
3143 }
3144 AddSourceLocation(Init->getMemberLocation(), Record);
3145 AddStmt(Init->getInit());
3146 AddDeclRef(Init->getAnonUnionMember(), Record);
3147 AddSourceLocation(Init->getLParenLoc(), Record);
3148 AddSourceLocation(Init->getRParenLoc(), Record);
3149 Record.push_back(Init->isWritten());
3150 if (Init->isWritten()) {
3151 Record.push_back(Init->getSourceOrder());
3152 } else {
3153 Record.push_back(Init->getNumArrayIndices());
3154 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3155 AddDeclRef(Init->getArrayIndex(i), Record);
3156 }
3157 }
3158}
3159
Sebastian Redl2c499f62010-08-18 23:56:43 +00003160void ASTWriter::SetReader(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003161 assert(Reader && "Cannot remove chain");
3162 assert(FirstDeclID == NextDeclID &&
3163 FirstTypeID == NextTypeID &&
3164 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003165 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003166 FirstMacroID == NextMacroID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003167 "Setting chain after writing has started.");
3168 Chain = Reader;
3169}
3170
Sebastian Redl539c5062010-08-18 23:57:32 +00003171void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003172 IdentifierIDs[II] = ID;
3173}
3174
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003175void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003176 // Always take the highest-numbered type index. This copes with an interesting
3177 // case for chained AST writing where we schedule writing the type and then,
3178 // later, deserialize the type from another AST. In this case, we want to
3179 // keep the higher-numbered entry so that we can properly write it out to
3180 // the AST file.
3181 TypeIdx &StoredIdx = TypeIdxs[T];
3182 if (Idx.getIndex() >= StoredIdx.getIndex())
3183 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003184}
3185
Sebastian Redl539c5062010-08-18 23:57:32 +00003186void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003187 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003188}
Sebastian Redl834bb972010-08-04 17:20:04 +00003189
Sebastian Redl539c5062010-08-18 23:57:32 +00003190void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003191 SelectorIDs[S] = ID;
3192}
Douglas Gregor91096292010-10-02 19:29:26 +00003193
3194void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
3195 MacroDefinition *MD) {
3196 MacroDefinitions[MD] = ID;
3197}