blob: e4fe99cde3eab7cb3805392f4ba693955bf26c8c [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"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000048#include <string.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051
Sebastian Redl3df5a082010-07-30 17:03:48 +000052template <typename T, typename Allocator>
53T *data(std::vector<T, Allocator> &v) {
54 return v.empty() ? 0 : &v.front();
55}
56template <typename T, typename Allocator>
57const T *data(const std::vector<T, Allocator> &v) {
58 return v.empty() ? 0 : &v.front();
59}
60
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061//===----------------------------------------------------------------------===//
62// Type serialization
63//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000064
Douglas Gregoref84c4b2009-04-09 22:27:44 +000065namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000066 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000067 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000068 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069
70 public:
71 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000072 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000074 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000075 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076
77 void VisitArrayType(const ArrayType *T);
78 void VisitFunctionType(const FunctionType *T);
79 void VisitTagType(const TagType *T);
80
81#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
82#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083#include "clang/AST/TypeNodes.def"
84 };
85}
86
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000087void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088 assert(false && "Built-in types are never serialized");
89}
90
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000091void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000093 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000098 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000113 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114}
115
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000117 Writer.AddTypeRef(T->getPointeeType(), Record);
118 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000119 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000123 Writer.AddTypeRef(T->getElementType(), Record);
124 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000125 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000129 VisitArrayType(T);
130 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000131 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000136 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137}
138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000141 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
142 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000143 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 Writer.AddTypeRef(T->getElementType(), Record);
149 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000150 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000156 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157}
158
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000161 FunctionType::ExtInfo C = T->getExtInfo();
162 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000163 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000164 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000165 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166}
167
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000170 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000171}
172
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000173void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174 VisitFunctionType(T);
175 Record.push_back(T->getNumArgs());
176 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
177 Writer.AddTypeRef(T->getArgType(I), Record);
178 Record.push_back(T->isVariadic());
179 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000180 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000181 Record.push_back(T->hasExceptionSpec());
182 Record.push_back(T->hasAnyExceptionSpec());
183 Record.push_back(T->getNumExceptions());
184 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
185 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000186 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000187}
188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000190 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000191 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000192}
John McCallb96ec562009-12-04 22:46:56 +0000193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000196 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
197 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000198 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000202 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000213 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000214}
215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000217 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000219 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220 "Cannot serialize in the middle of a type definition");
221}
222
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000225 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226}
227
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000230 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231}
232
John McCall81904512011-01-06 01:58:22 +0000233void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
234 Writer.AddTypeRef(T->getModifiedType(), Record);
235 Writer.AddTypeRef(T->getEquivalentType(), Record);
236 Record.push_back(T->getAttrKind());
237 Code = TYPE_ATTRIBUTED;
238}
239
Mike Stump11289f42009-09-09 15:08:12 +0000240void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000241ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000242 const SubstTemplateTypeParmType *T) {
243 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
244 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000246}
247
248void
Douglas Gregorada4b792011-01-14 02:55:32 +0000249ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
250 const SubstTemplateTypeParmPackType *T) {
251 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
252 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
253 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
254}
255
256void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000257ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000259 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000260 Writer.AddTemplateName(T->getTemplateName(), Record);
261 Record.push_back(T->getNumArgs());
262 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
263 ArgI != ArgE; ++ArgI)
264 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000265 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
266 : T->getCanonicalTypeInternal(),
267 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000268 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000269}
270
271void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000273 VisitArrayType(T);
274 Writer.AddStmt(T->getSizeExpr());
275 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000276 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000277}
278
279void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000280ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000281 const DependentSizedExtVectorType *T) {
282 // FIXME: Serialize this type (C++ only)
283 assert(false && "Cannot serialize dependent sized extended vector types");
284}
285
286void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000287ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000288 Record.push_back(T->getDepth());
289 Record.push_back(T->getIndex());
290 Record.push_back(T->isParameterPack());
291 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000292 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000293}
294
295void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000296ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000297 Record.push_back(T->getKeyword());
298 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
299 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000300 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
301 : T->getCanonicalTypeInternal(),
302 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000303 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000308 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000309 Record.push_back(T->getKeyword());
310 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
311 Writer.AddIdentifierRef(T->getIdentifier(), Record);
312 Record.push_back(T->getNumArgs());
313 for (DependentTemplateSpecializationType::iterator
314 I = T->begin(), E = T->end(); I != E; ++I)
315 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000316 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000317}
318
Douglas Gregord2fa7662010-12-20 02:24:11 +0000319void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
320 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000321 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
322 Record.push_back(*NumExpansions + 1);
323 else
324 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000325 Code = TYPE_PACK_EXPANSION;
326}
327
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000328void ASTTypeWriter::VisitParenType(const ParenType *T) {
329 Writer.AddTypeRef(T->getInnerType(), Record);
330 Code = TYPE_PAREN;
331}
332
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000333void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000334 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000337 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000338}
339
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000340void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000341 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000342 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000343 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000344}
345
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000346void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000347 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000352 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000353 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000354 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000355 E = T->qual_end(); I != E; ++I)
356 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000357 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000358}
359
Steve Narofffb4330f2009-06-17 22:40:22 +0000360void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000361ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000362 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000363 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000364}
365
John McCall8f115c62009-10-16 21:56:05 +0000366namespace {
367
368class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000369 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000370 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000371
372public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000373 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000374 : Writer(Writer), Record(Record) { }
375
John McCall17001972009-10-18 01:05:36 +0000376#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000377#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000378 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000379#include "clang/AST/TypeLocNodes.def"
380
John McCall17001972009-10-18 01:05:36 +0000381 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
382 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000383};
384
385}
386
John McCall17001972009-10-18 01:05:36 +0000387void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
388 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000389}
John McCall17001972009-10-18 01:05:36 +0000390void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000391 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
392 if (TL.needsExtraLocalData()) {
393 Record.push_back(TL.getWrittenTypeSpec());
394 Record.push_back(TL.getWrittenSignSpec());
395 Record.push_back(TL.getWrittenWidthSpec());
396 Record.push_back(TL.hasModeAttr());
397 }
John McCall8f115c62009-10-16 21:56:05 +0000398}
John McCall17001972009-10-18 01:05:36 +0000399void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
400 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000401}
John McCall17001972009-10-18 01:05:36 +0000402void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000404}
John McCall17001972009-10-18 01:05:36 +0000405void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000407}
John McCall17001972009-10-18 01:05:36 +0000408void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000410}
John McCall17001972009-10-18 01:05:36 +0000411void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000413}
John McCall17001972009-10-18 01:05:36 +0000414void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000416}
John McCall17001972009-10-18 01:05:36 +0000417void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
419 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
420 Record.push_back(TL.getSizeExpr() ? 1 : 0);
421 if (TL.getSizeExpr())
422 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000423}
John McCall17001972009-10-18 01:05:36 +0000424void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
425 VisitArrayTypeLoc(TL);
426}
427void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
428 VisitArrayTypeLoc(TL);
429}
430void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
431 VisitArrayTypeLoc(TL);
432}
433void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
434 DependentSizedArrayTypeLoc TL) {
435 VisitArrayTypeLoc(TL);
436}
437void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
438 DependentSizedExtVectorTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
440}
441void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
443}
444void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
446}
447void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
449 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000450 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000451 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
452 Writer.AddDeclRef(TL.getArg(i), Record);
453}
454void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
455 VisitFunctionTypeLoc(TL);
456}
457void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
458 VisitFunctionTypeLoc(TL);
459}
John McCallb96ec562009-12-04 22:46:56 +0000460void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getNameLoc(), Record);
462}
John McCall17001972009-10-18 01:05:36 +0000463void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getNameLoc(), Record);
465}
466void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000467 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
468 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
469 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000470}
471void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000472 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
473 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
474 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
475 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000476}
477void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
480void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
John McCall81904512011-01-06 01:58:22 +0000486void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
488 if (TL.hasAttrOperand()) {
489 SourceRange range = TL.getAttrOperandParensRange();
490 Writer.AddSourceLocation(range.getBegin(), Record);
491 Writer.AddSourceLocation(range.getEnd(), Record);
492 }
493 if (TL.hasAttrExprOperand()) {
494 Expr *operand = TL.getAttrExprOperand();
495 Record.push_back(operand ? 1 : 0);
496 if (operand) Writer.AddStmt(operand);
497 } else if (TL.hasAttrEnumOperand()) {
498 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
499 }
500}
John McCall17001972009-10-18 01:05:36 +0000501void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
John McCallcebee162009-10-18 09:09:24 +0000504void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
505 SubstTemplateTypeParmTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getNameLoc(), Record);
507}
Douglas Gregorada4b792011-01-14 02:55:32 +0000508void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
509 SubstTemplateTypeParmPackTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
John McCall17001972009-10-18 01:05:36 +0000512void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
513 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000514 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
515 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
516 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
517 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000518 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
519 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000520}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000521void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
523 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
524}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000525void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000526 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
527 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000528}
John McCalle78aac42010-03-10 03:28:59 +0000529void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000532void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000533 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
534 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
John McCallc392f372010-06-11 00:33:02 +0000537void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
538 DependentTemplateSpecializationTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
540 Writer.AddSourceRange(TL.getQualifierRange(), Record);
541 Writer.AddSourceLocation(TL.getNameLoc(), Record);
542 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
543 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
544 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000545 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
546 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000547}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000548void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
550}
John McCall17001972009-10-18 01:05:36 +0000551void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000553}
554void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
555 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
559 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000560}
John McCallfc93cf92009-10-22 22:37:11 +0000561void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000563}
John McCall8f115c62009-10-16 21:56:05 +0000564
Chris Lattner19cea4e2009-04-22 05:57:30 +0000565//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000566// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000567//===----------------------------------------------------------------------===//
568
Chris Lattner28fa4e62009-04-26 22:26:21 +0000569static void EmitBlockID(unsigned ID, const char *Name,
570 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000571 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000572 Record.clear();
573 Record.push_back(ID);
574 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
575
576 // Emit the block name if present.
577 if (Name == 0 || Name[0] == 0) return;
578 Record.clear();
579 while (*Name)
580 Record.push_back(*Name++);
581 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
582}
583
584static void EmitRecordID(unsigned ID, const char *Name,
585 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000586 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000587 Record.clear();
588 Record.push_back(ID);
589 while (*Name)
590 Record.push_back(*Name++);
591 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000592}
593
594static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000595 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000596#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000597 RECORD(STMT_STOP);
598 RECORD(STMT_NULL_PTR);
599 RECORD(STMT_NULL);
600 RECORD(STMT_COMPOUND);
601 RECORD(STMT_CASE);
602 RECORD(STMT_DEFAULT);
603 RECORD(STMT_LABEL);
604 RECORD(STMT_IF);
605 RECORD(STMT_SWITCH);
606 RECORD(STMT_WHILE);
607 RECORD(STMT_DO);
608 RECORD(STMT_FOR);
609 RECORD(STMT_GOTO);
610 RECORD(STMT_INDIRECT_GOTO);
611 RECORD(STMT_CONTINUE);
612 RECORD(STMT_BREAK);
613 RECORD(STMT_RETURN);
614 RECORD(STMT_DECL);
615 RECORD(STMT_ASM);
616 RECORD(EXPR_PREDEFINED);
617 RECORD(EXPR_DECL_REF);
618 RECORD(EXPR_INTEGER_LITERAL);
619 RECORD(EXPR_FLOATING_LITERAL);
620 RECORD(EXPR_IMAGINARY_LITERAL);
621 RECORD(EXPR_STRING_LITERAL);
622 RECORD(EXPR_CHARACTER_LITERAL);
623 RECORD(EXPR_PAREN);
624 RECORD(EXPR_UNARY_OPERATOR);
625 RECORD(EXPR_SIZEOF_ALIGN_OF);
626 RECORD(EXPR_ARRAY_SUBSCRIPT);
627 RECORD(EXPR_CALL);
628 RECORD(EXPR_MEMBER);
629 RECORD(EXPR_BINARY_OPERATOR);
630 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
631 RECORD(EXPR_CONDITIONAL_OPERATOR);
632 RECORD(EXPR_IMPLICIT_CAST);
633 RECORD(EXPR_CSTYLE_CAST);
634 RECORD(EXPR_COMPOUND_LITERAL);
635 RECORD(EXPR_EXT_VECTOR_ELEMENT);
636 RECORD(EXPR_INIT_LIST);
637 RECORD(EXPR_DESIGNATED_INIT);
638 RECORD(EXPR_IMPLICIT_VALUE_INIT);
639 RECORD(EXPR_VA_ARG);
640 RECORD(EXPR_ADDR_LABEL);
641 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000642 RECORD(EXPR_CHOOSE);
643 RECORD(EXPR_GNU_NULL);
644 RECORD(EXPR_SHUFFLE_VECTOR);
645 RECORD(EXPR_BLOCK);
646 RECORD(EXPR_BLOCK_DECL_REF);
647 RECORD(EXPR_OBJC_STRING_LITERAL);
648 RECORD(EXPR_OBJC_ENCODE);
649 RECORD(EXPR_OBJC_SELECTOR_EXPR);
650 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
651 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
652 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
653 RECORD(EXPR_OBJC_KVC_REF_EXPR);
654 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000655 RECORD(STMT_OBJC_FOR_COLLECTION);
656 RECORD(STMT_OBJC_CATCH);
657 RECORD(STMT_OBJC_FINALLY);
658 RECORD(STMT_OBJC_AT_TRY);
659 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
660 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000661 RECORD(EXPR_CXX_OPERATOR_CALL);
662 RECORD(EXPR_CXX_CONSTRUCT);
663 RECORD(EXPR_CXX_STATIC_CAST);
664 RECORD(EXPR_CXX_DYNAMIC_CAST);
665 RECORD(EXPR_CXX_REINTERPRET_CAST);
666 RECORD(EXPR_CXX_CONST_CAST);
667 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
668 RECORD(EXPR_CXX_BOOL_LITERAL);
669 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000670 RECORD(EXPR_CXX_TYPEID_EXPR);
671 RECORD(EXPR_CXX_TYPEID_TYPE);
672 RECORD(EXPR_CXX_UUIDOF_EXPR);
673 RECORD(EXPR_CXX_UUIDOF_TYPE);
674 RECORD(EXPR_CXX_THIS);
675 RECORD(EXPR_CXX_THROW);
676 RECORD(EXPR_CXX_DEFAULT_ARG);
677 RECORD(EXPR_CXX_BIND_TEMPORARY);
678 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
679 RECORD(EXPR_CXX_NEW);
680 RECORD(EXPR_CXX_DELETE);
681 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
682 RECORD(EXPR_EXPR_WITH_CLEANUPS);
683 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
684 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
685 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
686 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
687 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
688 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
689 RECORD(EXPR_CXX_NOEXCEPT);
690 RECORD(EXPR_OPAQUE_VALUE);
691 RECORD(EXPR_BINARY_TYPE_TRAIT);
692 RECORD(EXPR_PACK_EXPANSION);
693 RECORD(EXPR_SIZEOF_PACK);
694 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000695 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000696#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000697}
Mike Stump11289f42009-09-09 15:08:12 +0000698
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000699void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000700 RecordData Record;
701 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000702
Sebastian Redl539c5062010-08-18 23:57:32 +0000703#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
704#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000705
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000706 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000707 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000708 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000709 RECORD(TYPE_OFFSET);
710 RECORD(DECL_OFFSET);
711 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000712 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000713 RECORD(IDENTIFIER_OFFSET);
714 RECORD(IDENTIFIER_TABLE);
715 RECORD(EXTERNAL_DEFINITIONS);
716 RECORD(SPECIAL_TYPES);
717 RECORD(STATISTICS);
718 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000719 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000720 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
721 RECORD(SELECTOR_OFFSETS);
722 RECORD(METHOD_POOL);
723 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000724 RECORD(SOURCE_LOCATION_OFFSETS);
725 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000726 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000727 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000728 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000729 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000730 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000731 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000732 RECORD(TU_UPDATE_LEXICAL);
733 RECORD(REDECLS_UPDATE_LATEST);
734 RECORD(SEMA_DECL_REFS);
735 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
736 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
737 RECORD(DECL_REPLACEMENTS);
738 RECORD(UPDATE_VISIBLE);
739 RECORD(DECL_UPDATE_OFFSETS);
740 RECORD(DECL_UPDATES);
741 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
742 RECORD(DIAG_PRAGMA_MAPPINGS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000743 RECORD(HEADER_SEARCH_TABLE);
744
Chris Lattner28fa4e62009-04-26 22:26:21 +0000745 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000746 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000747 RECORD(SM_SLOC_FILE_ENTRY);
748 RECORD(SM_SLOC_BUFFER_ENTRY);
749 RECORD(SM_SLOC_BUFFER_BLOB);
750 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
751 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattner28fa4e62009-04-26 22:26:21 +0000753 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000754 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000755 RECORD(PP_MACRO_OBJECT_LIKE);
756 RECORD(PP_MACRO_FUNCTION_LIKE);
757 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000758
Douglas Gregor12bfa382009-10-17 00:13:19 +0000759 // Decls and Types block.
760 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000761 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000762 RECORD(TYPE_COMPLEX);
763 RECORD(TYPE_POINTER);
764 RECORD(TYPE_BLOCK_POINTER);
765 RECORD(TYPE_LVALUE_REFERENCE);
766 RECORD(TYPE_RVALUE_REFERENCE);
767 RECORD(TYPE_MEMBER_POINTER);
768 RECORD(TYPE_CONSTANT_ARRAY);
769 RECORD(TYPE_INCOMPLETE_ARRAY);
770 RECORD(TYPE_VARIABLE_ARRAY);
771 RECORD(TYPE_VECTOR);
772 RECORD(TYPE_EXT_VECTOR);
773 RECORD(TYPE_FUNCTION_PROTO);
774 RECORD(TYPE_FUNCTION_NO_PROTO);
775 RECORD(TYPE_TYPEDEF);
776 RECORD(TYPE_TYPEOF_EXPR);
777 RECORD(TYPE_TYPEOF);
778 RECORD(TYPE_RECORD);
779 RECORD(TYPE_ENUM);
780 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000781 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000782 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000783 RECORD(TYPE_DECLTYPE);
784 RECORD(TYPE_ELABORATED);
785 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
786 RECORD(TYPE_UNRESOLVED_USING);
787 RECORD(TYPE_INJECTED_CLASS_NAME);
788 RECORD(TYPE_OBJC_OBJECT);
789 RECORD(TYPE_TEMPLATE_TYPE_PARM);
790 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
791 RECORD(TYPE_DEPENDENT_NAME);
792 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
793 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
794 RECORD(TYPE_PAREN);
795 RECORD(TYPE_PACK_EXPANSION);
796 RECORD(TYPE_ATTRIBUTED);
797 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000798 RECORD(DECL_TRANSLATION_UNIT);
799 RECORD(DECL_TYPEDEF);
800 RECORD(DECL_ENUM);
801 RECORD(DECL_RECORD);
802 RECORD(DECL_ENUM_CONSTANT);
803 RECORD(DECL_FUNCTION);
804 RECORD(DECL_OBJC_METHOD);
805 RECORD(DECL_OBJC_INTERFACE);
806 RECORD(DECL_OBJC_PROTOCOL);
807 RECORD(DECL_OBJC_IVAR);
808 RECORD(DECL_OBJC_AT_DEFS_FIELD);
809 RECORD(DECL_OBJC_CLASS);
810 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
811 RECORD(DECL_OBJC_CATEGORY);
812 RECORD(DECL_OBJC_CATEGORY_IMPL);
813 RECORD(DECL_OBJC_IMPLEMENTATION);
814 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
815 RECORD(DECL_OBJC_PROPERTY);
816 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000817 RECORD(DECL_FIELD);
818 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000819 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000820 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000821 RECORD(DECL_FILE_SCOPE_ASM);
822 RECORD(DECL_BLOCK);
823 RECORD(DECL_CONTEXT_LEXICAL);
824 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000825 RECORD(DECL_NAMESPACE);
826 RECORD(DECL_NAMESPACE_ALIAS);
827 RECORD(DECL_USING);
828 RECORD(DECL_USING_SHADOW);
829 RECORD(DECL_USING_DIRECTIVE);
830 RECORD(DECL_UNRESOLVED_USING_VALUE);
831 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
832 RECORD(DECL_LINKAGE_SPEC);
833 RECORD(DECL_CXX_RECORD);
834 RECORD(DECL_CXX_METHOD);
835 RECORD(DECL_CXX_CONSTRUCTOR);
836 RECORD(DECL_CXX_DESTRUCTOR);
837 RECORD(DECL_CXX_CONVERSION);
838 RECORD(DECL_ACCESS_SPEC);
839 RECORD(DECL_FRIEND);
840 RECORD(DECL_FRIEND_TEMPLATE);
841 RECORD(DECL_CLASS_TEMPLATE);
842 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
843 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
844 RECORD(DECL_FUNCTION_TEMPLATE);
845 RECORD(DECL_TEMPLATE_TYPE_PARM);
846 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
847 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
848 RECORD(DECL_STATIC_ASSERT);
849 RECORD(DECL_CXX_BASE_SPECIFIERS);
850 RECORD(DECL_INDIRECTFIELD);
851 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
852
Douglas Gregor92a96f52011-02-08 21:58:10 +0000853 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
854 RECORD(PPD_MACRO_INSTANTIATION);
855 RECORD(PPD_MACRO_DEFINITION);
856 RECORD(PPD_INCLUSION_DIRECTIVE);
857
Douglas Gregor12bfa382009-10-17 00:13:19 +0000858 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000859 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000860#undef RECORD
861#undef BLOCK
862 Stream.ExitBlock();
863}
864
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000865/// \brief Adjusts the given filename to only write out the portion of the
866/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000867///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000868/// \param Filename the file name to adjust.
869///
870/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
871/// the returned filename will be adjusted by this system root.
872///
873/// \returns either the original filename (if it needs no adjustment) or the
874/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000875static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000876adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
877 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000878
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000879 if (!isysroot)
880 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000881
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000882 // Verify that the filename and the system root have the same prefix.
883 unsigned Pos = 0;
884 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
885 if (Filename[Pos] != isysroot[Pos])
886 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000887
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000888 // We hit the end of the filename before we hit the end of the system root.
889 if (!Filename[Pos])
890 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000891
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000892 // If the file name has a '/' at the current position, skip over the '/'.
893 // We distinguish sysroot-based includes from absolute includes by the
894 // absence of '/' at the beginning of sysroot-based includes.
895 if (Filename[Pos] == '/')
896 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000897
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000898 return Filename + Pos;
899}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000900
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000901/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000902void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000903 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000904
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000905 // Metadata
906 const TargetInfo &Target = Context.Target;
907 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000908 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000909 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000910 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
911 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000912 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
913 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
914 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000915 // Target triple or chained PCH name
916 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000917 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000918
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000919 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000920 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
921 Record.push_back(VERSION_MAJOR);
922 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000923 Record.push_back(CLANG_VERSION_MAJOR);
924 Record.push_back(CLANG_VERSION_MINOR);
925 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000926 // FIXME: This writes the absolute path for chained headers.
927 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
928 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000929
Douglas Gregor45fe0362009-05-12 01:31:05 +0000930 // Original file name
931 SourceManager &SM = Context.getSourceManager();
932 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
933 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000934 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000935 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
936 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
937
Michael J. Spencer740857f2010-12-21 16:45:57 +0000938 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000939
Michael J. Spencer740857f2010-12-21 16:45:57 +0000940 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000941
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000942 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000943 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000944 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000945 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000946 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000947 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000948 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000949
Ted Kremenek18e066f2010-01-22 22:12:47 +0000950 // Repository branch/version information.
951 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000952 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000953 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
954 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000955 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000956 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000957 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
958 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000959}
960
961/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000962void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000963 RecordData Record;
964 Record.push_back(LangOpts.Trigraphs);
965 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
966 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
967 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
968 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000969 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000970 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
971 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
972 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
973 Record.push_back(LangOpts.C99); // C99 Support
974 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000975 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
976 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000977 Record.push_back(LangOpts.CPlusPlus); // C++ Support
978 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000979 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000980
Douglas Gregor55abb232009-04-10 20:39:37 +0000981 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
982 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000983 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000984 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000985 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000986 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +0000987 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +0000988 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
989 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000990 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000991
Douglas Gregor55abb232009-04-10 20:39:37 +0000992 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000993 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
994 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000995 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000996 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000997 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000998
Douglas Gregordbe39272011-02-01 15:15:22 +0000999 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001000 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1001 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1002 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1003
Chris Lattner258172e2009-04-27 07:35:58 +00001004 // Whether static initializers are protected by locks.
1005 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001006 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001007 Record.push_back(LangOpts.Blocks); // block extension to C
1008 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1009 // they are unused.
1010 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1011 // (modulo the platform support).
1012
Chris Lattner51924e512010-06-26 21:25:03 +00001013 Record.push_back(LangOpts.getSignedOverflowBehavior());
1014 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001015
1016 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001017 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001018 // defined.
1019 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1020 // opposed to __DYNAMIC__).
1021 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1022
1023 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1024 // used (instead of C99 semantics).
1025 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001026 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1027 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001028 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1029 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001030 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001031 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1032 // to the smallest integer type with
1033 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001034 Record.push_back(LangOpts.getGCMode());
1035 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001036 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001037 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001038 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001039 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001040 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001041 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001042 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +00001043 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001044}
1045
Douglas Gregora7f71a92009-04-10 03:52:48 +00001046//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001047// stat cache Serialization
1048//===----------------------------------------------------------------------===//
1049
1050namespace {
1051// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001052class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001053public:
1054 typedef const char * key_type;
1055 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001056
Chris Lattner2a6fa472010-11-23 19:28:12 +00001057 typedef struct stat data_type;
1058 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001059
1060 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001061 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001062 }
Mike Stump11289f42009-09-09 15:08:12 +00001063
1064 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001065 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1066 data_type_ref Data) {
1067 unsigned StrLen = strlen(path);
1068 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001069 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001070 clang::io::Emit8(Out, DataLen);
1071 return std::make_pair(StrLen + 1, DataLen);
1072 }
Mike Stump11289f42009-09-09 15:08:12 +00001073
Douglas Gregorc5046832009-04-27 18:38:38 +00001074 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1075 Out.write(path, KeyLen);
1076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattner2a6fa472010-11-23 19:28:12 +00001078 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001079 data_type_ref Data, unsigned DataLen) {
1080 using namespace clang::io;
1081 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattner2a6fa472010-11-23 19:28:12 +00001083 Emit32(Out, (uint32_t) Data.st_ino);
1084 Emit32(Out, (uint32_t) Data.st_dev);
1085 Emit16(Out, (uint16_t) Data.st_mode);
1086 Emit64(Out, (uint64_t) Data.st_mtime);
1087 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001088
1089 assert(Out.tell() - Start == DataLen && "Wrong data length");
1090 }
1091};
1092} // end anonymous namespace
1093
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001094/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001095void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 // Build the on-disk hash table containing information about every
1097 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001098 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001099 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001100 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001101 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001102 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1103 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001104 Generator.insert(Filename, Stat->second);
1105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Douglas Gregorc5046832009-04-27 18:38:38 +00001107 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001108 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001109 uint32_t BucketOffset;
1110 {
1111 llvm::raw_svector_ostream Out(StatCacheData);
1112 // Make sure that no bucket is at offset 0
1113 clang::io::Emit32(Out, 0);
1114 BucketOffset = Generator.Emit(Out);
1115 }
1116
1117 // Create a blob abbreviation
1118 using namespace llvm;
1119 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001120 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1124 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1125
1126 // Write the stat cache
1127 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001128 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001129 Record.push_back(BucketOffset);
1130 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001131 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001132}
1133
1134//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001135// Source Manager Serialization
1136//===----------------------------------------------------------------------===//
1137
1138/// \brief Create an abbreviation for the SLocEntry that refers to a
1139/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001140static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001141 using namespace llvm;
1142 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001143 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001148 // FileEntry fields.
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001152 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001153}
1154
1155/// \brief Create an abbreviation for the SLocEntry that refers to a
1156/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001157static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001158 using namespace llvm;
1159 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001160 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001166 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001167}
1168
1169/// \brief Create an abbreviation for the SLocEntry that refers to a
1170/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001171static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001172 using namespace llvm;
1173 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001174 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001176 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001177}
1178
1179/// \brief Create an abbreviation for the SLocEntry that refers to an
1180/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001181static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001182 using namespace llvm;
1183 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001184 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001190 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001191}
1192
Douglas Gregor09b69892011-02-10 17:09:37 +00001193namespace {
1194 // Trait used for the on-disk hash table of header search information.
1195 class HeaderFileInfoTrait {
1196 ASTWriter &Writer;
1197 HeaderSearch &HS;
1198
1199 public:
1200 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1201 : Writer(Writer), HS(HS) { }
1202
1203 typedef const char *key_type;
1204 typedef key_type key_type_ref;
1205
1206 typedef HeaderFileInfo data_type;
1207 typedef const data_type &data_type_ref;
1208
1209 static unsigned ComputeHash(const char *path) {
1210 // The hash is based only on the filename portion of the key, so that the
1211 // reader can match based on filenames when symlinking or excess path
1212 // elements ("foo/../", "../") change the form of the name. However,
1213 // complete path is still the key.
1214 return llvm::HashString(llvm::sys::path::filename(path));
1215 }
1216
1217 std::pair<unsigned,unsigned>
1218 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1219 data_type_ref Data) {
1220 unsigned StrLen = strlen(path);
1221 clang::io::Emit16(Out, StrLen);
1222 unsigned DataLen = 1 + 2 + 4;
1223 clang::io::Emit8(Out, DataLen);
1224 return std::make_pair(StrLen + 1, DataLen);
1225 }
1226
1227 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1228 Out.write(path, KeyLen);
1229 }
1230
1231 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1232 data_type_ref Data, unsigned DataLen) {
1233 using namespace clang::io;
1234 uint64_t Start = Out.tell(); (void)Start;
1235
1236 unsigned char Flags = (Data.isImport << 3)
1237 | (Data.DirInfo << 1)
1238 | Data.Resolved;
1239 Emit8(Out, (uint8_t)Flags);
1240 Emit16(Out, (uint16_t) Data.NumIncludes);
1241
1242 if (!Data.ControllingMacro)
1243 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1244 else
1245 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1246 assert(Out.tell() - Start == DataLen && "Wrong data length");
1247 }
1248 };
1249} // end anonymous namespace
1250
1251/// \brief Write the header search block for the list of files that
1252///
1253/// \param HS The header search structure to save.
1254///
1255/// \param Chain Whether we're creating a chained AST file.
1256void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1257 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1258 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1259
1260 if (FilesByUID.size() > HS.header_file_size())
1261 FilesByUID.resize(HS.header_file_size());
1262
1263 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1264 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1265 llvm::SmallVector<const char *, 4> SavedStrings;
1266 unsigned NumHeaderSearchEntries = 0;
1267 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1268 const FileEntry *File = FilesByUID[UID];
1269 if (!File)
1270 continue;
1271
1272 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1273 if (HFI.External && Chain)
1274 continue;
1275
1276 // Turn the file name into an absolute path, if it isn't already.
1277 const char *Filename = File->getName();
1278 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1279
1280 // If we performed any translation on the file name at all, we need to
1281 // save this string, since the generator will refer to it later.
1282 if (Filename != File->getName()) {
1283 Filename = strdup(Filename);
1284 SavedStrings.push_back(Filename);
1285 }
1286
1287 Generator.insert(Filename, HFI, GeneratorTrait);
1288 ++NumHeaderSearchEntries;
1289 }
1290
1291 // Create the on-disk hash table in a buffer.
1292 llvm::SmallString<4096> TableData;
1293 uint32_t BucketOffset;
1294 {
1295 llvm::raw_svector_ostream Out(TableData);
1296 // Make sure that no bucket is at offset 0
1297 clang::io::Emit32(Out, 0);
1298 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1299 }
1300
1301 // Create a blob abbreviation
1302 using namespace llvm;
1303 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1304 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1308 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1309
1310 // Write the stat cache
1311 RecordData Record;
1312 Record.push_back(HEADER_SEARCH_TABLE);
1313 Record.push_back(BucketOffset);
1314 Record.push_back(NumHeaderSearchEntries);
1315 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1316
1317 // Free all of the strings we had to duplicate.
1318 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1319 free((void*)SavedStrings[I]);
1320}
1321
Douglas Gregora7f71a92009-04-10 03:52:48 +00001322/// \brief Writes the block containing the serialized form of the
1323/// source manager.
1324///
1325/// TODO: We should probably use an on-disk hash table (stored in a
1326/// blob), indexed based on the file name, so that we only create
1327/// entries for files that we actually need. In the common case (no
1328/// errors), we probably won't have to create file entries for any of
1329/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001330void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001331 const Preprocessor &PP,
1332 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001333 RecordData Record;
1334
Chris Lattner0910e3b2009-04-10 17:16:57 +00001335 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001336 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001337
1338 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001339 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1340 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1341 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1342 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001343
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001344 // Write the line table.
1345 if (SourceMgr.hasLineTable()) {
1346 LineTableInfo &LineTable = SourceMgr.getLineTable();
1347
1348 // Emit the file names
1349 Record.push_back(LineTable.getNumFilenames());
1350 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1351 // Emit the file name
1352 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001353 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001354 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1355 Record.push_back(FilenameLen);
1356 if (FilenameLen)
1357 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1358 }
Mike Stump11289f42009-09-09 15:08:12 +00001359
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001360 // Emit the line entries
1361 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1362 L != LEnd; ++L) {
1363 // Emit the file ID
1364 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001365
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001366 // Emit the line entries
1367 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001368 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001369 LEEnd = L->second.end();
1370 LE != LEEnd; ++LE) {
1371 Record.push_back(LE->FileOffset);
1372 Record.push_back(LE->LineNo);
1373 Record.push_back(LE->FilenameID);
1374 Record.push_back((unsigned)LE->FileKind);
1375 Record.push_back(LE->IncludeOffset);
1376 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001377 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001378 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001379 }
1380
Douglas Gregor258ae542009-04-27 06:38:32 +00001381 // Write out the source location entry table. We skip the first
1382 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001383 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001384 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001385 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1386 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1387 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1388 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001389 // Get this source location entry.
1390 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001391
Douglas Gregor258ae542009-04-27 06:38:32 +00001392 // Record the offset of this source-location entry.
1393 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1394
1395 // Figure out which record code to use.
1396 unsigned Code;
1397 if (SLoc->isFile()) {
1398 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001399 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001400 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001401 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001402 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001403 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001404 Record.clear();
1405 Record.push_back(Code);
1406
1407 Record.push_back(SLoc->getOffset());
1408 if (SLoc->isFile()) {
1409 const SrcMgr::FileInfo &File = SLoc->getFile();
1410 Record.push_back(File.getIncludeLoc().getRawEncoding());
1411 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1412 Record.push_back(File.hasLineDirectives());
1413
1414 const SrcMgr::ContentCache *Content = File.getContentCache();
1415 if (Content->Entry) {
1416 // The source location entry is a file. The blob associated
1417 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001418
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001419 // Emit size/modification time for this file.
1420 Record.push_back(Content->Entry->getSize());
1421 Record.push_back(Content->Entry->getModificationTime());
1422
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001423 // Turn the file name into an absolute path, if it isn't already.
1424 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001425 llvm::SmallString<128> FilePath(Filename);
1426 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001427 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001428
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001429 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001430 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001431 } else {
1432 // The source location entry is a buffer. The blob associated
1433 // with this entry contains the contents of the buffer.
1434
1435 // We add one to the size so that we capture the trailing NULL
1436 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1437 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001438 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001439 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001440 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001441 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1442 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001443 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001444 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001445 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001446 llvm::StringRef(Buffer->getBufferStart(),
1447 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001448
1449 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001450 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001451 }
1452 } else {
1453 // The source location entry is an instantiation.
1454 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1455 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1456 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1457 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1458
1459 // Compute the token length for this macro expansion.
1460 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001461 if (I + 1 != N)
1462 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001463 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1464 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1465 }
1466 }
1467
Douglas Gregor8f45df52009-04-16 22:23:12 +00001468 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001469
1470 if (SLocEntryOffsets.empty())
1471 return;
1472
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001473 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001474 // table is used for lazily loading source-location information.
1475 using namespace llvm;
1476 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001477 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1481 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001482
Douglas Gregor258ae542009-04-27 06:38:32 +00001483 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001484 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001485 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001486 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1487 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001488 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001489 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001490 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001491
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001492 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001493 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001494 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001495}
1496
Douglas Gregorc5046832009-04-27 18:38:38 +00001497//===----------------------------------------------------------------------===//
1498// Preprocessor Serialization
1499//===----------------------------------------------------------------------===//
1500
Chris Lattnereeffaef2009-04-10 17:15:23 +00001501/// \brief Writes the block containing the serialized form of the
1502/// preprocessor.
1503///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001504void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001505 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001506
Chris Lattner0af3ba12009-04-13 01:29:17 +00001507 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1508 if (PP.getCounterValue() != 0) {
1509 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001510 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001511 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001512 }
1513
1514 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001515 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001516
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001517 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001518 // FIXME: use diagnostics subsystem for localization etc.
1519 if (PP.SawDateOrTime())
1520 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001521
Douglas Gregor796d76a2010-10-20 22:00:55 +00001522
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001523 // Loop over all the macro definitions that are live at the end of the file,
1524 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001525 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001526
Douglas Gregor92a96f52011-02-08 21:58:10 +00001527 // FIXME: If we are chaining, don't visit all of the macros!
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001528 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1529 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001530 // FIXME: This emits macros in hash table order, we should do it in a stable
1531 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001532 MacroInfo *MI = I->second;
1533
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001534 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001535 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001536 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001537
1538 // FIXME: There is a (probably minor) optimization we could do here, if
1539 // the macro comes from the original PCH but the identifier comes from a
1540 // chained PCH, by storing the offset into the original PCH rather than
1541 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001542 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001543 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001544 continue;
1545
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001546 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001547 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001548 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1549 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001550
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001551 unsigned Code;
1552 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001553 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001554 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001555 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001556
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001557 Record.push_back(MI->isC99Varargs());
1558 Record.push_back(MI->isGNUVarargs());
1559 Record.push_back(MI->getNumArgs());
1560 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1561 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001562 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001563 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001564
Douglas Gregoraae92242010-03-19 21:51:54 +00001565 // If we have a detailed preprocessing record, record the macro definition
1566 // ID that corresponds to this macro.
1567 if (PPRec)
1568 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001569
Douglas Gregor8f45df52009-04-16 22:23:12 +00001570 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001571 Record.clear();
1572
Chris Lattner2199f5b2009-04-10 18:08:30 +00001573 // Emit the tokens array.
1574 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1575 // Note that we know that the preprocessor does not have any annotation
1576 // tokens in it because they are created by the parser, and thus can't be
1577 // in a macro definition.
1578 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001579
Chris Lattner2199f5b2009-04-10 18:08:30 +00001580 Record.push_back(Tok.getLocation().getRawEncoding());
1581 Record.push_back(Tok.getLength());
1582
Chris Lattner2199f5b2009-04-10 18:08:30 +00001583 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1584 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001585 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001586
Chris Lattner2199f5b2009-04-10 18:08:30 +00001587 // FIXME: Should translate token kind to a stable encoding.
1588 Record.push_back(Tok.getKind());
1589 // FIXME: Should translate token flags to a stable encoding.
1590 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001591
Sebastian Redl539c5062010-08-18 23:57:32 +00001592 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001593 Record.clear();
1594 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001595 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001596 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001597 Stream.ExitBlock();
1598
1599 if (PPRec)
1600 WritePreprocessorDetail(*PPRec);
1601}
1602
1603void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1604 if (PPRec.begin(Chain) == PPRec.end(Chain))
1605 return;
1606
1607 // Enter the preprocessor block.
1608 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001609
Douglas Gregoraae92242010-03-19 21:51:54 +00001610 // If the preprocessor has a preprocessing record, emit it.
1611 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001612 using namespace llvm;
1613
1614 // Set up the abbreviation for
1615 unsigned InclusionAbbrev = 0;
1616 {
1617 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1618 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1623 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1626 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1627 }
1628
1629 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1630 RecordData Record;
1631 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1632 EEnd = PPRec.end(Chain);
1633 E != EEnd; ++E) {
1634 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001635
Douglas Gregor92a96f52011-02-08 21:58:10 +00001636 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1637 // Record this macro definition's location.
1638 MacroID ID = getMacroDefinitionID(MD);
1639
1640 // Don't write the macro definition if it is from another AST file.
1641 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001642 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001643
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001644 // Notify the serialization listener that we're serializing this entity.
1645 if (SerializationListener)
1646 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001647 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001648
Douglas Gregor92a96f52011-02-08 21:58:10 +00001649 unsigned Position = ID - FirstMacroID;
1650 if (Position != MacroDefinitionOffsets.size()) {
1651 if (Position > MacroDefinitionOffsets.size())
1652 MacroDefinitionOffsets.resize(Position + 1);
1653
1654 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1655 } else
1656 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001657
Douglas Gregor92a96f52011-02-08 21:58:10 +00001658 Record.push_back(IndexBase + NumPreprocessingRecords++);
1659 Record.push_back(ID);
1660 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1661 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1662 AddIdentifierRef(MD->getName(), Record);
1663 AddSourceLocation(MD->getLocation(), Record);
1664 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1665 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001666 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001667
Douglas Gregor92a96f52011-02-08 21:58:10 +00001668 // Notify the serialization listener that we're serializing this entity.
1669 if (SerializationListener)
1670 SerializationListener->SerializedPreprocessedEntity(*E,
1671 Stream.GetCurrentBitNo());
1672
1673 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1674 Record.push_back(IndexBase + NumPreprocessingRecords++);
1675 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1676 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1677 AddIdentifierRef(MI->getName(), Record);
1678 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1679 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1680 continue;
1681 }
1682
1683 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1684 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1685 Record.push_back(IndexBase + NumPreprocessingRecords++);
1686 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1687 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1688 Record.push_back(ID->getFileName().size());
1689 Record.push_back(ID->wasInQuotes());
1690 Record.push_back(static_cast<unsigned>(ID->getKind()));
1691 llvm::SmallString<64> Buffer;
1692 Buffer += ID->getFileName();
1693 Buffer += ID->getFile()->getName();
1694 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1695 continue;
1696 }
1697
1698 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1699 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001700 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001701
Douglas Gregoraae92242010-03-19 21:51:54 +00001702 // Write the offsets table for the preprocessing record.
1703 if (NumPreprocessingRecords > 0) {
1704 // Write the offsets table for identifier IDs.
1705 using namespace llvm;
1706 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001707 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1710 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1711 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001712
Douglas Gregoraae92242010-03-19 21:51:54 +00001713 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001714 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001715 Record.push_back(NumPreprocessingRecords);
1716 Record.push_back(MacroDefinitionOffsets.size());
1717 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001718 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001719 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1720 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001721}
1722
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001723void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001724 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001725 for (Diagnostic::DiagStatePointsTy::const_iterator
1726 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1727 I != E; ++I) {
1728 const Diagnostic::DiagStatePoint &point = *I;
1729 if (point.Loc.isInvalid())
1730 continue;
1731
1732 Record.push_back(point.Loc.getRawEncoding());
1733 for (Diagnostic::DiagState::iterator
1734 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1735 unsigned diag = I->first, map = I->second;
1736 if (map & 0x10) { // mapping from a diagnostic pragma.
1737 Record.push_back(diag);
1738 Record.push_back(map & 0x7);
1739 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001740 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001741 Record.push_back(-1); // mark the end of the diag/map pairs for this
1742 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001743 }
1744
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001745 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001746 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001747}
1748
Douglas Gregorc5046832009-04-27 18:38:38 +00001749//===----------------------------------------------------------------------===//
1750// Type Serialization
1751//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001752
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001753/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001754void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001755 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001756 if (Idx.getIndex() == 0) // we haven't seen this type before.
1757 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001758
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001759 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001760
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001761 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001762 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001763 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001764 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001765 else if (TypeOffsets.size() < Index) {
1766 TypeOffsets.resize(Index + 1);
1767 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001768 }
1769
1770 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001771
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001772 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001773 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001774
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001775 if (T.hasLocalNonFastQualifiers()) {
1776 Qualifiers Qs = T.getLocalQualifiers();
1777 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001778 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001779 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001780 } else {
1781 switch (T->getTypeClass()) {
1782 // For all of the concrete, non-dependent types, call the
1783 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001784#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001785 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001786#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001787#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001788 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001789 }
1790
1791 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001792 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001793
1794 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001795 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001796}
1797
Douglas Gregorc5046832009-04-27 18:38:38 +00001798//===----------------------------------------------------------------------===//
1799// Declaration Serialization
1800//===----------------------------------------------------------------------===//
1801
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001802/// \brief Write the block containing all of the declaration IDs
1803/// lexically declared within the given DeclContext.
1804///
1805/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1806/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001807uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001808 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001809 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001810 return 0;
1811
Douglas Gregor8f45df52009-04-16 22:23:12 +00001812 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001813 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001814 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001815 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001816 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1817 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001818 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001819
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001820 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001821 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001822 reinterpret_cast<char*>(Decls.data()),
1823 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001824 return Offset;
1825}
1826
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001827void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001828 using namespace llvm;
1829 RecordData Record;
1830
1831 // Write the type offsets array
1832 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001833 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001834 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1835 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1836 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1837 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001838 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001839 Record.push_back(TypeOffsets.size());
1840 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001841 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001842 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1843
1844 // Write the declaration offsets array
1845 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001846 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1849 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1850 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001851 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001852 Record.push_back(DeclOffsets.size());
1853 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001854 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001855 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1856}
1857
Douglas Gregorc5046832009-04-27 18:38:38 +00001858//===----------------------------------------------------------------------===//
1859// Global Method Pool and Selector Serialization
1860//===----------------------------------------------------------------------===//
1861
Douglas Gregore84a9da2009-04-20 20:36:09 +00001862namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001863// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001864class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001865 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001866
1867public:
1868 typedef Selector key_type;
1869 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001870
Sebastian Redl834bb972010-08-04 17:20:04 +00001871 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001872 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001873 ObjCMethodList Instance, Factory;
1874 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001875 typedef const data_type& data_type_ref;
1876
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001877 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001878
Douglas Gregorc78d3462009-04-24 21:10:55 +00001879 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001880 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001881 }
Mike Stump11289f42009-09-09 15:08:12 +00001882
1883 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001884 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1885 data_type_ref Methods) {
1886 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1887 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001888 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1889 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001890 Method = Method->Next)
1891 if (Method->Method)
1892 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001893 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001894 Method = Method->Next)
1895 if (Method->Method)
1896 DataLen += 4;
1897 clang::io::Emit16(Out, DataLen);
1898 return std::make_pair(KeyLen, DataLen);
1899 }
Mike Stump11289f42009-09-09 15:08:12 +00001900
Douglas Gregor95c13f52009-04-25 17:48:32 +00001901 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001902 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001903 assert((Start >> 32) == 0 && "Selector key offset too large");
1904 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001905 unsigned N = Sel.getNumArgs();
1906 clang::io::Emit16(Out, N);
1907 if (N == 0)
1908 N = 1;
1909 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001910 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001911 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1912 }
Mike Stump11289f42009-09-09 15:08:12 +00001913
Douglas Gregorc78d3462009-04-24 21:10:55 +00001914 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001915 data_type_ref Methods, unsigned DataLen) {
1916 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001917 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001918 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001919 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001920 Method = Method->Next)
1921 if (Method->Method)
1922 ++NumInstanceMethods;
1923
1924 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001925 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001926 Method = Method->Next)
1927 if (Method->Method)
1928 ++NumFactoryMethods;
1929
1930 clang::io::Emit16(Out, NumInstanceMethods);
1931 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001932 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001933 Method = Method->Next)
1934 if (Method->Method)
1935 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001936 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001937 Method = Method->Next)
1938 if (Method->Method)
1939 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001940
1941 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001942 }
1943};
1944} // end anonymous namespace
1945
Sebastian Redla19a67f2010-08-03 21:58:15 +00001946/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001947///
1948/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001949/// in an on-disk hash table indexed by the selector. The hash table also
1950/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001951void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001952 using namespace llvm;
1953
Sebastian Redla19a67f2010-08-03 21:58:15 +00001954 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001955 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001956 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001957 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001958 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001959 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001960 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001961 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001962
Sebastian Redla19a67f2010-08-03 21:58:15 +00001963 // Create the on-disk hash table representation. We walk through every
1964 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001965 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001966 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001967 I = SelectorIDs.begin(), E = SelectorIDs.end();
1968 I != E; ++I) {
1969 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001970 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001971 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001972 I->second,
1973 ObjCMethodList(),
1974 ObjCMethodList()
1975 };
1976 if (F != SemaRef.MethodPool.end()) {
1977 Data.Instance = F->second.first;
1978 Data.Factory = F->second.second;
1979 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001980 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001981 // changed.
1982 if (Chain && I->second < FirstSelectorID) {
1983 // Selector already exists. Did it change?
1984 bool changed = false;
1985 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1986 M = M->Next) {
1987 if (M->Method->getPCHLevel() == 0)
1988 changed = true;
1989 }
1990 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1991 M = M->Next) {
1992 if (M->Method->getPCHLevel() == 0)
1993 changed = true;
1994 }
1995 if (!changed)
1996 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001997 } else if (Data.Instance.Method || Data.Factory.Method) {
1998 // A new method pool entry.
1999 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002000 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002001 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002002 }
2003
Douglas Gregorc78d3462009-04-24 21:10:55 +00002004 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002005 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002006 uint32_t BucketOffset;
2007 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002008 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002009 llvm::raw_svector_ostream Out(MethodPool);
2010 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002011 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002012 BucketOffset = Generator.Emit(Out, Trait);
2013 }
2014
2015 // Create a blob abbreviation
2016 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002017 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2021 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2022
Douglas Gregor95c13f52009-04-25 17:48:32 +00002023 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002024 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002025 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002026 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002027 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002028 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002029
2030 // Create a blob abbreviation for the selector table offsets.
2031 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002032 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2035 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2036
2037 // Write the selector offsets table.
2038 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002039 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002040 Record.push_back(SelectorOffsets.size());
2041 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002042 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002043 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002044 }
2045}
2046
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002047/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002048void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002049 using namespace llvm;
2050 if (SemaRef.ReferencedSelectors.empty())
2051 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002052
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002053 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002054
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002055 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002056 // very tricky to fix, and given that @selector shouldn't really appear in
2057 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002058 for (DenseMap<Selector, SourceLocation>::iterator S =
2059 SemaRef.ReferencedSelectors.begin(),
2060 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2061 Selector Sel = (*S).first;
2062 SourceLocation Loc = (*S).second;
2063 AddSelectorRef(Sel, Record);
2064 AddSourceLocation(Loc, Record);
2065 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002066 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002067}
2068
Douglas Gregorc5046832009-04-27 18:38:38 +00002069//===----------------------------------------------------------------------===//
2070// Identifier Table Serialization
2071//===----------------------------------------------------------------------===//
2072
Douglas Gregorc78d3462009-04-24 21:10:55 +00002073namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002074class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002075 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002076 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002077
Douglas Gregor1d583f22009-04-28 21:18:29 +00002078 /// \brief Determines whether this is an "interesting" identifier
2079 /// that needs a full IdentifierInfo structure written into the hash
2080 /// table.
2081 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2082 return II->isPoisoned() ||
2083 II->isExtensionToken() ||
2084 II->hasMacroDefinition() ||
2085 II->getObjCOrBuiltinID() ||
2086 II->getFETokenInfo<void>();
2087 }
2088
Douglas Gregore84a9da2009-04-20 20:36:09 +00002089public:
2090 typedef const IdentifierInfo* key_type;
2091 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002092
Sebastian Redl539c5062010-08-18 23:57:32 +00002093 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002094 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002095
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002096 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002097 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002098
2099 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002100 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002101 }
Mike Stump11289f42009-09-09 15:08:12 +00002102
2103 std::pair<unsigned,unsigned>
2104 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002105 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002106 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002107 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2108 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002109 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002110 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002111 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002112 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002113 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2114 DEnd = IdentifierResolver::end();
2115 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002116 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002117 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002118 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002119 // We emit the key length after the data length so that every
2120 // string is preceded by a 16-bit length. This matches the PTH
2121 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002122 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002123 return std::make_pair(KeyLen, DataLen);
2124 }
Mike Stump11289f42009-09-09 15:08:12 +00002125
2126 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002127 unsigned KeyLen) {
2128 // Record the location of the key data. This is used when generating
2129 // the mapping from persistent IDs to strings.
2130 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002131 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002132 }
Mike Stump11289f42009-09-09 15:08:12 +00002133
2134 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002135 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002136 if (!isInterestingIdentifier(II)) {
2137 clang::io::Emit32(Out, ID << 1);
2138 return;
2139 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002140
Douglas Gregor1d583f22009-04-28 21:18:29 +00002141 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002142 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002143 bool hasMacroDefinition =
2144 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002145 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002146 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002147 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2148 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2149 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002150 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002151 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002152 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002153
Douglas Gregorc3366a52009-04-21 23:56:24 +00002154 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002155 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002156
Douglas Gregora868bbd2009-04-21 22:25:48 +00002157 // Emit the declaration IDs in reverse order, because the
2158 // IdentifierResolver provides the declarations as they would be
2159 // visible (e.g., the function "stat" would come before the struct
2160 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2161 // adds declarations to the end of the list (so we need to see the
2162 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002163 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002164 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002165 IdentifierResolver::end());
2166 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2167 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002168 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002169 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002170 }
2171};
2172} // end anonymous namespace
2173
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002174/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002175///
2176/// The identifier table consists of a blob containing string data
2177/// (the actual identifiers themselves) and a separate "offsets" index
2178/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002179void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002180 using namespace llvm;
2181
2182 // Create and write out the blob that contains the identifier
2183 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002184 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002185 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002186 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002187
Douglas Gregore6648fb2009-04-28 20:33:11 +00002188 // Look for any identifiers that were named while processing the
2189 // headers, but are otherwise not needed. We add these to the hash
2190 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002191 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002192 // file.
2193 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2194 IDEnd = PP.getIdentifierTable().end();
2195 ID != IDEnd; ++ID)
2196 getIdentifierRef(ID->second);
2197
Sebastian Redlff4a2952010-07-23 23:49:55 +00002198 // Create the on-disk hash table representation. We only store offsets
2199 // for identifiers that appear here for the first time.
2200 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002201 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002202 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2203 ID != IDEnd; ++ID) {
2204 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002205 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002206 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002207 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002208
Douglas Gregore84a9da2009-04-20 20:36:09 +00002209 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002210 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002211 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002212 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002213 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002214 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002215 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002216 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002217 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002218 }
2219
2220 // Create a blob abbreviation
2221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002222 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002225 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002226
2227 // Write the identifier table
2228 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002229 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002230 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002231 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002232 }
2233
2234 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002236 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2239 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2240
2241 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002242 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002243 Record.push_back(IdentifierOffsets.size());
2244 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002245 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002246 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002247}
2248
Douglas Gregorc5046832009-04-27 18:38:38 +00002249//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002250// DeclContext's Name Lookup Table Serialization
2251//===----------------------------------------------------------------------===//
2252
2253namespace {
2254// Trait used for the on-disk hash table used in the method pool.
2255class ASTDeclContextNameLookupTrait {
2256 ASTWriter &Writer;
2257
2258public:
2259 typedef DeclarationName key_type;
2260 typedef key_type key_type_ref;
2261
2262 typedef DeclContext::lookup_result data_type;
2263 typedef const data_type& data_type_ref;
2264
2265 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2266
2267 unsigned ComputeHash(DeclarationName Name) {
2268 llvm::FoldingSetNodeID ID;
2269 ID.AddInteger(Name.getNameKind());
2270
2271 switch (Name.getNameKind()) {
2272 case DeclarationName::Identifier:
2273 ID.AddString(Name.getAsIdentifierInfo()->getName());
2274 break;
2275 case DeclarationName::ObjCZeroArgSelector:
2276 case DeclarationName::ObjCOneArgSelector:
2277 case DeclarationName::ObjCMultiArgSelector:
2278 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2279 break;
2280 case DeclarationName::CXXConstructorName:
2281 case DeclarationName::CXXDestructorName:
2282 case DeclarationName::CXXConversionFunctionName:
2283 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2284 break;
2285 case DeclarationName::CXXOperatorName:
2286 ID.AddInteger(Name.getCXXOverloadedOperator());
2287 break;
2288 case DeclarationName::CXXLiteralOperatorName:
2289 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2290 case DeclarationName::CXXUsingDirective:
2291 break;
2292 }
2293
2294 return ID.ComputeHash();
2295 }
2296
2297 std::pair<unsigned,unsigned>
2298 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2299 data_type_ref Lookup) {
2300 unsigned KeyLen = 1;
2301 switch (Name.getNameKind()) {
2302 case DeclarationName::Identifier:
2303 case DeclarationName::ObjCZeroArgSelector:
2304 case DeclarationName::ObjCOneArgSelector:
2305 case DeclarationName::ObjCMultiArgSelector:
2306 case DeclarationName::CXXConstructorName:
2307 case DeclarationName::CXXDestructorName:
2308 case DeclarationName::CXXConversionFunctionName:
2309 case DeclarationName::CXXLiteralOperatorName:
2310 KeyLen += 4;
2311 break;
2312 case DeclarationName::CXXOperatorName:
2313 KeyLen += 1;
2314 break;
2315 case DeclarationName::CXXUsingDirective:
2316 break;
2317 }
2318 clang::io::Emit16(Out, KeyLen);
2319
2320 // 2 bytes for num of decls and 4 for each DeclID.
2321 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2322 clang::io::Emit16(Out, DataLen);
2323
2324 return std::make_pair(KeyLen, DataLen);
2325 }
2326
2327 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2328 using namespace clang::io;
2329
2330 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2331 Emit8(Out, Name.getNameKind());
2332 switch (Name.getNameKind()) {
2333 case DeclarationName::Identifier:
2334 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2335 break;
2336 case DeclarationName::ObjCZeroArgSelector:
2337 case DeclarationName::ObjCOneArgSelector:
2338 case DeclarationName::ObjCMultiArgSelector:
2339 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2340 break;
2341 case DeclarationName::CXXConstructorName:
2342 case DeclarationName::CXXDestructorName:
2343 case DeclarationName::CXXConversionFunctionName:
2344 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2345 break;
2346 case DeclarationName::CXXOperatorName:
2347 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2348 Emit8(Out, Name.getCXXOverloadedOperator());
2349 break;
2350 case DeclarationName::CXXLiteralOperatorName:
2351 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2352 break;
2353 case DeclarationName::CXXUsingDirective:
2354 break;
2355 }
2356 }
2357
2358 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2359 data_type Lookup, unsigned DataLen) {
2360 uint64_t Start = Out.tell(); (void)Start;
2361 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2362 for (; Lookup.first != Lookup.second; ++Lookup.first)
2363 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2364
2365 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2366 }
2367};
2368} // end anonymous namespace
2369
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002370/// \brief Write the block containing all of the declaration IDs
2371/// visible from the given DeclContext.
2372///
2373/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002374/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002375uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2376 DeclContext *DC) {
2377 if (DC->getPrimaryContext() != DC)
2378 return 0;
2379
2380 // Since there is no name lookup into functions or methods, don't bother to
2381 // build a visible-declarations table for these entities.
2382 if (DC->isFunctionOrMethod())
2383 return 0;
2384
2385 // If not in C++, we perform name lookup for the translation unit via the
2386 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2387 // FIXME: In C++ we need the visible declarations in order to "see" the
2388 // friend declarations, is there a way to do this without writing the table ?
2389 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2390 return 0;
2391
2392 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002393 if (DC->hasExternalVisibleStorage())
2394 DC->MaterializeVisibleDeclsFromExternalStorage();
2395 else
2396 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002397
2398 // Serialize the contents of the mapping used for lookup. Note that,
2399 // although we have two very different code paths, the serialized
2400 // representation is the same for both cases: a declaration name,
2401 // followed by a size, followed by references to the visible
2402 // declarations that have that name.
2403 uint64_t Offset = Stream.GetCurrentBitNo();
2404 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2405 if (!Map || Map->empty())
2406 return 0;
2407
2408 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2409 ASTDeclContextNameLookupTrait Trait(*this);
2410
2411 // Create the on-disk hash table representation.
2412 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2413 D != DEnd; ++D) {
2414 DeclarationName Name = D->first;
2415 DeclContext::lookup_result Result = D->second.getLookupResult();
2416 Generator.insert(Name, Result, Trait);
2417 }
2418
2419 // Create the on-disk hash table in a buffer.
2420 llvm::SmallString<4096> LookupTable;
2421 uint32_t BucketOffset;
2422 {
2423 llvm::raw_svector_ostream Out(LookupTable);
2424 // Make sure that no bucket is at offset 0
2425 clang::io::Emit32(Out, 0);
2426 BucketOffset = Generator.Emit(Out, Trait);
2427 }
2428
2429 // Write the lookup table
2430 RecordData Record;
2431 Record.push_back(DECL_CONTEXT_VISIBLE);
2432 Record.push_back(BucketOffset);
2433 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2434 LookupTable.str());
2435
2436 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2437 ++NumVisibleDeclContexts;
2438 return Offset;
2439}
2440
Sebastian Redla4071b42010-08-24 00:50:09 +00002441/// \brief Write an UPDATE_VISIBLE block for the given context.
2442///
2443/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2444/// DeclContext in a dependent AST file. As such, they only exist for the TU
2445/// (in C++) and for namespaces.
2446void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002447 // Make the context build its lookup table, but don't make it load external
2448 // decls.
2449 DC->lookup(DeclarationName());
2450
2451 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2452 if (!Map || Map->empty())
2453 return;
2454
2455 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2456 ASTDeclContextNameLookupTrait Trait(*this);
2457
2458 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002459 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2460 D != DEnd; ++D) {
2461 DeclarationName Name = D->first;
2462 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002463 // For any name that appears in this table, the results are complete, i.e.
2464 // they overwrite results from previous PCHs. Merging is always a mess.
2465 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002466 }
2467
2468 // Create the on-disk hash table in a buffer.
2469 llvm::SmallString<4096> LookupTable;
2470 uint32_t BucketOffset;
2471 {
2472 llvm::raw_svector_ostream Out(LookupTable);
2473 // Make sure that no bucket is at offset 0
2474 clang::io::Emit32(Out, 0);
2475 BucketOffset = Generator.Emit(Out, Trait);
2476 }
2477
2478 // Write the lookup table
2479 RecordData Record;
2480 Record.push_back(UPDATE_VISIBLE);
2481 Record.push_back(getDeclID(cast<Decl>(DC)));
2482 Record.push_back(BucketOffset);
2483 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2484}
2485
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002486//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002487// General Serialization Routines
2488//===----------------------------------------------------------------------===//
2489
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002490/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002491void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002492 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002493 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2494 const Attr * A = *i;
2495 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2496 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002497
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002498#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002499
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002500 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002501}
2502
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002503void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002504 Record.push_back(Str.size());
2505 Record.insert(Record.end(), Str.begin(), Str.end());
2506}
2507
Douglas Gregore84a9da2009-04-20 20:36:09 +00002508/// \brief Note that the identifier II occurs at the given offset
2509/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002510void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002511 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002512 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002513 // up earlier in the chain and thus don't need an offset.
2514 if (ID >= FirstIdentID)
2515 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002516}
2517
Douglas Gregor95c13f52009-04-25 17:48:32 +00002518/// \brief Note that the selector Sel occurs at the given offset
2519/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002520void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002521 unsigned ID = SelectorIDs[Sel];
2522 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002523 // Don't record offsets for selectors that are also available in a different
2524 // file.
2525 if (ID < FirstSelectorID)
2526 return;
2527 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002528}
2529
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002530ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002531 : Stream(Stream), Chain(0), SerializationListener(0),
2532 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002533 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002534 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002535 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2536 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002537 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002538 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2539 NextCXXBaseSpecifiersID(1)
2540{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002541}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002542
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002543void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002544 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002545 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002546 Stream.Emit((unsigned)'C', 8);
2547 Stream.Emit((unsigned)'P', 8);
2548 Stream.Emit((unsigned)'C', 8);
2549 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002550
Chris Lattner28fa4e62009-04-26 22:26:21 +00002551 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002552
Sebastian Redl143413f2010-07-12 22:02:52 +00002553 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002554 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002555 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002556 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002557}
2558
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002559void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002560 const char *isysroot) {
2561 using namespace llvm;
2562
2563 ASTContext &Context = SemaRef.Context;
2564 Preprocessor &PP = SemaRef.PP;
2565
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002566 // The translation unit is the first declaration we'll emit.
2567 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002568 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002569 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002570
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002571 // Make sure that we emit IdentifierInfos (and any attached
2572 // declarations) for builtins.
2573 {
2574 IdentifierTable &Table = PP.getIdentifierTable();
2575 llvm::SmallVector<const char *, 32> BuiltinNames;
2576 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2577 Context.getLangOptions().NoBuiltin);
2578 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2579 getIdentifierRef(&Table.get(BuiltinNames[I]));
2580 }
2581
Chris Lattner0c797362009-09-08 18:19:27 +00002582 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002583 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002584 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002585 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002586 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2587 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002588 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002589
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002590 // Build a record containing all of the file scoped decls in this file.
2591 RecordData UnusedFileScopedDecls;
2592 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2593 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002594
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002595 RecordData WeakUndeclaredIdentifiers;
2596 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2597 WeakUndeclaredIdentifiers.push_back(
2598 SemaRef.WeakUndeclaredIdentifiers.size());
2599 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2600 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2601 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2602 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2603 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2604 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2605 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2606 }
2607 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002608
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002609 // Build a record containing all of the locally-scoped external
2610 // declarations in this header file. Generally, this record will be
2611 // empty.
2612 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002613 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002614 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002615 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002616 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2617 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2618 TD != TDEnd; ++TD)
2619 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2620
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002621 // Build a record containing all of the ext_vector declarations.
2622 RecordData ExtVectorDecls;
2623 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2624 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2625
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002626 // Build a record containing all of the VTable uses information.
2627 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002628 if (!SemaRef.VTableUses.empty()) {
2629 VTableUses.push_back(SemaRef.VTableUses.size());
2630 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2631 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2632 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2633 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2634 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002635 }
2636
2637 // Build a record containing all of dynamic classes declarations.
2638 RecordData DynamicClasses;
2639 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2640 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2641
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002642 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002643 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002644 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002645 I = SemaRef.PendingInstantiations.begin(),
2646 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2647 AddDeclRef(I->first, PendingInstantiations);
2648 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002649 }
2650 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2651 "There are local ones at end of translation unit!");
2652
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002653 // Build a record containing some declaration references.
2654 RecordData SemaDeclRefs;
2655 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2656 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2657 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2658 }
2659
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002660 RecordData CUDASpecialDeclRefs;
2661 if (Context.getcudaConfigureCallDecl()) {
2662 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2663 }
2664
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002665 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002666 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002667 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002668 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002669 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002670 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002671 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002672 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002673 // Write the record of special types.
2674 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002675
Steve Naroffc277ad12009-07-18 15:33:26 +00002676 AddTypeRef(Context.getBuiltinVaListType(), Record);
2677 AddTypeRef(Context.getObjCIdType(), Record);
2678 AddTypeRef(Context.getObjCSelType(), Record);
2679 AddTypeRef(Context.getObjCProtoType(), Record);
2680 AddTypeRef(Context.getObjCClassType(), Record);
2681 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2682 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2683 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002684 AddTypeRef(Context.getjmp_bufType(), Record);
2685 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002686 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2687 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002688 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002689 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002690 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2691 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002692 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002693 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002694
Douglas Gregor1970d882009-04-26 03:49:13 +00002695 // Keep writing types and declarations until all types and
2696 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002697 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002698 WriteDeclsBlockAbbrevs();
2699 while (!DeclTypesToEmit.empty()) {
2700 DeclOrType DOT = DeclTypesToEmit.front();
2701 DeclTypesToEmit.pop();
2702 if (DOT.isType())
2703 WriteType(DOT.getType());
2704 else
2705 WriteDecl(Context, DOT.getDecl());
2706 }
2707 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002708
Douglas Gregor45053152009-10-17 17:25:45 +00002709 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002710 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002711 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002712 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002713 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002714
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002715 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002716 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002717
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002718 // Write the C++ base-specifier set offsets.
2719 if (!CXXBaseSpecifiersOffsets.empty()) {
2720 // Create a blob abbreviation for the C++ base specifiers offsets.
2721 using namespace llvm;
2722
2723 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2724 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2727 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2728
2729 // Write the selector offsets table.
2730 Record.clear();
2731 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2732 Record.push_back(CXXBaseSpecifiersOffsets.size());
2733 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2734 (const char *)CXXBaseSpecifiersOffsets.data(),
2735 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2736 }
2737
Douglas Gregord4df8652009-04-22 22:02:47 +00002738 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002739 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002740 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002741
2742 // Write the record containing tentative definitions.
2743 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002744 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002745
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002746 // Write the record containing unused file scoped decls.
2747 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002748 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002749
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002750 // Write the record containing weak undeclared identifiers.
2751 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002752 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002753 WeakUndeclaredIdentifiers);
2754
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002755 // Write the record containing locally-scoped external definitions.
2756 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002757 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002758 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002759
2760 // Write the record containing ext_vector type names.
2761 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002762 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002763
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002764 // Write the record containing VTable uses information.
2765 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002766 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002767
2768 // Write the record containing dynamic classes declarations.
2769 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002770 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002771
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002772 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002773 if (!PendingInstantiations.empty())
2774 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002775
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002776 // Write the record containing declaration references of Sema.
2777 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002778 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002779
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002780 // Write the record containing CUDA-specific declaration references.
2781 if (!CUDASpecialDeclRefs.empty())
2782 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2783
Douglas Gregor08f01292009-04-17 22:13:46 +00002784 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002785 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002786 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002787 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002788 Record.push_back(NumLexicalDeclContexts);
2789 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002790 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002791 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002792}
2793
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002794void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002795 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002796 using namespace llvm;
2797
2798 ASTContext &Context = SemaRef.Context;
2799 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002800
Sebastian Redl143413f2010-07-12 22:02:52 +00002801 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002802 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002803 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002804 if (StatCalls && !isysroot)
2805 WriteStatCache(*StatCalls);
2806 // FIXME: Source manager block should only write new stuff, which could be
2807 // done by tracking the largest ID in the chain
2808 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002809
2810 // The special types are in the chained PCH.
2811
2812 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002813 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002814 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002815 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002816 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2817 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002818 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002819 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002820 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002821 else if ((*I)->isChangedSinceDeserialization())
2822 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002823 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002824 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002825 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002826 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002827 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2828 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2829 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002830 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002831 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2832 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002833 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002834 // And a visible updates block for the DeclContexts.
2835 Abv = new llvm::BitCodeAbbrev();
2836 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2837 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2838 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2839 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2840 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2841 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002842
Sebastian Redl98912122010-07-27 23:01:28 +00002843 // Build a record containing all of the new tentative definitions in this
2844 // file, in TentativeDefinitions order.
2845 RecordData TentativeDefinitions;
2846 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2847 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2848 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2849 }
2850
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002851 // Build a record containing all of the file scoped decls in this file.
2852 RecordData UnusedFileScopedDecls;
2853 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2854 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2855 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002856 }
2857
Sebastian Redl08aca90252010-08-05 18:21:25 +00002858 // We write the entire table, overwriting the tables from the chain.
2859 RecordData WeakUndeclaredIdentifiers;
2860 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2861 WeakUndeclaredIdentifiers.push_back(
2862 SemaRef.WeakUndeclaredIdentifiers.size());
2863 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2864 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2865 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2866 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2867 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2868 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2869 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2870 }
2871 }
2872
Sebastian Redl98912122010-07-27 23:01:28 +00002873 // Build a record containing all of the locally-scoped external
2874 // declarations in this header file. Generally, this record will be
2875 // empty.
2876 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002877 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002878 // nondeterminstic!
2879 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2880 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2881 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2882 TD != TDEnd; ++TD) {
2883 if (TD->second->getPCHLevel() == 0)
2884 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2885 }
2886
2887 // Build a record containing all of the ext_vector declarations.
2888 RecordData ExtVectorDecls;
2889 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2890 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2891 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2892 }
2893
Sebastian Redl08aca90252010-08-05 18:21:25 +00002894 // Build a record containing all of the VTable uses information.
2895 // We write everything here, because it's too hard to determine whether
2896 // a use is new to this part.
2897 RecordData VTableUses;
2898 if (!SemaRef.VTableUses.empty()) {
2899 VTableUses.push_back(SemaRef.VTableUses.size());
2900 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2901 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2902 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2903 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2904 }
2905 }
2906
2907 // Build a record containing all of dynamic classes declarations.
2908 RecordData DynamicClasses;
2909 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2910 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2911 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2912
2913 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002914 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002915 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002916 I = SemaRef.PendingInstantiations.begin(),
2917 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002918 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002919 AddDeclRef(I->first, PendingInstantiations);
2920 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002921 }
2922 }
2923 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2924 "There are local ones at end of translation unit!");
2925
2926 // Build a record containing some declaration references.
2927 // It's not worth the effort to avoid duplication here.
2928 RecordData SemaDeclRefs;
2929 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2930 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2931 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2932 }
2933
Sebastian Redl539c5062010-08-18 23:57:32 +00002934 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002935 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002936 for (DeclsToRewriteTy::iterator
2937 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2938 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002939 while (!DeclTypesToEmit.empty()) {
2940 DeclOrType DOT = DeclTypesToEmit.front();
2941 DeclTypesToEmit.pop();
2942 if (DOT.isType())
2943 WriteType(DOT.getType());
2944 else
2945 WriteDecl(Context, DOT.getDecl());
2946 }
2947 Stream.ExitBlock();
2948
Sebastian Redl98912122010-07-27 23:01:28 +00002949 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002950 WriteSelectors(SemaRef);
2951 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002952 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002953 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002954 // FIXME: For chained PCH only write the new mappings (we currently
2955 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002956 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002957
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002958 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002959 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002960 RecordData FirstLatestDeclIDs;
2961 for (FirstLatestDeclMap::iterator
2962 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2963 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2964 "Expected first & second to be in different PCHs");
2965 AddDeclRef(I->first, FirstLatestDeclIDs);
2966 AddDeclRef(I->second, FirstLatestDeclIDs);
2967 }
2968 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002969 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002970
Sebastian Redl98912122010-07-27 23:01:28 +00002971 // Write the record containing external, unnamed definitions.
2972 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002973 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002974
2975 // Write the record containing tentative definitions.
2976 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002977 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002978
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002979 // Write the record containing unused file scoped decls.
2980 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002981 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002982
Sebastian Redl08aca90252010-08-05 18:21:25 +00002983 // Write the record containing weak undeclared identifiers.
2984 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002985 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002986 WeakUndeclaredIdentifiers);
2987
Sebastian Redl98912122010-07-27 23:01:28 +00002988 // Write the record containing locally-scoped external definitions.
2989 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002990 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002991 LocallyScopedExternalDecls);
2992
2993 // Write the record containing ext_vector type names.
2994 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002995 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002996
Sebastian Redl08aca90252010-08-05 18:21:25 +00002997 // Write the record containing VTable uses information.
2998 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002999 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003000
3001 // Write the record containing dynamic classes declarations.
3002 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003003 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003004
3005 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003006 if (!PendingInstantiations.empty())
3007 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003008
3009 // Write the record containing declaration references of Sema.
3010 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003011 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003012
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003013 // Write the updates to DeclContexts.
3014 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3015 I = UpdatedDeclContexts.begin(),
3016 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003017 I != E; ++I)
3018 WriteDeclContextVisibleUpdate(*I);
3019
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003020 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003021
Sebastian Redl98912122010-07-27 23:01:28 +00003022 Record.clear();
3023 Record.push_back(NumStatements);
3024 Record.push_back(NumMacros);
3025 Record.push_back(NumLexicalDeclContexts);
3026 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003027 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003028 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003029 Stream.ExitBlock();
3030}
3031
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003032void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003033 if (DeclUpdates.empty())
3034 return;
3035
3036 RecordData OffsetsRecord;
3037 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3038 for (DeclUpdateMap::iterator
3039 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3040 const Decl *D = I->first;
3041 UpdateRecord &URec = I->second;
3042
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003043 if (DeclsToRewrite.count(D))
3044 continue; // The decl will be written completely,no need to store updates.
3045
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003046 uint64_t Offset = Stream.GetCurrentBitNo();
3047 Stream.EmitRecord(DECL_UPDATES, URec);
3048
3049 OffsetsRecord.push_back(GetDeclRef(D));
3050 OffsetsRecord.push_back(Offset);
3051 }
3052 Stream.ExitBlock();
3053 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3054}
3055
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003056void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003057 if (ReplacedDecls.empty())
3058 return;
3059
3060 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003061 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003062 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3063 Record.push_back(I->first);
3064 Record.push_back(I->second);
3065 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003066 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003067}
3068
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003069void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003070 Record.push_back(Loc.getRawEncoding());
3071}
3072
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003073void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003074 AddSourceLocation(Range.getBegin(), Record);
3075 AddSourceLocation(Range.getEnd(), Record);
3076}
3077
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003078void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003079 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003080 const uint64_t *Words = Value.getRawData();
3081 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003082}
3083
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003084void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003085 Record.push_back(Value.isUnsigned());
3086 AddAPInt(Value, Record);
3087}
3088
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003089void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003090 AddAPInt(Value.bitcastToAPInt(), Record);
3091}
3092
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003093void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003094 Record.push_back(getIdentifierRef(II));
3095}
3096
Sebastian Redl539c5062010-08-18 23:57:32 +00003097IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003098 if (II == 0)
3099 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003100
Sebastian Redl539c5062010-08-18 23:57:32 +00003101 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003102 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003103 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003104 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003105}
3106
Sebastian Redl50e26582010-09-15 19:54:06 +00003107MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003108 if (MD == 0)
3109 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003110
3111 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003112 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003113 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003114 return ID;
3115}
3116
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003117void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003118 Record.push_back(getSelectorRef(SelRef));
3119}
3120
Sebastian Redl539c5062010-08-18 23:57:32 +00003121SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003122 if (Sel.getAsOpaquePtr() == 0) {
3123 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003124 }
3125
Sebastian Redl539c5062010-08-18 23:57:32 +00003126 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003127 if (SID == 0 && Chain) {
3128 // This might trigger a ReadSelector callback, which will set the ID for
3129 // this selector.
3130 Chain->LoadSelector(Sel);
3131 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003132 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003133 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003134 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003135 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003136}
3137
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003138void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003139 AddDeclRef(Temp->getDestructor(), Record);
3140}
3141
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003142void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3143 CXXBaseSpecifier const *BasesEnd,
3144 RecordDataImpl &Record) {
3145 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3146 CXXBaseSpecifiersToWrite.push_back(
3147 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3148 Bases, BasesEnd));
3149 Record.push_back(NextCXXBaseSpecifiersID++);
3150}
3151
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003152void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003153 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003154 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003155 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003156 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003157 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003158 break;
3159 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003160 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003161 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003162 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003163 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3164 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003165 break;
3166 case TemplateArgument::TemplateExpansion:
3167 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3168 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003169 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003170 break;
John McCall0ad16662009-10-29 08:12:44 +00003171 case TemplateArgument::Null:
3172 case TemplateArgument::Integral:
3173 case TemplateArgument::Declaration:
3174 case TemplateArgument::Pack:
3175 break;
3176 }
3177}
3178
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003179void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003180 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003181 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003182
3183 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3184 bool InfoHasSameExpr
3185 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3186 Record.push_back(InfoHasSameExpr);
3187 if (InfoHasSameExpr)
3188 return; // Avoid storing the same expr twice.
3189 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003190 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3191 Record);
3192}
3193
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003194void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003195 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003196 AddTypeRef(QualType(), Record);
3197 return;
3198 }
3199
John McCallbcd03502009-12-07 02:54:59 +00003200 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00003201 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00003202 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003203 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003204}
3205
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003206void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003207 Record.push_back(GetOrCreateTypeID(T));
3208}
3209
3210TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003211 return MakeTypeID(T,
3212 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3213}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003214
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003215TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003216 return MakeTypeID(T,
3217 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003218}
3219
3220TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3221 if (T.isNull())
3222 return TypeIdx();
3223 assert(!T.getLocalFastQualifiers());
3224
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003225 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003226 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003227 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003228 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003229 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003230 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003231 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003232 return Idx;
3233}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003234
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003235TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003236 if (T.isNull())
3237 return TypeIdx();
3238 assert(!T.getLocalFastQualifiers());
3239
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003240 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3241 assert(I != TypeIdxs.end() && "Type not emitted!");
3242 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003243}
3244
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003245void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003246 Record.push_back(GetDeclRef(D));
3247}
3248
Sebastian Redl539c5062010-08-18 23:57:32 +00003249DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003250 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003251 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003252 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003253 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003254 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003255 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003256 // We haven't seen this declaration before. Give it a new ID and
3257 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003258 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003259 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003260 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3261 // We don't add it to the replacement collection here, because we don't
3262 // have the offset yet.
3263 DeclTypesToEmit.push(const_cast<Decl *>(D));
3264 // Reset the flag, so that we don't add this decl multiple times.
3265 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003266 }
3267
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003268 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003269}
3270
Sebastian Redl539c5062010-08-18 23:57:32 +00003271DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003272 if (D == 0)
3273 return 0;
3274
3275 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3276 return DeclIDs[D];
3277}
3278
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003279void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003280 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003281 Record.push_back(Name.getNameKind());
3282 switch (Name.getNameKind()) {
3283 case DeclarationName::Identifier:
3284 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3285 break;
3286
3287 case DeclarationName::ObjCZeroArgSelector:
3288 case DeclarationName::ObjCOneArgSelector:
3289 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003290 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003291 break;
3292
3293 case DeclarationName::CXXConstructorName:
3294 case DeclarationName::CXXDestructorName:
3295 case DeclarationName::CXXConversionFunctionName:
3296 AddTypeRef(Name.getCXXNameType(), Record);
3297 break;
3298
3299 case DeclarationName::CXXOperatorName:
3300 Record.push_back(Name.getCXXOverloadedOperator());
3301 break;
3302
Alexis Hunt3d221f22009-11-29 07:34:05 +00003303 case DeclarationName::CXXLiteralOperatorName:
3304 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3305 break;
3306
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003307 case DeclarationName::CXXUsingDirective:
3308 // No extra data to emit
3309 break;
3310 }
3311}
Chris Lattnerca025db2010-05-07 21:43:38 +00003312
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003313void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003314 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003315 switch (Name.getNameKind()) {
3316 case DeclarationName::CXXConstructorName:
3317 case DeclarationName::CXXDestructorName:
3318 case DeclarationName::CXXConversionFunctionName:
3319 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3320 break;
3321
3322 case DeclarationName::CXXOperatorName:
3323 AddSourceLocation(
3324 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3325 Record);
3326 AddSourceLocation(
3327 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3328 Record);
3329 break;
3330
3331 case DeclarationName::CXXLiteralOperatorName:
3332 AddSourceLocation(
3333 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3334 Record);
3335 break;
3336
3337 case DeclarationName::Identifier:
3338 case DeclarationName::ObjCZeroArgSelector:
3339 case DeclarationName::ObjCOneArgSelector:
3340 case DeclarationName::ObjCMultiArgSelector:
3341 case DeclarationName::CXXUsingDirective:
3342 break;
3343 }
3344}
3345
3346void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003347 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003348 AddDeclarationName(NameInfo.getName(), Record);
3349 AddSourceLocation(NameInfo.getLoc(), Record);
3350 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3351}
3352
3353void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003354 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003355 AddNestedNameSpecifier(Info.NNS, Record);
3356 AddSourceRange(Info.NNSRange, Record);
3357 Record.push_back(Info.NumTemplParamLists);
3358 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3359 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3360}
3361
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003362void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003363 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003364 // Nested name specifiers usually aren't too long. I think that 8 would
3365 // typically accomodate the vast majority.
3366 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3367
3368 // Push each of the NNS's onto a stack for serialization in reverse order.
3369 while (NNS) {
3370 NestedNames.push_back(NNS);
3371 NNS = NNS->getPrefix();
3372 }
3373
3374 Record.push_back(NestedNames.size());
3375 while(!NestedNames.empty()) {
3376 NNS = NestedNames.pop_back_val();
3377 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3378 Record.push_back(Kind);
3379 switch (Kind) {
3380 case NestedNameSpecifier::Identifier:
3381 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3382 break;
3383
3384 case NestedNameSpecifier::Namespace:
3385 AddDeclRef(NNS->getAsNamespace(), Record);
3386 break;
3387
3388 case NestedNameSpecifier::TypeSpec:
3389 case NestedNameSpecifier::TypeSpecWithTemplate:
3390 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3391 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3392 break;
3393
3394 case NestedNameSpecifier::Global:
3395 // Don't need to write an associated value.
3396 break;
3397 }
3398 }
3399}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003400
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003401void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003402 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003403 Record.push_back(Kind);
3404 switch (Kind) {
3405 case TemplateName::Template:
3406 AddDeclRef(Name.getAsTemplateDecl(), Record);
3407 break;
3408
3409 case TemplateName::OverloadedTemplate: {
3410 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3411 Record.push_back(OvT->size());
3412 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3413 I != E; ++I)
3414 AddDeclRef(*I, Record);
3415 break;
3416 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003417
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003418 case TemplateName::QualifiedTemplate: {
3419 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3420 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3421 Record.push_back(QualT->hasTemplateKeyword());
3422 AddDeclRef(QualT->getTemplateDecl(), Record);
3423 break;
3424 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003425
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003426 case TemplateName::DependentTemplate: {
3427 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3428 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3429 Record.push_back(DepT->isIdentifier());
3430 if (DepT->isIdentifier())
3431 AddIdentifierRef(DepT->getIdentifier(), Record);
3432 else
3433 Record.push_back(DepT->getOperator());
3434 break;
3435 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003436
3437 case TemplateName::SubstTemplateTemplateParmPack: {
3438 SubstTemplateTemplateParmPackStorage *SubstPack
3439 = Name.getAsSubstTemplateTemplateParmPack();
3440 AddDeclRef(SubstPack->getParameterPack(), Record);
3441 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3442 break;
3443 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003444 }
3445}
3446
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003447void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003448 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003449 Record.push_back(Arg.getKind());
3450 switch (Arg.getKind()) {
3451 case TemplateArgument::Null:
3452 break;
3453 case TemplateArgument::Type:
3454 AddTypeRef(Arg.getAsType(), Record);
3455 break;
3456 case TemplateArgument::Declaration:
3457 AddDeclRef(Arg.getAsDecl(), Record);
3458 break;
3459 case TemplateArgument::Integral:
3460 AddAPSInt(*Arg.getAsIntegral(), Record);
3461 AddTypeRef(Arg.getIntegralType(), Record);
3462 break;
3463 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003464 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3465 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003466 case TemplateArgument::TemplateExpansion:
3467 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003468 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3469 Record.push_back(*NumExpansions + 1);
3470 else
3471 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003472 break;
3473 case TemplateArgument::Expression:
3474 AddStmt(Arg.getAsExpr());
3475 break;
3476 case TemplateArgument::Pack:
3477 Record.push_back(Arg.pack_size());
3478 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3479 I != E; ++I)
3480 AddTemplateArgument(*I, Record);
3481 break;
3482 }
3483}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003484
3485void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003486ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003487 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003488 assert(TemplateParams && "No TemplateParams!");
3489 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3490 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3491 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3492 Record.push_back(TemplateParams->size());
3493 for (TemplateParameterList::const_iterator
3494 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3495 P != PEnd; ++P)
3496 AddDeclRef(*P, Record);
3497}
3498
3499/// \brief Emit a template argument list.
3500void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003501ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003502 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003503 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003504 Record.push_back(TemplateArgs->size());
3505 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003506 AddTemplateArgument(TemplateArgs->get(i), Record);
3507}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003508
3509
3510void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003511ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003512 Record.push_back(Set.size());
3513 for (UnresolvedSetImpl::const_iterator
3514 I = Set.begin(), E = Set.end(); I != E; ++I) {
3515 AddDeclRef(I.getDecl(), Record);
3516 Record.push_back(I.getAccess());
3517 }
3518}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003519
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003520void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003521 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003522 Record.push_back(Base.isVirtual());
3523 Record.push_back(Base.isBaseOfClass());
3524 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003525 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003526 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003527 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003528 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3529 : SourceLocation(),
3530 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003531}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003532
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003533void ASTWriter::FlushCXXBaseSpecifiers() {
3534 RecordData Record;
3535 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3536 Record.clear();
3537
3538 // Record the offset of this base-specifier set.
3539 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3540 if (Index == CXXBaseSpecifiersOffsets.size())
3541 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3542 else {
3543 if (Index > CXXBaseSpecifiersOffsets.size())
3544 CXXBaseSpecifiersOffsets.resize(Index + 1);
3545 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3546 }
3547
3548 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3549 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3550 Record.push_back(BEnd - B);
3551 for (; B != BEnd; ++B)
3552 AddCXXBaseSpecifier(*B, Record);
3553 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003554
3555 // Flush any expressions that were written as part of the base specifiers.
3556 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003557 }
3558
3559 CXXBaseSpecifiersToWrite.clear();
3560}
3561
Alexis Hunt1d792652011-01-08 20:30:50 +00003562void ASTWriter::AddCXXCtorInitializers(
3563 const CXXCtorInitializer * const *CtorInitializers,
3564 unsigned NumCtorInitializers,
3565 RecordDataImpl &Record) {
3566 Record.push_back(NumCtorInitializers);
3567 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3568 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003569
3570 Record.push_back(Init->isBaseInitializer());
3571 if (Init->isBaseInitializer()) {
3572 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3573 Record.push_back(Init->isBaseVirtual());
3574 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003575 Record.push_back(Init->isIndirectMemberInitializer());
3576 if (Init->isIndirectMemberInitializer())
3577 AddDeclRef(Init->getIndirectMember(), Record);
3578 else
3579 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003580 }
Francois Pichetd583da02010-12-04 09:14:42 +00003581
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003582 AddSourceLocation(Init->getMemberLocation(), Record);
3583 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003584 AddSourceLocation(Init->getLParenLoc(), Record);
3585 AddSourceLocation(Init->getRParenLoc(), Record);
3586 Record.push_back(Init->isWritten());
3587 if (Init->isWritten()) {
3588 Record.push_back(Init->getSourceOrder());
3589 } else {
3590 Record.push_back(Init->getNumArrayIndices());
3591 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3592 AddDeclRef(Init->getArrayIndex(i), Record);
3593 }
3594 }
3595}
3596
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003597void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3598 assert(D->DefinitionData);
3599 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3600 Record.push_back(Data.UserDeclaredConstructor);
3601 Record.push_back(Data.UserDeclaredCopyConstructor);
3602 Record.push_back(Data.UserDeclaredCopyAssignment);
3603 Record.push_back(Data.UserDeclaredDestructor);
3604 Record.push_back(Data.Aggregate);
3605 Record.push_back(Data.PlainOldData);
3606 Record.push_back(Data.Empty);
3607 Record.push_back(Data.Polymorphic);
3608 Record.push_back(Data.Abstract);
3609 Record.push_back(Data.HasTrivialConstructor);
3610 Record.push_back(Data.HasTrivialCopyConstructor);
3611 Record.push_back(Data.HasTrivialCopyAssignment);
3612 Record.push_back(Data.HasTrivialDestructor);
3613 Record.push_back(Data.ComputedVisibleConversions);
3614 Record.push_back(Data.DeclaredDefaultConstructor);
3615 Record.push_back(Data.DeclaredCopyConstructor);
3616 Record.push_back(Data.DeclaredCopyAssignment);
3617 Record.push_back(Data.DeclaredDestructor);
3618
3619 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003620 if (Data.NumBases > 0)
3621 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3622 Record);
3623
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003624 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3625 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003626 if (Data.NumVBases > 0)
3627 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3628 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003629
3630 AddUnresolvedSet(Data.Conversions, Record);
3631 AddUnresolvedSet(Data.VisibleConversions, Record);
3632 // Data.Definition is the owning decl, no need to write it.
3633 AddDeclRef(Data.FirstFriend, Record);
3634}
3635
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003636void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003637 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003638 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003639 assert(FirstDeclID == NextDeclID &&
3640 FirstTypeID == NextTypeID &&
3641 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003642 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003643 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003644 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003645 "Setting chain after writing has started.");
3646 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003647
3648 FirstDeclID += Chain->getTotalNumDecls();
3649 FirstTypeID += Chain->getTotalNumTypes();
3650 FirstIdentID += Chain->getTotalNumIdentifiers();
3651 FirstSelectorID += Chain->getTotalNumSelectors();
3652 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003653 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003654 NextDeclID = FirstDeclID;
3655 NextTypeID = FirstTypeID;
3656 NextIdentID = FirstIdentID;
3657 NextSelectorID = FirstSelectorID;
3658 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003659 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003660}
3661
Sebastian Redl539c5062010-08-18 23:57:32 +00003662void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003663 IdentifierIDs[II] = ID;
3664}
3665
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003666void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003667 // Always take the highest-numbered type index. This copes with an interesting
3668 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003669 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003670 // keep the higher-numbered entry so that we can properly write it out to
3671 // the AST file.
3672 TypeIdx &StoredIdx = TypeIdxs[T];
3673 if (Idx.getIndex() >= StoredIdx.getIndex())
3674 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003675}
3676
Sebastian Redl539c5062010-08-18 23:57:32 +00003677void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003678 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003679}
Sebastian Redl834bb972010-08-04 17:20:04 +00003680
Sebastian Redl539c5062010-08-18 23:57:32 +00003681void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003682 SelectorIDs[S] = ID;
3683}
Douglas Gregor91096292010-10-02 19:29:26 +00003684
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003685void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003686 MacroDefinition *MD) {
3687 MacroDefinitions[MD] = ID;
3688}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003689
3690void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3691 assert(D->isDefinition());
3692 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3693 // We are interested when a PCH decl is modified.
3694 if (RD->getPCHLevel() > 0) {
3695 // A forward reference was mutated into a definition. Rewrite it.
3696 // FIXME: This happens during template instantiation, should we
3697 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003698 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003699 }
3700
3701 for (CXXRecordDecl::redecl_iterator
3702 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3703 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3704 if (Redecl == RD)
3705 continue;
3706
3707 // We are interested when a PCH decl is modified.
3708 if (Redecl->getPCHLevel() > 0) {
3709 UpdateRecord &Record = DeclUpdates[Redecl];
3710 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3711 assert(Redecl->DefinitionData);
3712 assert(Redecl->DefinitionData->Definition == D);
3713 AddDeclRef(D, Record); // the DefinitionDecl
3714 }
3715 }
3716 }
3717}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003718void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3719 // TU and namespaces are handled elsewhere.
3720 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3721 return;
3722
3723 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3724 return; // Not a source decl added to a DeclContext from PCH.
3725
3726 AddUpdatedDeclContext(DC);
3727}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003728
3729void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3730 assert(D->isImplicit());
3731 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3732 return; // Not a source member added to a class from PCH.
3733 if (!isa<CXXMethodDecl>(D))
3734 return; // We are interested in lazily declared implicit methods.
3735
3736 // A decl coming from PCH was modified.
3737 assert(RD->isDefinition());
3738 UpdateRecord &Record = DeclUpdates[RD];
3739 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3740 AddDeclRef(D, Record);
3741}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003742
3743void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3744 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003745 // The specializations set is kept in the canonical template.
3746 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003747 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3748 return; // Not a source specialization added to a template from PCH.
3749
3750 UpdateRecord &Record = DeclUpdates[TD];
3751 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3752 AddDeclRef(D, Record);
3753}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003754
3755ASTSerializationListener::~ASTSerializationListener() { }