blob: a5977f5cfe8d9592119e3b89c9ec4bf80f410e35 [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"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000033#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000035#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000036#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000037#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000038#include "llvm/ADT/APFloat.h"
39#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000040#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000041#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000042#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor45fe0362009-05-12 01:31:05 +000043#include "llvm/System/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000044#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000045using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000046using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047
Sebastian Redl3df5a082010-07-30 17:03:48 +000048template <typename T, typename Allocator>
49T *data(std::vector<T, Allocator> &v) {
50 return v.empty() ? 0 : &v.front();
51}
52template <typename T, typename Allocator>
53const T *data(const std::vector<T, Allocator> &v) {
54 return v.empty() ? 0 : &v.front();
55}
56
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057//===----------------------------------------------------------------------===//
58// Type serialization
59//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000060
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000062 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000063 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000064 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000065
66 public:
67 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000068 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000070 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000071 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072
73 void VisitArrayType(const ArrayType *T);
74 void VisitFunctionType(const FunctionType *T);
75 void VisitTagType(const TagType *T);
76
77#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
78#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079#include "clang/AST/TypeNodes.def"
80 };
81}
82
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000083void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084 assert(false && "Built-in types are never serialized");
85}
86
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000087void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000089 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090}
91
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000092void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000094 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000099 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100}
101
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000104 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000113 Writer.AddTypeRef(T->getPointeeType(), Record);
114 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000115 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116}
117
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119 Writer.AddTypeRef(T->getElementType(), Record);
120 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000121 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122}
123
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125 VisitArrayType(T);
126 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000127 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128}
129
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000132 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133}
134
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000137 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
138 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000139 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000140 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000141}
142
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000143void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144 Writer.AddTypeRef(T->getElementType(), Record);
145 Record.push_back(T->getNumElements());
Chris Lattner37141f42010-06-23 06:00:24 +0000146 Record.push_back(T->getAltiVecSpecific());
Sebastian Redl539c5062010-08-18 23:57:32 +0000147 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148}
149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000152 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153}
154
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000157 FunctionType::ExtInfo C = T->getExtInfo();
158 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000159 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000160 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000161 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162}
163
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000166 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167}
168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170 VisitFunctionType(T);
171 Record.push_back(T->getNumArgs());
172 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
173 Writer.AddTypeRef(T->getArgType(I), Record);
174 Record.push_back(T->isVariadic());
175 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000176 Record.push_back(T->hasExceptionSpec());
177 Record.push_back(T->hasAnyExceptionSpec());
178 Record.push_back(T->getNumExceptions());
179 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
180 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000181 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000182}
183
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000184void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000185 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000186 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000187}
John McCallb96ec562009-12-04 22:46:56 +0000188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000191 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
192 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000193 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000194}
195
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000196void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000197 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000198 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000207 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000212 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000214 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000215 "Cannot serialize in the middle of a type definition");
216}
217
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000220 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000221}
222
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000225 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226}
227
Mike Stump11289f42009-09-09 15:08:12 +0000228void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000229ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000230 const SubstTemplateTypeParmType *T) {
231 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
232 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000233 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000234}
235
236void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000237ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000238 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000239 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000240 Writer.AddTemplateName(T->getTemplateName(), Record);
241 Record.push_back(T->getNumArgs());
242 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
243 ArgI != ArgE; ++ArgI)
244 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000245 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
246 : T->getCanonicalTypeInternal(),
247 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000248 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000249}
250
251void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000252ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000253 VisitArrayType(T);
254 Writer.AddStmt(T->getSizeExpr());
255 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000256 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000257}
258
259void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000261 const DependentSizedExtVectorType *T) {
262 // FIXME: Serialize this type (C++ only)
263 assert(false && "Cannot serialize dependent sized extended vector types");
264}
265
266void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000267ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000268 Record.push_back(T->getDepth());
269 Record.push_back(T->getIndex());
270 Record.push_back(T->isParameterPack());
271 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000272 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000273}
274
275void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000276ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000277 Record.push_back(T->getKeyword());
278 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
279 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000280 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
281 : T->getCanonicalTypeInternal(),
282 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000283 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000284}
285
286void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000287ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000288 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000289 Record.push_back(T->getKeyword());
290 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
291 Writer.AddIdentifierRef(T->getIdentifier(), Record);
292 Record.push_back(T->getNumArgs());
293 for (DependentTemplateSpecializationType::iterator
294 I = T->begin(), E = T->end(); I != E; ++I)
295 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000296 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000297}
298
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000299void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000300 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000301 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
302 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000303 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000304}
305
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000306void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000307 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000308 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000309 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000310}
311
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000313 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000314 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000315}
316
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000317void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000318 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000319 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000320 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000321 E = T->qual_end(); I != E; ++I)
322 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000323 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000324}
325
Steve Narofffb4330f2009-06-17 22:40:22 +0000326void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000327ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000328 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000329 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000330}
331
John McCall8f115c62009-10-16 21:56:05 +0000332namespace {
333
334class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000335 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000336 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000337
338public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000339 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000340 : Writer(Writer), Record(Record) { }
341
John McCall17001972009-10-18 01:05:36 +0000342#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000343#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000344 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000345#include "clang/AST/TypeLocNodes.def"
346
John McCall17001972009-10-18 01:05:36 +0000347 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
348 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000349};
350
351}
352
John McCall17001972009-10-18 01:05:36 +0000353void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
354 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000355}
John McCall17001972009-10-18 01:05:36 +0000356void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000357 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
358 if (TL.needsExtraLocalData()) {
359 Record.push_back(TL.getWrittenTypeSpec());
360 Record.push_back(TL.getWrittenSignSpec());
361 Record.push_back(TL.getWrittenWidthSpec());
362 Record.push_back(TL.hasModeAttr());
363 }
John McCall8f115c62009-10-16 21:56:05 +0000364}
John McCall17001972009-10-18 01:05:36 +0000365void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
366 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000367}
John McCall17001972009-10-18 01:05:36 +0000368void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
369 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000370}
John McCall17001972009-10-18 01:05:36 +0000371void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
372 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000373}
John McCall17001972009-10-18 01:05:36 +0000374void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
375 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000376}
John McCall17001972009-10-18 01:05:36 +0000377void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000379}
John McCall17001972009-10-18 01:05:36 +0000380void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
381 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000382}
John McCall17001972009-10-18 01:05:36 +0000383void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
384 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
385 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
386 Record.push_back(TL.getSizeExpr() ? 1 : 0);
387 if (TL.getSizeExpr())
388 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000389}
John McCall17001972009-10-18 01:05:36 +0000390void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
391 VisitArrayTypeLoc(TL);
392}
393void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
394 VisitArrayTypeLoc(TL);
395}
396void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
397 VisitArrayTypeLoc(TL);
398}
399void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
400 DependentSizedArrayTypeLoc TL) {
401 VisitArrayTypeLoc(TL);
402}
403void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
404 DependentSizedExtVectorTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getNameLoc(), Record);
406}
407void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getNameLoc(), Record);
409}
410void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getNameLoc(), Record);
412}
413void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
415 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000416 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000417 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
418 Writer.AddDeclRef(TL.getArg(i), Record);
419}
420void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
421 VisitFunctionTypeLoc(TL);
422}
423void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
424 VisitFunctionTypeLoc(TL);
425}
John McCallb96ec562009-12-04 22:46:56 +0000426void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getNameLoc(), Record);
428}
John McCall17001972009-10-18 01:05:36 +0000429void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
430 Writer.AddSourceLocation(TL.getNameLoc(), Record);
431}
432void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000433 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
434 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
435 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000436}
437void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000438 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
439 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
440 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
441 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000442}
443void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
446void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
449void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
451}
John McCall17001972009-10-18 01:05:36 +0000452void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getNameLoc(), Record);
454}
John McCallcebee162009-10-18 09:09:24 +0000455void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
456 SubstTemplateTypeParmTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getNameLoc(), Record);
458}
John McCall17001972009-10-18 01:05:36 +0000459void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
460 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000461 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
462 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
463 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
464 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000465 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
466 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000467}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000468void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000469 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
470 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000471}
John McCalle78aac42010-03-10 03:28:59 +0000472void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
473 Writer.AddSourceLocation(TL.getNameLoc(), Record);
474}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000475void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000476 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
477 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
John McCallc392f372010-06-11 00:33:02 +0000480void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
481 DependentTemplateSpecializationTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
483 Writer.AddSourceRange(TL.getQualifierRange(), Record);
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
486 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
487 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000488 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
489 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000490}
John McCall17001972009-10-18 01:05:36 +0000491void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
492 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000493}
494void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
495 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000496 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
497 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
498 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
499 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000500}
John McCallfc93cf92009-10-22 22:37:11 +0000501void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000503}
John McCall8f115c62009-10-16 21:56:05 +0000504
Chris Lattner19cea4e2009-04-22 05:57:30 +0000505//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000506// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000507//===----------------------------------------------------------------------===//
508
Chris Lattner28fa4e62009-04-26 22:26:21 +0000509static void EmitBlockID(unsigned ID, const char *Name,
510 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000511 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000512 Record.clear();
513 Record.push_back(ID);
514 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
515
516 // Emit the block name if present.
517 if (Name == 0 || Name[0] == 0) return;
518 Record.clear();
519 while (*Name)
520 Record.push_back(*Name++);
521 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
522}
523
524static void EmitRecordID(unsigned ID, const char *Name,
525 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000526 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000527 Record.clear();
528 Record.push_back(ID);
529 while (*Name)
530 Record.push_back(*Name++);
531 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000532}
533
534static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000535 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000536#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000537 RECORD(STMT_STOP);
538 RECORD(STMT_NULL_PTR);
539 RECORD(STMT_NULL);
540 RECORD(STMT_COMPOUND);
541 RECORD(STMT_CASE);
542 RECORD(STMT_DEFAULT);
543 RECORD(STMT_LABEL);
544 RECORD(STMT_IF);
545 RECORD(STMT_SWITCH);
546 RECORD(STMT_WHILE);
547 RECORD(STMT_DO);
548 RECORD(STMT_FOR);
549 RECORD(STMT_GOTO);
550 RECORD(STMT_INDIRECT_GOTO);
551 RECORD(STMT_CONTINUE);
552 RECORD(STMT_BREAK);
553 RECORD(STMT_RETURN);
554 RECORD(STMT_DECL);
555 RECORD(STMT_ASM);
556 RECORD(EXPR_PREDEFINED);
557 RECORD(EXPR_DECL_REF);
558 RECORD(EXPR_INTEGER_LITERAL);
559 RECORD(EXPR_FLOATING_LITERAL);
560 RECORD(EXPR_IMAGINARY_LITERAL);
561 RECORD(EXPR_STRING_LITERAL);
562 RECORD(EXPR_CHARACTER_LITERAL);
563 RECORD(EXPR_PAREN);
564 RECORD(EXPR_UNARY_OPERATOR);
565 RECORD(EXPR_SIZEOF_ALIGN_OF);
566 RECORD(EXPR_ARRAY_SUBSCRIPT);
567 RECORD(EXPR_CALL);
568 RECORD(EXPR_MEMBER);
569 RECORD(EXPR_BINARY_OPERATOR);
570 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
571 RECORD(EXPR_CONDITIONAL_OPERATOR);
572 RECORD(EXPR_IMPLICIT_CAST);
573 RECORD(EXPR_CSTYLE_CAST);
574 RECORD(EXPR_COMPOUND_LITERAL);
575 RECORD(EXPR_EXT_VECTOR_ELEMENT);
576 RECORD(EXPR_INIT_LIST);
577 RECORD(EXPR_DESIGNATED_INIT);
578 RECORD(EXPR_IMPLICIT_VALUE_INIT);
579 RECORD(EXPR_VA_ARG);
580 RECORD(EXPR_ADDR_LABEL);
581 RECORD(EXPR_STMT);
582 RECORD(EXPR_TYPES_COMPATIBLE);
583 RECORD(EXPR_CHOOSE);
584 RECORD(EXPR_GNU_NULL);
585 RECORD(EXPR_SHUFFLE_VECTOR);
586 RECORD(EXPR_BLOCK);
587 RECORD(EXPR_BLOCK_DECL_REF);
588 RECORD(EXPR_OBJC_STRING_LITERAL);
589 RECORD(EXPR_OBJC_ENCODE);
590 RECORD(EXPR_OBJC_SELECTOR_EXPR);
591 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
592 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
593 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
594 RECORD(EXPR_OBJC_KVC_REF_EXPR);
595 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000596 RECORD(STMT_OBJC_FOR_COLLECTION);
597 RECORD(STMT_OBJC_CATCH);
598 RECORD(STMT_OBJC_FINALLY);
599 RECORD(STMT_OBJC_AT_TRY);
600 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
601 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000602 RECORD(EXPR_CXX_OPERATOR_CALL);
603 RECORD(EXPR_CXX_CONSTRUCT);
604 RECORD(EXPR_CXX_STATIC_CAST);
605 RECORD(EXPR_CXX_DYNAMIC_CAST);
606 RECORD(EXPR_CXX_REINTERPRET_CAST);
607 RECORD(EXPR_CXX_CONST_CAST);
608 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
609 RECORD(EXPR_CXX_BOOL_LITERAL);
610 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000611#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000612}
Mike Stump11289f42009-09-09 15:08:12 +0000613
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000614void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000615 RecordData Record;
616 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000617
Sebastian Redl539c5062010-08-18 23:57:32 +0000618#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
619#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000620
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000621 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000622 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000623 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000624 RECORD(TYPE_OFFSET);
625 RECORD(DECL_OFFSET);
626 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000627 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000628 RECORD(IDENTIFIER_OFFSET);
629 RECORD(IDENTIFIER_TABLE);
630 RECORD(EXTERNAL_DEFINITIONS);
631 RECORD(SPECIAL_TYPES);
632 RECORD(STATISTICS);
633 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000634 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000635 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
636 RECORD(SELECTOR_OFFSETS);
637 RECORD(METHOD_POOL);
638 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000639 RECORD(SOURCE_LOCATION_OFFSETS);
640 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000641 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000642 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000643 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000644 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000645 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000646 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000647
Chris Lattner28fa4e62009-04-26 22:26:21 +0000648 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000649 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000650 RECORD(SM_SLOC_FILE_ENTRY);
651 RECORD(SM_SLOC_BUFFER_ENTRY);
652 RECORD(SM_SLOC_BUFFER_BLOB);
653 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
654 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000655
Chris Lattner28fa4e62009-04-26 22:26:21 +0000656 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000657 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000658 RECORD(PP_MACRO_OBJECT_LIKE);
659 RECORD(PP_MACRO_FUNCTION_LIKE);
660 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000661 RECORD(PP_MACRO_INSTANTIATION);
662 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000663
Douglas Gregor12bfa382009-10-17 00:13:19 +0000664 // Decls and Types block.
665 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000666 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667 RECORD(TYPE_COMPLEX);
668 RECORD(TYPE_POINTER);
669 RECORD(TYPE_BLOCK_POINTER);
670 RECORD(TYPE_LVALUE_REFERENCE);
671 RECORD(TYPE_RVALUE_REFERENCE);
672 RECORD(TYPE_MEMBER_POINTER);
673 RECORD(TYPE_CONSTANT_ARRAY);
674 RECORD(TYPE_INCOMPLETE_ARRAY);
675 RECORD(TYPE_VARIABLE_ARRAY);
676 RECORD(TYPE_VECTOR);
677 RECORD(TYPE_EXT_VECTOR);
678 RECORD(TYPE_FUNCTION_PROTO);
679 RECORD(TYPE_FUNCTION_NO_PROTO);
680 RECORD(TYPE_TYPEDEF);
681 RECORD(TYPE_TYPEOF_EXPR);
682 RECORD(TYPE_TYPEOF);
683 RECORD(TYPE_RECORD);
684 RECORD(TYPE_ENUM);
685 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000686 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000687 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000688 RECORD(DECL_TRANSLATION_UNIT);
689 RECORD(DECL_TYPEDEF);
690 RECORD(DECL_ENUM);
691 RECORD(DECL_RECORD);
692 RECORD(DECL_ENUM_CONSTANT);
693 RECORD(DECL_FUNCTION);
694 RECORD(DECL_OBJC_METHOD);
695 RECORD(DECL_OBJC_INTERFACE);
696 RECORD(DECL_OBJC_PROTOCOL);
697 RECORD(DECL_OBJC_IVAR);
698 RECORD(DECL_OBJC_AT_DEFS_FIELD);
699 RECORD(DECL_OBJC_CLASS);
700 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
701 RECORD(DECL_OBJC_CATEGORY);
702 RECORD(DECL_OBJC_CATEGORY_IMPL);
703 RECORD(DECL_OBJC_IMPLEMENTATION);
704 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
705 RECORD(DECL_OBJC_PROPERTY);
706 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000707 RECORD(DECL_FIELD);
708 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000709 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000710 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000711 RECORD(DECL_FILE_SCOPE_ASM);
712 RECORD(DECL_BLOCK);
713 RECORD(DECL_CONTEXT_LEXICAL);
714 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000715 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000716 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000717#undef RECORD
718#undef BLOCK
719 Stream.ExitBlock();
720}
721
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000722/// \brief Adjusts the given filename to only write out the portion of the
723/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000724///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000725/// \param Filename the file name to adjust.
726///
727/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
728/// the returned filename will be adjusted by this system root.
729///
730/// \returns either the original filename (if it needs no adjustment) or the
731/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000732static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000733adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
734 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000735
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000736 if (!isysroot)
737 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000738
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000739 // Verify that the filename and the system root have the same prefix.
740 unsigned Pos = 0;
741 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
742 if (Filename[Pos] != isysroot[Pos])
743 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000744
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000745 // We hit the end of the filename before we hit the end of the system root.
746 if (!Filename[Pos])
747 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000748
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000749 // If the file name has a '/' at the current position, skip over the '/'.
750 // We distinguish sysroot-based includes from absolute includes by the
751 // absence of '/' at the beginning of sysroot-based includes.
752 if (Filename[Pos] == '/')
753 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000754
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000755 return Filename + Pos;
756}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000757
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000758/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000759void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000760 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000761
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000762 // Metadata
763 const TargetInfo &Target = Context.Target;
764 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000765 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000766 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
771 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000772 // Target triple or chained PCH name
773 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000774 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000775
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000776 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000777 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
778 Record.push_back(VERSION_MAJOR);
779 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000780 Record.push_back(CLANG_VERSION_MAJOR);
781 Record.push_back(CLANG_VERSION_MINOR);
782 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000783 // FIXME: This writes the absolute path for chained headers.
784 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
785 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000786
Douglas Gregor45fe0362009-05-12 01:31:05 +0000787 // Original file name
788 SourceManager &SM = Context.getSourceManager();
789 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
790 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000791 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000792 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
793 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
794
795 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000796
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000797 MainFilePath.makeAbsolute();
Douglas Gregor45fe0362009-05-12 01:31:05 +0000798
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000799 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000800 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000801 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000802 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000803 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000804 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000805 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000806
Ted Kremenek18e066f2010-01-22 22:12:47 +0000807 // Repository branch/version information.
808 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000809 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000810 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
811 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000812 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000813 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000814 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
815 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000816}
817
818/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000819void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000820 RecordData Record;
821 Record.push_back(LangOpts.Trigraphs);
822 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
823 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
824 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
825 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000826 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000827 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
828 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
829 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
830 Record.push_back(LangOpts.C99); // C99 Support
831 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000832 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
833 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000834 Record.push_back(LangOpts.CPlusPlus); // C++ Support
835 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000836 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000837
Douglas Gregor55abb232009-04-10 20:39:37 +0000838 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
839 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000840 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000841 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000842 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000843 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000844 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000845
Douglas Gregor55abb232009-04-10 20:39:37 +0000846 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000847 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
848 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000849 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000850 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000851 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000852
853 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
854 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
855 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
856
Chris Lattner258172e2009-04-27 07:35:58 +0000857 // Whether static initializers are protected by locks.
858 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000859 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000860 Record.push_back(LangOpts.Blocks); // block extension to C
861 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
862 // they are unused.
863 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
864 // (modulo the platform support).
865
Chris Lattner51924e512010-06-26 21:25:03 +0000866 Record.push_back(LangOpts.getSignedOverflowBehavior());
867 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000868
869 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000870 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000871 // defined.
872 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
873 // opposed to __DYNAMIC__).
874 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
875
876 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
877 // used (instead of C99 semantics).
878 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000879 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
880 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000881 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
882 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000883 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000884 Record.push_back(LangOpts.getGCMode());
885 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000886 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000887 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000888 Record.push_back(LangOpts.OpenCL);
Mike Stumpd9546382009-12-12 01:27:46 +0000889 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000890 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000891 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000892 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000893}
894
Douglas Gregora7f71a92009-04-10 03:52:48 +0000895//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000896// stat cache Serialization
897//===----------------------------------------------------------------------===//
898
899namespace {
900// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000901class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000902public:
903 typedef const char * key_type;
904 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000905
Douglas Gregorc5046832009-04-27 18:38:38 +0000906 typedef std::pair<int, struct stat> data_type;
907 typedef const data_type& data_type_ref;
908
909 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000910 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000911 }
Mike Stump11289f42009-09-09 15:08:12 +0000912
913 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000914 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
915 data_type_ref Data) {
916 unsigned StrLen = strlen(path);
917 clang::io::Emit16(Out, StrLen);
918 unsigned DataLen = 1; // result value
919 if (Data.first == 0)
920 DataLen += 4 + 4 + 2 + 8 + 8;
921 clang::io::Emit8(Out, DataLen);
922 return std::make_pair(StrLen + 1, DataLen);
923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
Douglas Gregorc5046832009-04-27 18:38:38 +0000925 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
926 Out.write(path, KeyLen);
927 }
Mike Stump11289f42009-09-09 15:08:12 +0000928
Douglas Gregorc5046832009-04-27 18:38:38 +0000929 void EmitData(llvm::raw_ostream& Out, key_type_ref,
930 data_type_ref Data, unsigned DataLen) {
931 using namespace clang::io;
932 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000933
Douglas Gregorc5046832009-04-27 18:38:38 +0000934 // Result of stat()
935 Emit8(Out, Data.first? 1 : 0);
Mike Stump11289f42009-09-09 15:08:12 +0000936
Douglas Gregorc5046832009-04-27 18:38:38 +0000937 if (Data.first == 0) {
938 Emit32(Out, (uint32_t) Data.second.st_ino);
939 Emit32(Out, (uint32_t) Data.second.st_dev);
940 Emit16(Out, (uint16_t) Data.second.st_mode);
941 Emit64(Out, (uint64_t) Data.second.st_mtime);
942 Emit64(Out, (uint64_t) Data.second.st_size);
943 }
944
945 assert(Out.tell() - Start == DataLen && "Wrong data length");
946 }
947};
948} // end anonymous namespace
949
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000950/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000951void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000952 // Build the on-disk hash table containing information about every
953 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000954 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000955 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000956 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000957 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958 Stat != StatEnd; ++Stat, ++NumStatEntries) {
959 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000960 Generator.insert(Filename, Stat->second);
961 }
Mike Stump11289f42009-09-09 15:08:12 +0000962
Douglas Gregorc5046832009-04-27 18:38:38 +0000963 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000964 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000965 uint32_t BucketOffset;
966 {
967 llvm::raw_svector_ostream Out(StatCacheData);
968 // Make sure that no bucket is at offset 0
969 clang::io::Emit32(Out, 0);
970 BucketOffset = Generator.Emit(Out);
971 }
972
973 // Create a blob abbreviation
974 using namespace llvm;
975 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000976 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
980 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
981
982 // Write the stat cache
983 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000984 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +0000985 Record.push_back(BucketOffset);
986 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000987 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +0000988}
989
990//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +0000991// Source Manager Serialization
992//===----------------------------------------------------------------------===//
993
994/// \brief Create an abbreviation for the SLocEntry that refers to a
995/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +0000996static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000997 using namespace llvm;
998 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000999 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001004 // FileEntry fields.
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001007 // HeaderFileInfo fields.
1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001013 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001014}
1015
1016/// \brief Create an abbreviation for the SLocEntry that refers to a
1017/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001018static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001019 using namespace llvm;
1020 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001021 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001027 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001028}
1029
1030/// \brief Create an abbreviation for the SLocEntry that refers to a
1031/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001032static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001033 using namespace llvm;
1034 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001035 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001037 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001038}
1039
1040/// \brief Create an abbreviation for the SLocEntry that refers to an
1041/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001042static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001043 using namespace llvm;
1044 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001045 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001051 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001052}
1053
1054/// \brief Writes the block containing the serialized form of the
1055/// source manager.
1056///
1057/// TODO: We should probably use an on-disk hash table (stored in a
1058/// blob), indexed based on the file name, so that we only create
1059/// entries for files that we actually need. In the common case (no
1060/// errors), we probably won't have to create file entries for any of
1061/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001062void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001063 const Preprocessor &PP,
1064 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001065 RecordData Record;
1066
Chris Lattner0910e3b2009-04-10 17:16:57 +00001067 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001068 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001069
1070 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001071 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1072 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1073 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1074 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001075
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001076 // Write the line table.
1077 if (SourceMgr.hasLineTable()) {
1078 LineTableInfo &LineTable = SourceMgr.getLineTable();
1079
1080 // Emit the file names
1081 Record.push_back(LineTable.getNumFilenames());
1082 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1083 // Emit the file name
1084 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001085 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001086 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1087 Record.push_back(FilenameLen);
1088 if (FilenameLen)
1089 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1090 }
Mike Stump11289f42009-09-09 15:08:12 +00001091
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001092 // Emit the line entries
1093 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1094 L != LEnd; ++L) {
1095 // Emit the file ID
1096 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001097
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001098 // Emit the line entries
1099 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001100 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001101 LEEnd = L->second.end();
1102 LE != LEEnd; ++LE) {
1103 Record.push_back(LE->FileOffset);
1104 Record.push_back(LE->LineNo);
1105 Record.push_back(LE->FilenameID);
1106 Record.push_back((unsigned)LE->FileKind);
1107 Record.push_back(LE->IncludeOffset);
1108 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001109 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001110 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001111 }
1112
Douglas Gregor258ae542009-04-27 06:38:32 +00001113 // Write out the source location entry table. We skip the first
1114 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001115 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001116 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001117 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1118 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1119 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1120 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001121 // Get this source location entry.
1122 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001123
Douglas Gregor258ae542009-04-27 06:38:32 +00001124 // Record the offset of this source-location entry.
1125 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1126
1127 // Figure out which record code to use.
1128 unsigned Code;
1129 if (SLoc->isFile()) {
1130 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001131 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001132 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001133 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001134 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001135 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001136 Record.clear();
1137 Record.push_back(Code);
1138
1139 Record.push_back(SLoc->getOffset());
1140 if (SLoc->isFile()) {
1141 const SrcMgr::FileInfo &File = SLoc->getFile();
1142 Record.push_back(File.getIncludeLoc().getRawEncoding());
1143 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1144 Record.push_back(File.hasLineDirectives());
1145
1146 const SrcMgr::ContentCache *Content = File.getContentCache();
1147 if (Content->Entry) {
1148 // The source location entry is a file. The blob associated
1149 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001151 // Emit size/modification time for this file.
1152 Record.push_back(Content->Entry->getSize());
1153 Record.push_back(Content->Entry->getModificationTime());
1154
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001155 // Emit header-search information associated with this file.
1156 HeaderFileInfo HFI;
1157 HeaderSearch &HS = PP.getHeaderSearchInfo();
1158 if (Content->Entry->getUID() < HS.header_file_size())
1159 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1160 Record.push_back(HFI.isImport);
1161 Record.push_back(HFI.DirInfo);
1162 Record.push_back(HFI.NumIncludes);
1163 AddIdentifierRef(HFI.ControllingMacro, Record);
1164
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001165 // Turn the file name into an absolute path, if it isn't already.
1166 const char *Filename = Content->Entry->getName();
1167 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001168 FilePath.makeAbsolute();
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001169 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001170
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001171 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001172 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001173
1174 // FIXME: For now, preload all file source locations, so that
1175 // we get the appropriate File entries in the reader. This is
1176 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001177 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001178 } else {
1179 // The source location entry is a buffer. The blob associated
1180 // with this entry contains the contents of the buffer.
1181
1182 // We add one to the size so that we capture the trailing NULL
1183 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1184 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001185 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001186 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001187 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001188 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1189 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001190 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001191 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001192 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001193 llvm::StringRef(Buffer->getBufferStart(),
1194 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001195
1196 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001197 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001198 }
1199 } else {
1200 // The source location entry is an instantiation.
1201 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1202 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1203 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1204 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1205
1206 // Compute the token length for this macro expansion.
1207 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001208 if (I + 1 != N)
1209 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001210 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1211 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1212 }
1213 }
1214
Douglas Gregor8f45df52009-04-16 22:23:12 +00001215 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001216
1217 if (SLocEntryOffsets.empty())
1218 return;
1219
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001220 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001221 // table is used for lazily loading source-location information.
1222 using namespace llvm;
1223 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001224 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1228 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001229
Douglas Gregor258ae542009-04-27 06:38:32 +00001230 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001231 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001232 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001233 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1234 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001235 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001236 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001237 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001238
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001239 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001240 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001241 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001242}
1243
Douglas Gregorc5046832009-04-27 18:38:38 +00001244//===----------------------------------------------------------------------===//
1245// Preprocessor Serialization
1246//===----------------------------------------------------------------------===//
1247
Chris Lattnereeffaef2009-04-10 17:15:23 +00001248/// \brief Writes the block containing the serialized form of the
1249/// preprocessor.
1250///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001251void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001252 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001253
Chris Lattner0af3ba12009-04-13 01:29:17 +00001254 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1255 if (PP.getCounterValue() != 0) {
1256 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001257 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001258 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001259 }
1260
1261 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001262 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001263
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001264 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001265 // FIXME: use diagnostics subsystem for localization etc.
1266 if (PP.SawDateOrTime())
1267 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001268
Douglas Gregor796d76a2010-10-20 22:00:55 +00001269
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001270 // Loop over all the macro definitions that are live at the end of the file,
1271 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001272 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001273 unsigned InclusionAbbrev = 0;
1274 if (PPRec) {
1275 using namespace llvm;
1276 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1277 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001285 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001286 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001287
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001288 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1289 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001290 // FIXME: This emits macros in hash table order, we should do it in a stable
1291 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001292 MacroInfo *MI = I->second;
1293
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001294 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001295 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001296 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001297
1298 // FIXME: There is a (probably minor) optimization we could do here, if
1299 // the macro comes from the original PCH but the identifier comes from a
1300 // chained PCH, by storing the offset into the original PCH rather than
1301 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001302 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001303 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001304 continue;
1305
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001306 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001307 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001308 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1309 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001310
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001311 unsigned Code;
1312 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001313 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001314 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001315 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001316
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001317 Record.push_back(MI->isC99Varargs());
1318 Record.push_back(MI->isGNUVarargs());
1319 Record.push_back(MI->getNumArgs());
1320 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1321 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001322 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001323 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001324
Douglas Gregoraae92242010-03-19 21:51:54 +00001325 // If we have a detailed preprocessing record, record the macro definition
1326 // ID that corresponds to this macro.
1327 if (PPRec)
1328 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001329
Douglas Gregor8f45df52009-04-16 22:23:12 +00001330 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001331 Record.clear();
1332
Chris Lattner2199f5b2009-04-10 18:08:30 +00001333 // Emit the tokens array.
1334 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1335 // Note that we know that the preprocessor does not have any annotation
1336 // tokens in it because they are created by the parser, and thus can't be
1337 // in a macro definition.
1338 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001339
Chris Lattner2199f5b2009-04-10 18:08:30 +00001340 Record.push_back(Tok.getLocation().getRawEncoding());
1341 Record.push_back(Tok.getLength());
1342
Chris Lattner2199f5b2009-04-10 18:08:30 +00001343 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1344 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001345 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001346
Chris Lattner2199f5b2009-04-10 18:08:30 +00001347 // FIXME: Should translate token kind to a stable encoding.
1348 Record.push_back(Tok.getKind());
1349 // FIXME: Should translate token flags to a stable encoding.
1350 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001351
Sebastian Redl539c5062010-08-18 23:57:32 +00001352 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001353 Record.clear();
1354 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001355 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001356 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001357
Douglas Gregoraae92242010-03-19 21:51:54 +00001358 // If the preprocessor has a preprocessing record, emit it.
1359 unsigned NumPreprocessingRecords = 0;
1360 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001361 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001362 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1363 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001364 E != EEnd; ++E) {
1365 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001366
Douglas Gregoraae92242010-03-19 21:51:54 +00001367 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001368 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001369 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1370 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1371 AddIdentifierRef(MI->getName(), Record);
1372 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
Sebastian Redl539c5062010-08-18 23:57:32 +00001373 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001374 continue;
1375 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001376
Douglas Gregoraae92242010-03-19 21:51:54 +00001377 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1378 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001379 MacroID ID = getMacroDefinitionID(MD);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001380
Douglas Gregor91096292010-10-02 19:29:26 +00001381 // Don't write the macro definition if it is from another AST file.
1382 if (ID < FirstMacroID)
1383 continue;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001384
Douglas Gregor91096292010-10-02 19:29:26 +00001385 unsigned Position = ID - FirstMacroID;
1386 if (Position != MacroDefinitionOffsets.size()) {
1387 if (Position > MacroDefinitionOffsets.size())
1388 MacroDefinitionOffsets.resize(Position + 1);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001389
1390 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001391 } else
1392 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001393
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001394 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001395 Record.push_back(ID);
1396 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1397 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1398 AddIdentifierRef(MD->getName(), Record);
1399 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001400 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001401 continue;
1402 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001403
Douglas Gregor796d76a2010-10-20 22:00:55 +00001404 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1405 Record.push_back(PP_INCLUSION_DIRECTIVE);
1406 Record.push_back(IndexBase + NumPreprocessingRecords++);
1407 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1408 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1409 Record.push_back(ID->getFileName().size());
1410 Record.push_back(ID->wasInQuotes());
1411 Record.push_back(static_cast<unsigned>(ID->getKind()));
1412 llvm::SmallString<64> Buffer;
1413 Buffer += ID->getFileName();
1414 Buffer += ID->getFile()->getName();
1415 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1416 continue;
1417 }
Douglas Gregoraae92242010-03-19 21:51:54 +00001418 }
1419 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001420
Douglas Gregor8f45df52009-04-16 22:23:12 +00001421 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001422
Douglas Gregoraae92242010-03-19 21:51:54 +00001423 // Write the offsets table for the preprocessing record.
1424 if (NumPreprocessingRecords > 0) {
1425 // Write the offsets table for identifier IDs.
1426 using namespace llvm;
1427 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001428 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1432 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001433
Douglas Gregoraae92242010-03-19 21:51:54 +00001434 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001435 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001436 Record.push_back(NumPreprocessingRecords);
1437 Record.push_back(MacroDefinitionOffsets.size());
1438 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001439 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001440 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1441 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001442}
1443
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001444void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1445 RecordData Record;
1446 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
1447 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i);
1448 if (Map & 0x8) { // user mapping.
1449 Record.push_back(i);
1450 Record.push_back(Map & 0x7);
1451 }
1452 }
1453
1454 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
1455}
1456
Douglas Gregorc5046832009-04-27 18:38:38 +00001457//===----------------------------------------------------------------------===//
1458// Type Serialization
1459//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001460
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001461/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001462void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001463 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001464 if (Idx.getIndex() == 0) // we haven't seen this type before.
1465 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001466
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001467 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001468
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001469 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001470 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001471 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001472 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001473 else if (TypeOffsets.size() < Index) {
1474 TypeOffsets.resize(Index + 1);
1475 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001476 }
1477
1478 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001479
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001480 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001481 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001482
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001483 if (T.hasLocalNonFastQualifiers()) {
1484 Qualifiers Qs = T.getLocalQualifiers();
1485 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001486 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001487 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001488 } else {
1489 switch (T->getTypeClass()) {
1490 // For all of the concrete, non-dependent types, call the
1491 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001492#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001493 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001494#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001495#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001496 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001497 }
1498
1499 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001500 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001501
1502 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001503 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001504}
1505
Douglas Gregorc5046832009-04-27 18:38:38 +00001506//===----------------------------------------------------------------------===//
1507// Declaration Serialization
1508//===----------------------------------------------------------------------===//
1509
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001510/// \brief Write the block containing all of the declaration IDs
1511/// lexically declared within the given DeclContext.
1512///
1513/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1514/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001515uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001516 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001517 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001518 return 0;
1519
Douglas Gregor8f45df52009-04-16 22:23:12 +00001520 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001521 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001522 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001523 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001524 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1525 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001526 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001527
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001528 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001529 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001530 reinterpret_cast<char*>(Decls.data()),
1531 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001532 return Offset;
1533}
1534
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001535void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001536 using namespace llvm;
1537 RecordData Record;
1538
1539 // Write the type offsets array
1540 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001541 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1543 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1544 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1545 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001546 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001547 Record.push_back(TypeOffsets.size());
1548 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001549 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001550 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1551
1552 // Write the declaration offsets array
1553 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001554 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1557 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1558 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001559 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001560 Record.push_back(DeclOffsets.size());
1561 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001562 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001563 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1564}
1565
Douglas Gregorc5046832009-04-27 18:38:38 +00001566//===----------------------------------------------------------------------===//
1567// Global Method Pool and Selector Serialization
1568//===----------------------------------------------------------------------===//
1569
Douglas Gregore84a9da2009-04-20 20:36:09 +00001570namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001571// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001572class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001573 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001574
1575public:
1576 typedef Selector key_type;
1577 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001578
Sebastian Redl834bb972010-08-04 17:20:04 +00001579 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001580 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001581 ObjCMethodList Instance, Factory;
1582 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001583 typedef const data_type& data_type_ref;
1584
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001585 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001586
Douglas Gregorc78d3462009-04-24 21:10:55 +00001587 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001588 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001589 }
Mike Stump11289f42009-09-09 15:08:12 +00001590
1591 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001592 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1593 data_type_ref Methods) {
1594 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1595 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001596 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1597 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001598 Method = Method->Next)
1599 if (Method->Method)
1600 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001601 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001602 Method = Method->Next)
1603 if (Method->Method)
1604 DataLen += 4;
1605 clang::io::Emit16(Out, DataLen);
1606 return std::make_pair(KeyLen, DataLen);
1607 }
Mike Stump11289f42009-09-09 15:08:12 +00001608
Douglas Gregor95c13f52009-04-25 17:48:32 +00001609 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001610 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001611 assert((Start >> 32) == 0 && "Selector key offset too large");
1612 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001613 unsigned N = Sel.getNumArgs();
1614 clang::io::Emit16(Out, N);
1615 if (N == 0)
1616 N = 1;
1617 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001618 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001619 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1620 }
Mike Stump11289f42009-09-09 15:08:12 +00001621
Douglas Gregorc78d3462009-04-24 21:10:55 +00001622 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001623 data_type_ref Methods, unsigned DataLen) {
1624 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001625 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001626 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001627 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001628 Method = Method->Next)
1629 if (Method->Method)
1630 ++NumInstanceMethods;
1631
1632 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001633 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001634 Method = Method->Next)
1635 if (Method->Method)
1636 ++NumFactoryMethods;
1637
1638 clang::io::Emit16(Out, NumInstanceMethods);
1639 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001640 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001641 Method = Method->Next)
1642 if (Method->Method)
1643 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001644 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001645 Method = Method->Next)
1646 if (Method->Method)
1647 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001648
1649 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001650 }
1651};
1652} // end anonymous namespace
1653
Sebastian Redla19a67f2010-08-03 21:58:15 +00001654/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001655///
1656/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001657/// in an on-disk hash table indexed by the selector. The hash table also
1658/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001659void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001660 using namespace llvm;
1661
Sebastian Redla19a67f2010-08-03 21:58:15 +00001662 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001663 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001664 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001665 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001666 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001667 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001668 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001669 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001670
Sebastian Redla19a67f2010-08-03 21:58:15 +00001671 // Create the on-disk hash table representation. We walk through every
1672 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001673 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001674 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001675 I = SelectorIDs.begin(), E = SelectorIDs.end();
1676 I != E; ++I) {
1677 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001678 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001679 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001680 I->second,
1681 ObjCMethodList(),
1682 ObjCMethodList()
1683 };
1684 if (F != SemaRef.MethodPool.end()) {
1685 Data.Instance = F->second.first;
1686 Data.Factory = F->second.second;
1687 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001688 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001689 // changed.
1690 if (Chain && I->second < FirstSelectorID) {
1691 // Selector already exists. Did it change?
1692 bool changed = false;
1693 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1694 M = M->Next) {
1695 if (M->Method->getPCHLevel() == 0)
1696 changed = true;
1697 }
1698 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1699 M = M->Next) {
1700 if (M->Method->getPCHLevel() == 0)
1701 changed = true;
1702 }
1703 if (!changed)
1704 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001705 } else if (Data.Instance.Method || Data.Factory.Method) {
1706 // A new method pool entry.
1707 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001708 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001709 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001710 }
1711
Douglas Gregorc78d3462009-04-24 21:10:55 +00001712 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001713 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001714 uint32_t BucketOffset;
1715 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001716 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001717 llvm::raw_svector_ostream Out(MethodPool);
1718 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001719 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001720 BucketOffset = Generator.Emit(Out, Trait);
1721 }
1722
1723 // Create a blob abbreviation
1724 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001725 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1729 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1730
Douglas Gregor95c13f52009-04-25 17:48:32 +00001731 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001732 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001733 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001734 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001735 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001736 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001737
1738 // Create a blob abbreviation for the selector table offsets.
1739 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001740 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1743 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1744
1745 // Write the selector offsets table.
1746 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001747 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001748 Record.push_back(SelectorOffsets.size());
1749 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001750 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001751 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001752 }
1753}
1754
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001755/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001756void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001757 using namespace llvm;
1758 if (SemaRef.ReferencedSelectors.empty())
1759 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001760
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001761 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001762
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001763 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001764 // very tricky to fix, and given that @selector shouldn't really appear in
1765 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001766 for (DenseMap<Selector, SourceLocation>::iterator S =
1767 SemaRef.ReferencedSelectors.begin(),
1768 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1769 Selector Sel = (*S).first;
1770 SourceLocation Loc = (*S).second;
1771 AddSelectorRef(Sel, Record);
1772 AddSourceLocation(Loc, Record);
1773 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001774 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001775}
1776
Douglas Gregorc5046832009-04-27 18:38:38 +00001777//===----------------------------------------------------------------------===//
1778// Identifier Table Serialization
1779//===----------------------------------------------------------------------===//
1780
Douglas Gregorc78d3462009-04-24 21:10:55 +00001781namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001782class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001783 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001784 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001785
Douglas Gregor1d583f22009-04-28 21:18:29 +00001786 /// \brief Determines whether this is an "interesting" identifier
1787 /// that needs a full IdentifierInfo structure written into the hash
1788 /// table.
1789 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1790 return II->isPoisoned() ||
1791 II->isExtensionToken() ||
1792 II->hasMacroDefinition() ||
1793 II->getObjCOrBuiltinID() ||
1794 II->getFETokenInfo<void>();
1795 }
1796
Douglas Gregore84a9da2009-04-20 20:36:09 +00001797public:
1798 typedef const IdentifierInfo* key_type;
1799 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001800
Sebastian Redl539c5062010-08-18 23:57:32 +00001801 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001802 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001803
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001804 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001805 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001806
1807 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001808 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001809 }
Mike Stump11289f42009-09-09 15:08:12 +00001810
1811 std::pair<unsigned,unsigned>
1812 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001813 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001814 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001815 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1816 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001817 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001818 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001819 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001820 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001821 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1822 DEnd = IdentifierResolver::end();
1823 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001824 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001825 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001826 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001827 // We emit the key length after the data length so that every
1828 // string is preceded by a 16-bit length. This matches the PTH
1829 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001830 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001831 return std::make_pair(KeyLen, DataLen);
1832 }
Mike Stump11289f42009-09-09 15:08:12 +00001833
1834 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001835 unsigned KeyLen) {
1836 // Record the location of the key data. This is used when generating
1837 // the mapping from persistent IDs to strings.
1838 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001839 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001840 }
Mike Stump11289f42009-09-09 15:08:12 +00001841
1842 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001843 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001844 if (!isInterestingIdentifier(II)) {
1845 clang::io::Emit32(Out, ID << 1);
1846 return;
1847 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001848
Douglas Gregor1d583f22009-04-28 21:18:29 +00001849 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001850 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001851 bool hasMacroDefinition =
1852 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001853 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001854 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001855 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1856 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1857 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001858 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001859 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001860 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001861
Douglas Gregorc3366a52009-04-21 23:56:24 +00001862 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001863 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001864
Douglas Gregora868bbd2009-04-21 22:25:48 +00001865 // Emit the declaration IDs in reverse order, because the
1866 // IdentifierResolver provides the declarations as they would be
1867 // visible (e.g., the function "stat" would come before the struct
1868 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1869 // adds declarations to the end of the list (so we need to see the
1870 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001871 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001872 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001873 IdentifierResolver::end());
1874 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1875 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001876 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001877 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001878 }
1879};
1880} // end anonymous namespace
1881
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001882/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001883///
1884/// The identifier table consists of a blob containing string data
1885/// (the actual identifiers themselves) and a separate "offsets" index
1886/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001887void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001888 using namespace llvm;
1889
1890 // Create and write out the blob that contains the identifier
1891 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001892 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001893 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001894 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001895
Douglas Gregore6648fb2009-04-28 20:33:11 +00001896 // Look for any identifiers that were named while processing the
1897 // headers, but are otherwise not needed. We add these to the hash
1898 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001899 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001900 // file.
1901 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1902 IDEnd = PP.getIdentifierTable().end();
1903 ID != IDEnd; ++ID)
1904 getIdentifierRef(ID->second);
1905
Sebastian Redlff4a2952010-07-23 23:49:55 +00001906 // Create the on-disk hash table representation. We only store offsets
1907 // for identifiers that appear here for the first time.
1908 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001909 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001910 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1911 ID != IDEnd; ++ID) {
1912 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001913 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001914 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001915 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001916
Douglas Gregore84a9da2009-04-20 20:36:09 +00001917 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001918 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001919 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001920 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001921 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001922 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001923 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001924 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001925 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001926 }
1927
1928 // Create a blob abbreviation
1929 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001933 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001934
1935 // Write the identifier table
1936 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001937 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001938 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001939 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001940 }
1941
1942 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001943 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001944 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1947 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1948
1949 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001950 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001951 Record.push_back(IdentifierOffsets.size());
1952 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001953 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001954 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001955}
1956
Douglas Gregorc5046832009-04-27 18:38:38 +00001957//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001958// DeclContext's Name Lookup Table Serialization
1959//===----------------------------------------------------------------------===//
1960
1961namespace {
1962// Trait used for the on-disk hash table used in the method pool.
1963class ASTDeclContextNameLookupTrait {
1964 ASTWriter &Writer;
1965
1966public:
1967 typedef DeclarationName key_type;
1968 typedef key_type key_type_ref;
1969
1970 typedef DeclContext::lookup_result data_type;
1971 typedef const data_type& data_type_ref;
1972
1973 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1974
1975 unsigned ComputeHash(DeclarationName Name) {
1976 llvm::FoldingSetNodeID ID;
1977 ID.AddInteger(Name.getNameKind());
1978
1979 switch (Name.getNameKind()) {
1980 case DeclarationName::Identifier:
1981 ID.AddString(Name.getAsIdentifierInfo()->getName());
1982 break;
1983 case DeclarationName::ObjCZeroArgSelector:
1984 case DeclarationName::ObjCOneArgSelector:
1985 case DeclarationName::ObjCMultiArgSelector:
1986 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1987 break;
1988 case DeclarationName::CXXConstructorName:
1989 case DeclarationName::CXXDestructorName:
1990 case DeclarationName::CXXConversionFunctionName:
1991 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1992 break;
1993 case DeclarationName::CXXOperatorName:
1994 ID.AddInteger(Name.getCXXOverloadedOperator());
1995 break;
1996 case DeclarationName::CXXLiteralOperatorName:
1997 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1998 case DeclarationName::CXXUsingDirective:
1999 break;
2000 }
2001
2002 return ID.ComputeHash();
2003 }
2004
2005 std::pair<unsigned,unsigned>
2006 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2007 data_type_ref Lookup) {
2008 unsigned KeyLen = 1;
2009 switch (Name.getNameKind()) {
2010 case DeclarationName::Identifier:
2011 case DeclarationName::ObjCZeroArgSelector:
2012 case DeclarationName::ObjCOneArgSelector:
2013 case DeclarationName::ObjCMultiArgSelector:
2014 case DeclarationName::CXXConstructorName:
2015 case DeclarationName::CXXDestructorName:
2016 case DeclarationName::CXXConversionFunctionName:
2017 case DeclarationName::CXXLiteralOperatorName:
2018 KeyLen += 4;
2019 break;
2020 case DeclarationName::CXXOperatorName:
2021 KeyLen += 1;
2022 break;
2023 case DeclarationName::CXXUsingDirective:
2024 break;
2025 }
2026 clang::io::Emit16(Out, KeyLen);
2027
2028 // 2 bytes for num of decls and 4 for each DeclID.
2029 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2030 clang::io::Emit16(Out, DataLen);
2031
2032 return std::make_pair(KeyLen, DataLen);
2033 }
2034
2035 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2036 using namespace clang::io;
2037
2038 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2039 Emit8(Out, Name.getNameKind());
2040 switch (Name.getNameKind()) {
2041 case DeclarationName::Identifier:
2042 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2043 break;
2044 case DeclarationName::ObjCZeroArgSelector:
2045 case DeclarationName::ObjCOneArgSelector:
2046 case DeclarationName::ObjCMultiArgSelector:
2047 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2048 break;
2049 case DeclarationName::CXXConstructorName:
2050 case DeclarationName::CXXDestructorName:
2051 case DeclarationName::CXXConversionFunctionName:
2052 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2053 break;
2054 case DeclarationName::CXXOperatorName:
2055 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2056 Emit8(Out, Name.getCXXOverloadedOperator());
2057 break;
2058 case DeclarationName::CXXLiteralOperatorName:
2059 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2060 break;
2061 case DeclarationName::CXXUsingDirective:
2062 break;
2063 }
2064 }
2065
2066 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2067 data_type Lookup, unsigned DataLen) {
2068 uint64_t Start = Out.tell(); (void)Start;
2069 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2070 for (; Lookup.first != Lookup.second; ++Lookup.first)
2071 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2072
2073 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2074 }
2075};
2076} // end anonymous namespace
2077
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002078/// \brief Write the block containing all of the declaration IDs
2079/// visible from the given DeclContext.
2080///
2081/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002082/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002083uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2084 DeclContext *DC) {
2085 if (DC->getPrimaryContext() != DC)
2086 return 0;
2087
2088 // Since there is no name lookup into functions or methods, don't bother to
2089 // build a visible-declarations table for these entities.
2090 if (DC->isFunctionOrMethod())
2091 return 0;
2092
2093 // If not in C++, we perform name lookup for the translation unit via the
2094 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2095 // FIXME: In C++ we need the visible declarations in order to "see" the
2096 // friend declarations, is there a way to do this without writing the table ?
2097 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2098 return 0;
2099
2100 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002101 if (DC->hasExternalVisibleStorage())
2102 DC->MaterializeVisibleDeclsFromExternalStorage();
2103 else
2104 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002105
2106 // Serialize the contents of the mapping used for lookup. Note that,
2107 // although we have two very different code paths, the serialized
2108 // representation is the same for both cases: a declaration name,
2109 // followed by a size, followed by references to the visible
2110 // declarations that have that name.
2111 uint64_t Offset = Stream.GetCurrentBitNo();
2112 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2113 if (!Map || Map->empty())
2114 return 0;
2115
2116 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2117 ASTDeclContextNameLookupTrait Trait(*this);
2118
2119 // Create the on-disk hash table representation.
2120 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2121 D != DEnd; ++D) {
2122 DeclarationName Name = D->first;
2123 DeclContext::lookup_result Result = D->second.getLookupResult();
2124 Generator.insert(Name, Result, Trait);
2125 }
2126
2127 // Create the on-disk hash table in a buffer.
2128 llvm::SmallString<4096> LookupTable;
2129 uint32_t BucketOffset;
2130 {
2131 llvm::raw_svector_ostream Out(LookupTable);
2132 // Make sure that no bucket is at offset 0
2133 clang::io::Emit32(Out, 0);
2134 BucketOffset = Generator.Emit(Out, Trait);
2135 }
2136
2137 // Write the lookup table
2138 RecordData Record;
2139 Record.push_back(DECL_CONTEXT_VISIBLE);
2140 Record.push_back(BucketOffset);
2141 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2142 LookupTable.str());
2143
2144 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2145 ++NumVisibleDeclContexts;
2146 return Offset;
2147}
2148
Sebastian Redla4071b42010-08-24 00:50:09 +00002149/// \brief Write an UPDATE_VISIBLE block for the given context.
2150///
2151/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2152/// DeclContext in a dependent AST file. As such, they only exist for the TU
2153/// (in C++) and for namespaces.
2154void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002155 // Make the context build its lookup table, but don't make it load external
2156 // decls.
2157 DC->lookup(DeclarationName());
2158
2159 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2160 if (!Map || Map->empty())
2161 return;
2162
2163 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2164 ASTDeclContextNameLookupTrait Trait(*this);
2165
2166 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002167 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2168 D != DEnd; ++D) {
2169 DeclarationName Name = D->first;
2170 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002171 // For any name that appears in this table, the results are complete, i.e.
2172 // they overwrite results from previous PCHs. Merging is always a mess.
2173 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002174 }
2175
2176 // Create the on-disk hash table in a buffer.
2177 llvm::SmallString<4096> LookupTable;
2178 uint32_t BucketOffset;
2179 {
2180 llvm::raw_svector_ostream Out(LookupTable);
2181 // Make sure that no bucket is at offset 0
2182 clang::io::Emit32(Out, 0);
2183 BucketOffset = Generator.Emit(Out, Trait);
2184 }
2185
2186 // Write the lookup table
2187 RecordData Record;
2188 Record.push_back(UPDATE_VISIBLE);
2189 Record.push_back(getDeclID(cast<Decl>(DC)));
2190 Record.push_back(BucketOffset);
2191 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2192}
2193
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002194//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002195// General Serialization Routines
2196//===----------------------------------------------------------------------===//
2197
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002198/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002199void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002200 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002201 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2202 const Attr * A = *i;
2203 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2204 AddSourceLocation(A->getLocation(), Record);
2205 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002206
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002207#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002208
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002209 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002210}
2211
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002212void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002213 Record.push_back(Str.size());
2214 Record.insert(Record.end(), Str.begin(), Str.end());
2215}
2216
Douglas Gregore84a9da2009-04-20 20:36:09 +00002217/// \brief Note that the identifier II occurs at the given offset
2218/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002219void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002220 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002221 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002222 // up earlier in the chain and thus don't need an offset.
2223 if (ID >= FirstIdentID)
2224 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002225}
2226
Douglas Gregor95c13f52009-04-25 17:48:32 +00002227/// \brief Note that the selector Sel occurs at the given offset
2228/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002229void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002230 unsigned ID = SelectorIDs[Sel];
2231 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002232 // Don't record offsets for selectors that are also available in a different
2233 // file.
2234 if (ID < FirstSelectorID)
2235 return;
2236 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002237}
2238
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002239ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redld95a56e2010-08-04 18:21:41 +00002240 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002241 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002242 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002243 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2244 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002245 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002246 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2247 NextCXXBaseSpecifiersID(1)
2248{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002249}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002250
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002251void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002252 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002253 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002254 Stream.Emit((unsigned)'C', 8);
2255 Stream.Emit((unsigned)'P', 8);
2256 Stream.Emit((unsigned)'C', 8);
2257 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002258
Chris Lattner28fa4e62009-04-26 22:26:21 +00002259 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002260
Sebastian Redl143413f2010-07-12 22:02:52 +00002261 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002262 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002263 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002264 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002265}
2266
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002267void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002268 const char *isysroot) {
2269 using namespace llvm;
2270
2271 ASTContext &Context = SemaRef.Context;
2272 Preprocessor &PP = SemaRef.PP;
2273
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002274 // The translation unit is the first declaration we'll emit.
2275 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002276 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002277 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002278
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002279 // Make sure that we emit IdentifierInfos (and any attached
2280 // declarations) for builtins.
2281 {
2282 IdentifierTable &Table = PP.getIdentifierTable();
2283 llvm::SmallVector<const char *, 32> BuiltinNames;
2284 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2285 Context.getLangOptions().NoBuiltin);
2286 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2287 getIdentifierRef(&Table.get(BuiltinNames[I]));
2288 }
2289
Chris Lattner0c797362009-09-08 18:19:27 +00002290 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002291 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002292 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002293 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002294 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2295 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002296 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002297
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002298 // Build a record containing all of the file scoped decls in this file.
2299 RecordData UnusedFileScopedDecls;
2300 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2301 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002302
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002303 RecordData WeakUndeclaredIdentifiers;
2304 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2305 WeakUndeclaredIdentifiers.push_back(
2306 SemaRef.WeakUndeclaredIdentifiers.size());
2307 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2308 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2309 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2310 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2311 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2312 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2313 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2314 }
2315 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002316
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002317 // Build a record containing all of the locally-scoped external
2318 // declarations in this header file. Generally, this record will be
2319 // empty.
2320 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002321 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002322 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002323 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002324 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2325 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2326 TD != TDEnd; ++TD)
2327 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2328
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002329 // Build a record containing all of the ext_vector declarations.
2330 RecordData ExtVectorDecls;
2331 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2332 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2333
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002334 // Build a record containing all of the VTable uses information.
2335 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002336 if (!SemaRef.VTableUses.empty()) {
2337 VTableUses.push_back(SemaRef.VTableUses.size());
2338 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2339 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2340 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2341 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2342 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002343 }
2344
2345 // Build a record containing all of dynamic classes declarations.
2346 RecordData DynamicClasses;
2347 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2348 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2349
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002350 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002351 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002352 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002353 I = SemaRef.PendingInstantiations.begin(),
2354 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2355 AddDeclRef(I->first, PendingInstantiations);
2356 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002357 }
2358 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2359 "There are local ones at end of translation unit!");
2360
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002361 // Build a record containing some declaration references.
2362 RecordData SemaDeclRefs;
2363 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2364 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2365 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2366 }
2367
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002368 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002369 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002370 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002371 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002372 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002373 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002374 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002375 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002376 // Write the record of special types.
2377 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002378
Steve Naroffc277ad12009-07-18 15:33:26 +00002379 AddTypeRef(Context.getBuiltinVaListType(), Record);
2380 AddTypeRef(Context.getObjCIdType(), Record);
2381 AddTypeRef(Context.getObjCSelType(), Record);
2382 AddTypeRef(Context.getObjCProtoType(), Record);
2383 AddTypeRef(Context.getObjCClassType(), Record);
2384 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2385 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2386 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002387 AddTypeRef(Context.getjmp_bufType(), Record);
2388 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002389 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2390 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002391 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002392 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002393 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2394 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002395 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002396 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002397
Douglas Gregor1970d882009-04-26 03:49:13 +00002398 // Keep writing types and declarations until all types and
2399 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002400 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002401 WriteDeclsBlockAbbrevs();
2402 while (!DeclTypesToEmit.empty()) {
2403 DeclOrType DOT = DeclTypesToEmit.front();
2404 DeclTypesToEmit.pop();
2405 if (DOT.isType())
2406 WriteType(DOT.getType());
2407 else
2408 WriteDecl(Context, DOT.getDecl());
2409 }
2410 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002411
Douglas Gregor45053152009-10-17 17:25:45 +00002412 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002413 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002414 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002415 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002416
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002417 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002418 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002419
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002420 // Write the C++ base-specifier set offsets.
2421 if (!CXXBaseSpecifiersOffsets.empty()) {
2422 // Create a blob abbreviation for the C++ base specifiers offsets.
2423 using namespace llvm;
2424
2425 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2426 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2429 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2430
2431 // Write the selector offsets table.
2432 Record.clear();
2433 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2434 Record.push_back(CXXBaseSpecifiersOffsets.size());
2435 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2436 (const char *)CXXBaseSpecifiersOffsets.data(),
2437 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2438 }
2439
Douglas Gregord4df8652009-04-22 22:02:47 +00002440 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002441 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002442 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002443
2444 // Write the record containing tentative definitions.
2445 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002446 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002447
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002448 // Write the record containing unused file scoped decls.
2449 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002450 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002451
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002452 // Write the record containing weak undeclared identifiers.
2453 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002454 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002455 WeakUndeclaredIdentifiers);
2456
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002457 // Write the record containing locally-scoped external definitions.
2458 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002459 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002460 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002461
2462 // Write the record containing ext_vector type names.
2463 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002464 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002465
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002466 // Write the record containing VTable uses information.
2467 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002468 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002469
2470 // Write the record containing dynamic classes declarations.
2471 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002472 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002473
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002474 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002475 if (!PendingInstantiations.empty())
2476 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002477
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002478 // Write the record containing declaration references of Sema.
2479 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002480 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002481
Douglas Gregor08f01292009-04-17 22:13:46 +00002482 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002483 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002484 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002485 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002486 Record.push_back(NumLexicalDeclContexts);
2487 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002488 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002489 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002490}
2491
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002492void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002493 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002494 using namespace llvm;
2495
2496 ASTContext &Context = SemaRef.Context;
2497 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002498
Sebastian Redl143413f2010-07-12 22:02:52 +00002499 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002500 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002501 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002502 if (StatCalls && !isysroot)
2503 WriteStatCache(*StatCalls);
2504 // FIXME: Source manager block should only write new stuff, which could be
2505 // done by tracking the largest ID in the chain
2506 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002507
2508 // The special types are in the chained PCH.
2509
2510 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002511 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002512 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002513 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002514 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2515 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002516 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002517 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002518 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002519 else if ((*I)->isChangedSinceDeserialization())
2520 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002521 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002522 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002523 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002524 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002525 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2526 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2527 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002528 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002529 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2530 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002531 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002532 // And a visible updates block for the DeclContexts.
2533 Abv = new llvm::BitCodeAbbrev();
2534 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2535 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2536 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2537 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2538 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2539 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002540
Sebastian Redl98912122010-07-27 23:01:28 +00002541 // Build a record containing all of the new tentative definitions in this
2542 // file, in TentativeDefinitions order.
2543 RecordData TentativeDefinitions;
2544 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2545 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2546 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2547 }
2548
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002549 // Build a record containing all of the file scoped decls in this file.
2550 RecordData UnusedFileScopedDecls;
2551 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2552 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2553 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002554 }
2555
Sebastian Redl08aca90252010-08-05 18:21:25 +00002556 // We write the entire table, overwriting the tables from the chain.
2557 RecordData WeakUndeclaredIdentifiers;
2558 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2559 WeakUndeclaredIdentifiers.push_back(
2560 SemaRef.WeakUndeclaredIdentifiers.size());
2561 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2562 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2563 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2564 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2565 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2566 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2567 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2568 }
2569 }
2570
Sebastian Redl98912122010-07-27 23:01:28 +00002571 // Build a record containing all of the locally-scoped external
2572 // declarations in this header file. Generally, this record will be
2573 // empty.
2574 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002575 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002576 // nondeterminstic!
2577 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2578 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2579 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2580 TD != TDEnd; ++TD) {
2581 if (TD->second->getPCHLevel() == 0)
2582 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2583 }
2584
2585 // Build a record containing all of the ext_vector declarations.
2586 RecordData ExtVectorDecls;
2587 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2588 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2589 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2590 }
2591
Sebastian Redl08aca90252010-08-05 18:21:25 +00002592 // Build a record containing all of the VTable uses information.
2593 // We write everything here, because it's too hard to determine whether
2594 // a use is new to this part.
2595 RecordData VTableUses;
2596 if (!SemaRef.VTableUses.empty()) {
2597 VTableUses.push_back(SemaRef.VTableUses.size());
2598 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2599 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2600 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2601 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2602 }
2603 }
2604
2605 // Build a record containing all of dynamic classes declarations.
2606 RecordData DynamicClasses;
2607 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2608 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2609 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2610
2611 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002612 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002613 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002614 I = SemaRef.PendingInstantiations.begin(),
2615 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002616 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002617 AddDeclRef(I->first, PendingInstantiations);
2618 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002619 }
2620 }
2621 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2622 "There are local ones at end of translation unit!");
2623
2624 // Build a record containing some declaration references.
2625 // It's not worth the effort to avoid duplication here.
2626 RecordData SemaDeclRefs;
2627 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2628 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2629 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2630 }
2631
Sebastian Redl539c5062010-08-18 23:57:32 +00002632 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002633 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002634 for (DeclsToRewriteTy::iterator
2635 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2636 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002637 while (!DeclTypesToEmit.empty()) {
2638 DeclOrType DOT = DeclTypesToEmit.front();
2639 DeclTypesToEmit.pop();
2640 if (DOT.isType())
2641 WriteType(DOT.getType());
2642 else
2643 WriteDecl(Context, DOT.getDecl());
2644 }
2645 Stream.ExitBlock();
2646
Sebastian Redl98912122010-07-27 23:01:28 +00002647 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002648 WriteSelectors(SemaRef);
2649 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002650 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002651 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002652 // FIXME: For chained PCH only write the new mappings (we currently
2653 // write all of them again).
2654 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002655
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002656 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002657 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002658 RecordData FirstLatestDeclIDs;
2659 for (FirstLatestDeclMap::iterator
2660 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2661 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2662 "Expected first & second to be in different PCHs");
2663 AddDeclRef(I->first, FirstLatestDeclIDs);
2664 AddDeclRef(I->second, FirstLatestDeclIDs);
2665 }
2666 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002667 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002668
Sebastian Redl98912122010-07-27 23:01:28 +00002669 // Write the record containing external, unnamed definitions.
2670 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002671 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002672
2673 // Write the record containing tentative definitions.
2674 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002675 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002676
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002677 // Write the record containing unused file scoped decls.
2678 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002679 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002680
Sebastian Redl08aca90252010-08-05 18:21:25 +00002681 // Write the record containing weak undeclared identifiers.
2682 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002683 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002684 WeakUndeclaredIdentifiers);
2685
Sebastian Redl98912122010-07-27 23:01:28 +00002686 // Write the record containing locally-scoped external definitions.
2687 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002688 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002689 LocallyScopedExternalDecls);
2690
2691 // Write the record containing ext_vector type names.
2692 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002693 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002694
Sebastian Redl08aca90252010-08-05 18:21:25 +00002695 // Write the record containing VTable uses information.
2696 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002697 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002698
2699 // Write the record containing dynamic classes declarations.
2700 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002701 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002702
2703 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002704 if (!PendingInstantiations.empty())
2705 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002706
2707 // Write the record containing declaration references of Sema.
2708 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002709 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002710
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002711 // Write the updates to DeclContexts.
2712 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2713 I = UpdatedDeclContexts.begin(),
2714 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002715 I != E; ++I)
2716 WriteDeclContextVisibleUpdate(*I);
2717
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002718 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002719
Sebastian Redl98912122010-07-27 23:01:28 +00002720 Record.clear();
2721 Record.push_back(NumStatements);
2722 Record.push_back(NumMacros);
2723 Record.push_back(NumLexicalDeclContexts);
2724 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002725 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002726 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002727 Stream.ExitBlock();
2728}
2729
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002730void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002731 if (DeclUpdates.empty())
2732 return;
2733
2734 RecordData OffsetsRecord;
2735 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2736 for (DeclUpdateMap::iterator
2737 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2738 const Decl *D = I->first;
2739 UpdateRecord &URec = I->second;
2740
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002741 if (DeclsToRewrite.count(D))
2742 continue; // The decl will be written completely,no need to store updates.
2743
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002744 uint64_t Offset = Stream.GetCurrentBitNo();
2745 Stream.EmitRecord(DECL_UPDATES, URec);
2746
2747 OffsetsRecord.push_back(GetDeclRef(D));
2748 OffsetsRecord.push_back(Offset);
2749 }
2750 Stream.ExitBlock();
2751 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2752}
2753
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002754void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002755 if (ReplacedDecls.empty())
2756 return;
2757
2758 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002759 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002760 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2761 Record.push_back(I->first);
2762 Record.push_back(I->second);
2763 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002764 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002765}
2766
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002767void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002768 Record.push_back(Loc.getRawEncoding());
2769}
2770
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002771void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002772 AddSourceLocation(Range.getBegin(), Record);
2773 AddSourceLocation(Range.getEnd(), Record);
2774}
2775
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002776void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002777 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002778 const uint64_t *Words = Value.getRawData();
2779 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002780}
2781
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002782void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002783 Record.push_back(Value.isUnsigned());
2784 AddAPInt(Value, Record);
2785}
2786
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002787void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002788 AddAPInt(Value.bitcastToAPInt(), Record);
2789}
2790
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002791void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002792 Record.push_back(getIdentifierRef(II));
2793}
2794
Sebastian Redl539c5062010-08-18 23:57:32 +00002795IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002796 if (II == 0)
2797 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002798
Sebastian Redl539c5062010-08-18 23:57:32 +00002799 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002800 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002801 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002802 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002803}
2804
Sebastian Redl50e26582010-09-15 19:54:06 +00002805MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002806 if (MD == 0)
2807 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002808
2809 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002810 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002811 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002812 return ID;
2813}
2814
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002815void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002816 Record.push_back(getSelectorRef(SelRef));
2817}
2818
Sebastian Redl539c5062010-08-18 23:57:32 +00002819SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002820 if (Sel.getAsOpaquePtr() == 0) {
2821 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002822 }
2823
Sebastian Redl539c5062010-08-18 23:57:32 +00002824 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002825 if (SID == 0 && Chain) {
2826 // This might trigger a ReadSelector callback, which will set the ID for
2827 // this selector.
2828 Chain->LoadSelector(Sel);
2829 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002830 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002831 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002832 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002833 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002834}
2835
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002836void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002837 AddDeclRef(Temp->getDestructor(), Record);
2838}
2839
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002840void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2841 CXXBaseSpecifier const *BasesEnd,
2842 RecordDataImpl &Record) {
2843 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2844 CXXBaseSpecifiersToWrite.push_back(
2845 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2846 Bases, BasesEnd));
2847 Record.push_back(NextCXXBaseSpecifiersID++);
2848}
2849
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002850void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002851 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002852 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002853 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002854 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002855 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002856 break;
2857 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002858 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002859 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002860 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002861 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2862 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002863 break;
John McCall0ad16662009-10-29 08:12:44 +00002864 case TemplateArgument::Null:
2865 case TemplateArgument::Integral:
2866 case TemplateArgument::Declaration:
2867 case TemplateArgument::Pack:
2868 break;
2869 }
2870}
2871
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002872void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002873 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002874 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002875
2876 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2877 bool InfoHasSameExpr
2878 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2879 Record.push_back(InfoHasSameExpr);
2880 if (InfoHasSameExpr)
2881 return; // Avoid storing the same expr twice.
2882 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002883 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2884 Record);
2885}
2886
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002887void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002888 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002889 AddTypeRef(QualType(), Record);
2890 return;
2891 }
2892
John McCallbcd03502009-12-07 02:54:59 +00002893 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002894 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002895 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002896 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002897}
2898
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002899void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002900 Record.push_back(GetOrCreateTypeID(T));
2901}
2902
2903TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002904 return MakeTypeID(T,
2905 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2906}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002907
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002908TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002909 return MakeTypeID(T,
2910 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002911}
2912
2913TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2914 if (T.isNull())
2915 return TypeIdx();
2916 assert(!T.getLocalFastQualifiers());
2917
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002918 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002919 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002920 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002921 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002922 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002923 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002924 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002925 return Idx;
2926}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002927
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002928TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002929 if (T.isNull())
2930 return TypeIdx();
2931 assert(!T.getLocalFastQualifiers());
2932
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002933 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2934 assert(I != TypeIdxs.end() && "Type not emitted!");
2935 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002936}
2937
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002938void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002939 Record.push_back(GetDeclRef(D));
2940}
2941
Sebastian Redl539c5062010-08-18 23:57:32 +00002942DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002943 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002944 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002945 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002946 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00002947 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002948 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002949 // We haven't seen this declaration before. Give it a new ID and
2950 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002951 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002952 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002953 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2954 // We don't add it to the replacement collection here, because we don't
2955 // have the offset yet.
2956 DeclTypesToEmit.push(const_cast<Decl *>(D));
2957 // Reset the flag, so that we don't add this decl multiple times.
2958 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002959 }
2960
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002961 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002962}
2963
Sebastian Redl539c5062010-08-18 23:57:32 +00002964DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002965 if (D == 0)
2966 return 0;
2967
2968 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2969 return DeclIDs[D];
2970}
2971
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002972void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00002973 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002974 Record.push_back(Name.getNameKind());
2975 switch (Name.getNameKind()) {
2976 case DeclarationName::Identifier:
2977 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2978 break;
2979
2980 case DeclarationName::ObjCZeroArgSelector:
2981 case DeclarationName::ObjCOneArgSelector:
2982 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00002983 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002984 break;
2985
2986 case DeclarationName::CXXConstructorName:
2987 case DeclarationName::CXXDestructorName:
2988 case DeclarationName::CXXConversionFunctionName:
2989 AddTypeRef(Name.getCXXNameType(), Record);
2990 break;
2991
2992 case DeclarationName::CXXOperatorName:
2993 Record.push_back(Name.getCXXOverloadedOperator());
2994 break;
2995
Alexis Hunt3d221f22009-11-29 07:34:05 +00002996 case DeclarationName::CXXLiteralOperatorName:
2997 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2998 break;
2999
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003000 case DeclarationName::CXXUsingDirective:
3001 // No extra data to emit
3002 break;
3003 }
3004}
Chris Lattnerca025db2010-05-07 21:43:38 +00003005
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003006void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003007 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003008 switch (Name.getNameKind()) {
3009 case DeclarationName::CXXConstructorName:
3010 case DeclarationName::CXXDestructorName:
3011 case DeclarationName::CXXConversionFunctionName:
3012 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3013 break;
3014
3015 case DeclarationName::CXXOperatorName:
3016 AddSourceLocation(
3017 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3018 Record);
3019 AddSourceLocation(
3020 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3021 Record);
3022 break;
3023
3024 case DeclarationName::CXXLiteralOperatorName:
3025 AddSourceLocation(
3026 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3027 Record);
3028 break;
3029
3030 case DeclarationName::Identifier:
3031 case DeclarationName::ObjCZeroArgSelector:
3032 case DeclarationName::ObjCOneArgSelector:
3033 case DeclarationName::ObjCMultiArgSelector:
3034 case DeclarationName::CXXUsingDirective:
3035 break;
3036 }
3037}
3038
3039void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003040 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003041 AddDeclarationName(NameInfo.getName(), Record);
3042 AddSourceLocation(NameInfo.getLoc(), Record);
3043 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3044}
3045
3046void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003047 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003048 AddNestedNameSpecifier(Info.NNS, Record);
3049 AddSourceRange(Info.NNSRange, Record);
3050 Record.push_back(Info.NumTemplParamLists);
3051 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3052 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3053}
3054
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003055void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003056 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003057 // Nested name specifiers usually aren't too long. I think that 8 would
3058 // typically accomodate the vast majority.
3059 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3060
3061 // Push each of the NNS's onto a stack for serialization in reverse order.
3062 while (NNS) {
3063 NestedNames.push_back(NNS);
3064 NNS = NNS->getPrefix();
3065 }
3066
3067 Record.push_back(NestedNames.size());
3068 while(!NestedNames.empty()) {
3069 NNS = NestedNames.pop_back_val();
3070 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3071 Record.push_back(Kind);
3072 switch (Kind) {
3073 case NestedNameSpecifier::Identifier:
3074 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3075 break;
3076
3077 case NestedNameSpecifier::Namespace:
3078 AddDeclRef(NNS->getAsNamespace(), Record);
3079 break;
3080
3081 case NestedNameSpecifier::TypeSpec:
3082 case NestedNameSpecifier::TypeSpecWithTemplate:
3083 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3084 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3085 break;
3086
3087 case NestedNameSpecifier::Global:
3088 // Don't need to write an associated value.
3089 break;
3090 }
3091 }
3092}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003093
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003094void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003095 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003096 Record.push_back(Kind);
3097 switch (Kind) {
3098 case TemplateName::Template:
3099 AddDeclRef(Name.getAsTemplateDecl(), Record);
3100 break;
3101
3102 case TemplateName::OverloadedTemplate: {
3103 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3104 Record.push_back(OvT->size());
3105 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3106 I != E; ++I)
3107 AddDeclRef(*I, Record);
3108 break;
3109 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003110
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003111 case TemplateName::QualifiedTemplate: {
3112 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3113 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3114 Record.push_back(QualT->hasTemplateKeyword());
3115 AddDeclRef(QualT->getTemplateDecl(), Record);
3116 break;
3117 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003118
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003119 case TemplateName::DependentTemplate: {
3120 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3121 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3122 Record.push_back(DepT->isIdentifier());
3123 if (DepT->isIdentifier())
3124 AddIdentifierRef(DepT->getIdentifier(), Record);
3125 else
3126 Record.push_back(DepT->getOperator());
3127 break;
3128 }
3129 }
3130}
3131
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003132void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003133 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003134 Record.push_back(Arg.getKind());
3135 switch (Arg.getKind()) {
3136 case TemplateArgument::Null:
3137 break;
3138 case TemplateArgument::Type:
3139 AddTypeRef(Arg.getAsType(), Record);
3140 break;
3141 case TemplateArgument::Declaration:
3142 AddDeclRef(Arg.getAsDecl(), Record);
3143 break;
3144 case TemplateArgument::Integral:
3145 AddAPSInt(*Arg.getAsIntegral(), Record);
3146 AddTypeRef(Arg.getIntegralType(), Record);
3147 break;
3148 case TemplateArgument::Template:
3149 AddTemplateName(Arg.getAsTemplate(), Record);
3150 break;
3151 case TemplateArgument::Expression:
3152 AddStmt(Arg.getAsExpr());
3153 break;
3154 case TemplateArgument::Pack:
3155 Record.push_back(Arg.pack_size());
3156 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3157 I != E; ++I)
3158 AddTemplateArgument(*I, Record);
3159 break;
3160 }
3161}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003162
3163void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003164ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003165 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003166 assert(TemplateParams && "No TemplateParams!");
3167 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3168 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3169 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3170 Record.push_back(TemplateParams->size());
3171 for (TemplateParameterList::const_iterator
3172 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3173 P != PEnd; ++P)
3174 AddDeclRef(*P, Record);
3175}
3176
3177/// \brief Emit a template argument list.
3178void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003179ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003180 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003181 assert(TemplateArgs && "No TemplateArgs!");
3182 Record.push_back(TemplateArgs->flat_size());
3183 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
3184 AddTemplateArgument(TemplateArgs->get(i), Record);
3185}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003186
3187
3188void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003189ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003190 Record.push_back(Set.size());
3191 for (UnresolvedSetImpl::const_iterator
3192 I = Set.begin(), E = Set.end(); I != E; ++I) {
3193 AddDeclRef(I.getDecl(), Record);
3194 Record.push_back(I.getAccess());
3195 }
3196}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003197
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003198void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003199 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003200 Record.push_back(Base.isVirtual());
3201 Record.push_back(Base.isBaseOfClass());
3202 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003203 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003204 AddSourceRange(Base.getSourceRange(), Record);
3205}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003206
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003207void ASTWriter::FlushCXXBaseSpecifiers() {
3208 RecordData Record;
3209 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3210 Record.clear();
3211
3212 // Record the offset of this base-specifier set.
3213 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3214 if (Index == CXXBaseSpecifiersOffsets.size())
3215 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3216 else {
3217 if (Index > CXXBaseSpecifiersOffsets.size())
3218 CXXBaseSpecifiersOffsets.resize(Index + 1);
3219 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3220 }
3221
3222 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3223 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3224 Record.push_back(BEnd - B);
3225 for (; B != BEnd; ++B)
3226 AddCXXBaseSpecifier(*B, Record);
3227 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003228
3229 // Flush any expressions that were written as part of the base specifiers.
3230 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003231 }
3232
3233 CXXBaseSpecifiersToWrite.clear();
3234}
3235
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003236void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003237 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003238 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003239 Record.push_back(NumBaseOrMembers);
3240 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3241 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3242
3243 Record.push_back(Init->isBaseInitializer());
3244 if (Init->isBaseInitializer()) {
3245 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3246 Record.push_back(Init->isBaseVirtual());
3247 } else {
3248 AddDeclRef(Init->getMember(), Record);
3249 }
3250 AddSourceLocation(Init->getMemberLocation(), Record);
3251 AddStmt(Init->getInit());
3252 AddDeclRef(Init->getAnonUnionMember(), Record);
3253 AddSourceLocation(Init->getLParenLoc(), Record);
3254 AddSourceLocation(Init->getRParenLoc(), Record);
3255 Record.push_back(Init->isWritten());
3256 if (Init->isWritten()) {
3257 Record.push_back(Init->getSourceOrder());
3258 } else {
3259 Record.push_back(Init->getNumArrayIndices());
3260 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3261 AddDeclRef(Init->getArrayIndex(i), Record);
3262 }
3263 }
3264}
3265
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003266void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3267 assert(D->DefinitionData);
3268 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3269 Record.push_back(Data.UserDeclaredConstructor);
3270 Record.push_back(Data.UserDeclaredCopyConstructor);
3271 Record.push_back(Data.UserDeclaredCopyAssignment);
3272 Record.push_back(Data.UserDeclaredDestructor);
3273 Record.push_back(Data.Aggregate);
3274 Record.push_back(Data.PlainOldData);
3275 Record.push_back(Data.Empty);
3276 Record.push_back(Data.Polymorphic);
3277 Record.push_back(Data.Abstract);
3278 Record.push_back(Data.HasTrivialConstructor);
3279 Record.push_back(Data.HasTrivialCopyConstructor);
3280 Record.push_back(Data.HasTrivialCopyAssignment);
3281 Record.push_back(Data.HasTrivialDestructor);
3282 Record.push_back(Data.ComputedVisibleConversions);
3283 Record.push_back(Data.DeclaredDefaultConstructor);
3284 Record.push_back(Data.DeclaredCopyConstructor);
3285 Record.push_back(Data.DeclaredCopyAssignment);
3286 Record.push_back(Data.DeclaredDestructor);
3287
3288 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003289 if (Data.NumBases > 0)
3290 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3291 Record);
3292
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003293 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3294 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003295 if (Data.NumVBases > 0)
3296 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3297 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003298
3299 AddUnresolvedSet(Data.Conversions, Record);
3300 AddUnresolvedSet(Data.VisibleConversions, Record);
3301 // Data.Definition is the owning decl, no need to write it.
3302 AddDeclRef(Data.FirstFriend, Record);
3303}
3304
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003305void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003306 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003307 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003308 assert(FirstDeclID == NextDeclID &&
3309 FirstTypeID == NextTypeID &&
3310 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003311 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003312 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003313 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003314 "Setting chain after writing has started.");
3315 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003316
3317 FirstDeclID += Chain->getTotalNumDecls();
3318 FirstTypeID += Chain->getTotalNumTypes();
3319 FirstIdentID += Chain->getTotalNumIdentifiers();
3320 FirstSelectorID += Chain->getTotalNumSelectors();
3321 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003322 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003323 NextDeclID = FirstDeclID;
3324 NextTypeID = FirstTypeID;
3325 NextIdentID = FirstIdentID;
3326 NextSelectorID = FirstSelectorID;
3327 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003328 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003329}
3330
Sebastian Redl539c5062010-08-18 23:57:32 +00003331void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003332 IdentifierIDs[II] = ID;
3333}
3334
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003335void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003336 // Always take the highest-numbered type index. This copes with an interesting
3337 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003338 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003339 // keep the higher-numbered entry so that we can properly write it out to
3340 // the AST file.
3341 TypeIdx &StoredIdx = TypeIdxs[T];
3342 if (Idx.getIndex() >= StoredIdx.getIndex())
3343 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003344}
3345
Sebastian Redl539c5062010-08-18 23:57:32 +00003346void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003347 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003348}
Sebastian Redl834bb972010-08-04 17:20:04 +00003349
Sebastian Redl539c5062010-08-18 23:57:32 +00003350void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003351 SelectorIDs[S] = ID;
3352}
Douglas Gregor91096292010-10-02 19:29:26 +00003353
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003354void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003355 MacroDefinition *MD) {
3356 MacroDefinitions[MD] = ID;
3357}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003358
3359void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3360 assert(D->isDefinition());
3361 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3362 // We are interested when a PCH decl is modified.
3363 if (RD->getPCHLevel() > 0) {
3364 // A forward reference was mutated into a definition. Rewrite it.
3365 // FIXME: This happens during template instantiation, should we
3366 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003367 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003368 }
3369
3370 for (CXXRecordDecl::redecl_iterator
3371 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3372 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3373 if (Redecl == RD)
3374 continue;
3375
3376 // We are interested when a PCH decl is modified.
3377 if (Redecl->getPCHLevel() > 0) {
3378 UpdateRecord &Record = DeclUpdates[Redecl];
3379 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3380 assert(Redecl->DefinitionData);
3381 assert(Redecl->DefinitionData->Definition == D);
3382 AddDeclRef(D, Record); // the DefinitionDecl
3383 }
3384 }
3385 }
3386}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003387void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3388 // TU and namespaces are handled elsewhere.
3389 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3390 return;
3391
3392 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3393 return; // Not a source decl added to a DeclContext from PCH.
3394
3395 AddUpdatedDeclContext(DC);
3396}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003397
3398void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3399 assert(D->isImplicit());
3400 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3401 return; // Not a source member added to a class from PCH.
3402 if (!isa<CXXMethodDecl>(D))
3403 return; // We are interested in lazily declared implicit methods.
3404
3405 // A decl coming from PCH was modified.
3406 assert(RD->isDefinition());
3407 UpdateRecord &Record = DeclUpdates[RD];
3408 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3409 AddDeclRef(D, Record);
3410}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003411
3412void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3413 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003414 // The specializations set is kept in the canonical template.
3415 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003416 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3417 return; // Not a source specialization added to a template from PCH.
3418
3419 UpdateRecord &Record = DeclUpdates[TD];
3420 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3421 AddDeclRef(D, Record);
3422}